exofs: Rename raid engine from exofs/ios.c => ore
[linux-2.6-block.git] / fs / exofs / ore.c
CommitLineData
b14f8ab2
BH
1/*
2 * Copyright (C) 2005, 2006
27d2e149 3 * Avishay Traeger (avishay@gmail.com)
b14f8ab2
BH
4 * Copyright (C) 2008, 2009
5 * Boaz Harrosh <bharrosh@panasas.com>
6 *
7 * This file is part of exofs.
8 *
9 * exofs is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation. Since it is based on ext2, and the only
12 * valid version of GPL for the Linux kernel is version 2, the only valid
13 * version of GPL for exofs is version 2.
14 *
15 * exofs is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with exofs; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
5a0e3ad6 25#include <linux/slab.h>
5d952b83 26#include <asm/div64.h>
b14f8ab2 27
8ff660ab 28#include <scsi/osd_ore.h>
b14f8ab2 29
8ff660ab 30#define ORE_ERR(fmt, a...) printk(KERN_ERR "ore: " fmt, ##a)
34ce4e7c 31
8ff660ab
BH
32#ifdef CONFIG_EXOFS_DEBUG
33#define ORE_DBGMSG(fmt, a...) \
34 printk(KERN_NOTICE "ore @%s:%d: " fmt, __func__, __LINE__, ##a)
35#else
36#define ORE_DBGMSG(fmt, a...) \
37 do { if (0) printk(fmt, ##a); } while (0)
38#endif
39
40/* u64 has problems with printk this will cast it to unsigned long long */
41#define _LLU(x) (unsigned long long)(x)
42
43#define ORE_DBGMSG2(M...) do {} while (0)
44/* #define ORE_DBGMSG2 ORE_DBGMSG */
45
46static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
9e9db456
BH
47{
48 return ios->comps->comps[index & ios->comps->single_comp].cred;
49}
50
8ff660ab 51static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
9e9db456
BH
52{
53 return &ios->comps->comps[index & ios->comps->single_comp].obj;
54}
55
8ff660ab 56static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
9e9db456
BH
57{
58 return ios->comps->ods[index];
59}
60
8ff660ab
BH
61int ore_get_rw_state(struct ore_layout *layout, struct ore_components *comps,
62 bool is_reading, u64 offset, u64 length,
63 struct ore_io_state **pios)
b14f8ab2 64{
8ff660ab 65 struct ore_io_state *ios;
06886a5a
BH
66
67 /*TODO: Maybe use kmem_cach per sbi of size
45d3abcb 68 * exofs_io_state_size(layout->s_numdevs)
06886a5a 69 */
8ff660ab 70 ios = kzalloc(ore_io_state_size(comps->numdevs), GFP_KERNEL);
06886a5a 71 if (unlikely(!ios)) {
8ff660ab
BH
72 ORE_DBGMSG("Failed kzalloc bytes=%d\n",
73 ore_io_state_size(comps->numdevs));
06886a5a
BH
74 *pios = NULL;
75 return -ENOMEM;
76 }
77
45d3abcb 78 ios->layout = layout;
9e9db456 79 ios->comps = comps;
e1042ba0
BH
80 ios->offset = offset;
81 ios->length = length;
82 ios->reading = is_reading;
83
06886a5a
BH
84 *pios = ios;
85 return 0;
b14f8ab2
BH
86}
87
8ff660ab
BH
88int ore_get_io_state(struct ore_layout *layout, struct ore_components *comps,
89 struct ore_io_state **ios)
e1042ba0 90{
8ff660ab 91 return ore_get_rw_state(layout, comps, true, 0, 0, ios);
e1042ba0
BH
92}
93
8ff660ab 94void ore_put_io_state(struct ore_io_state *ios)
b14f8ab2 95{
06886a5a
BH
96 if (ios) {
97 unsigned i;
b14f8ab2 98
06886a5a 99 for (i = 0; i < ios->numdevs; i++) {
8ff660ab 100 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
06886a5a
BH
101
102 if (per_dev->or)
103 osd_end_request(per_dev->or);
104 if (per_dev->bio)
105 bio_put(per_dev->bio);
106 }
107
108 kfree(ios);
b14f8ab2 109 }
06886a5a 110}
b14f8ab2 111
8ff660ab 112static void _sync_done(struct ore_io_state *ios, void *p)
06886a5a
BH
113{
114 struct completion *waiting = p;
b14f8ab2 115
06886a5a
BH
116 complete(waiting);
117}
118
119static void _last_io(struct kref *kref)
120{
8ff660ab
BH
121 struct ore_io_state *ios = container_of(
122 kref, struct ore_io_state, kref);
06886a5a
BH
123
124 ios->done(ios, ios->private);
125}
126
127static void _done_io(struct osd_request *or, void *p)
128{
8ff660ab 129 struct ore_io_state *ios = p;
06886a5a
BH
130
131 kref_put(&ios->kref, _last_io);
132}
133
8ff660ab 134static int ore_io_execute(struct ore_io_state *ios)
06886a5a
BH
135{
136 DECLARE_COMPLETION_ONSTACK(wait);
137 bool sync = (ios->done == NULL);
138 int i, ret;
139
140 if (sync) {
141 ios->done = _sync_done;
142 ios->private = &wait;
143 }
144
145 for (i = 0; i < ios->numdevs; i++) {
146 struct osd_request *or = ios->per_dev[i].or;
147 if (unlikely(!or))
148 continue;
149
9e9db456 150 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
06886a5a 151 if (unlikely(ret)) {
8ff660ab 152 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
06886a5a
BH
153 ret);
154 return ret;
155 }
156 }
157
158 kref_init(&ios->kref);
159
160 for (i = 0; i < ios->numdevs; i++) {
161 struct osd_request *or = ios->per_dev[i].or;
162 if (unlikely(!or))
163 continue;
164
165 kref_get(&ios->kref);
166 osd_execute_request_async(or, _done_io, ios);
167 }
168
169 kref_put(&ios->kref, _last_io);
170 ret = 0;
171
172 if (sync) {
173 wait_for_completion(&wait);
8ff660ab 174 ret = ore_check_io(ios, NULL);
06886a5a 175 }
b14f8ab2
BH
176 return ret;
177}
178
22ddc556
BH
179static void _clear_bio(struct bio *bio)
180{
181 struct bio_vec *bv;
182 unsigned i;
183
184 __bio_for_each_segment(bv, bio, i, 0) {
185 unsigned this_count = bv->bv_len;
186
187 if (likely(PAGE_SIZE == this_count))
188 clear_highpage(bv->bv_page);
189 else
190 zero_user(bv->bv_page, bv->bv_offset, this_count);
191 }
192}
193
8ff660ab 194int ore_check_io(struct ore_io_state *ios, u64 *resid)
b14f8ab2 195{
06886a5a
BH
196 enum osd_err_priority acumulated_osd_err = 0;
197 int acumulated_lin_err = 0;
198 int i;
b14f8ab2 199
06886a5a
BH
200 for (i = 0; i < ios->numdevs; i++) {
201 struct osd_sense_info osi;
22ddc556
BH
202 struct osd_request *or = ios->per_dev[i].or;
203 int ret;
204
205 if (unlikely(!or))
206 continue;
06886a5a 207
22ddc556 208 ret = osd_req_decode_sense(or, &osi);
06886a5a
BH
209 if (likely(!ret))
210 continue;
211
22ddc556
BH
212 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
213 /* start read offset passed endof file */
214 _clear_bio(ios->per_dev[i].bio);
8ff660ab 215 ORE_DBGMSG("start read offset passed end of file "
22ddc556 216 "offset=0x%llx, length=0x%llx\n",
5d952b83
BH
217 _LLU(ios->per_dev[i].offset),
218 _LLU(ios->per_dev[i].length));
22ddc556
BH
219
220 continue; /* we recovered */
06886a5a
BH
221 }
222
223 if (osi.osd_err_pri >= acumulated_osd_err) {
224 acumulated_osd_err = osi.osd_err_pri;
225 acumulated_lin_err = ret;
226 }
227 }
228
229 /* TODO: raid specific residual calculations */
230 if (resid) {
231 if (likely(!acumulated_lin_err))
232 *resid = 0;
233 else
234 *resid = ios->length;
235 }
236
237 return acumulated_lin_err;
238}
239
b367e78b
BH
240/*
241 * L - logical offset into the file
242 *
50a76fd3 243 * U - The number of bytes in a stripe within a group
b367e78b
BH
244 *
245 * U = stripe_unit * group_width
246 *
50a76fd3
BH
247 * T - The number of bytes striped within a group of component objects
248 * (before advancing to the next group)
b367e78b 249 *
50a76fd3
BH
250 * T = stripe_unit * group_width * group_depth
251 *
252 * S - The number of bytes striped across all component objects
253 * before the pattern repeats
254 *
255 * S = stripe_unit * group_width * group_depth * group_count
256 *
257 * M - The "major" (i.e., across all components) stripe number
258 *
259 * M = L / S
260 *
261 * G - Counts the groups from the beginning of the major stripe
262 *
263 * G = (L - (M * S)) / T [or (L % S) / T]
264 *
265 * H - The byte offset within the group
266 *
267 * H = (L - (M * S)) % T [or (L % S) % T]
268 *
269 * N - The "minor" (i.e., across the group) stripe number
270 *
271 * N = H / U
b367e78b
BH
272 *
273 * C - The component index coresponding to L
274 *
50a76fd3
BH
275 * C = (H - (N * U)) / stripe_unit + G * group_width
276 * [or (L % U) / stripe_unit + G * group_width]
b367e78b
BH
277 *
278 * O - The component offset coresponding to L
279 *
50a76fd3 280 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
b367e78b 281 */
b367e78b
BH
282struct _striping_info {
283 u64 obj_offset;
50a76fd3 284 u64 group_length;
16f75bb3 285 u64 M; /* for truncate */
b367e78b
BH
286 unsigned dev;
287 unsigned unit_off;
288};
289
8ff660ab 290static void _calc_stripe_info(struct ore_layout *layout, u64 file_offset,
b367e78b 291 struct _striping_info *si)
5d952b83 292{
16f75bb3
BH
293 u32 stripe_unit = layout->stripe_unit;
294 u32 group_width = layout->group_width;
295 u64 group_depth = layout->group_depth;
50a76fd3 296
b367e78b 297 u32 U = stripe_unit * group_width;
50a76fd3 298 u64 T = U * group_depth;
16f75bb3 299 u64 S = T * layout->group_count;
50a76fd3
BH
300 u64 M = div64_u64(file_offset, S);
301
302 /*
303 G = (L - (M * S)) / T
304 H = (L - (M * S)) % T
305 */
306 u64 LmodS = file_offset - M * S;
307 u32 G = div64_u64(LmodS, T);
308 u64 H = LmodS - G * T;
309
310 u32 N = div_u64(H, U);
311
312 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
313 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
16f75bb3 314 si->dev *= layout->mirrors_p1;
b367e78b 315
50a76fd3 316 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
5d952b83 317
50a76fd3
BH
318 si->obj_offset = si->unit_off + (N * stripe_unit) +
319 (M * group_depth * stripe_unit);
320
321 si->group_length = T - H;
16f75bb3 322 si->M = M;
5d952b83
BH
323}
324
8ff660ab
BH
325static int _add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
326 unsigned pgbase, struct ore_per_dev_state *per_dev,
86093aaf 327 int cur_len)
5d952b83 328{
86093aaf 329 unsigned pg = *cur_pg;
5d952b83 330 struct request_queue *q =
9e9db456 331 osd_request_queue(_ios_od(ios, per_dev->dev));
5d952b83
BH
332
333 per_dev->length += cur_len;
334
335 if (per_dev->bio == NULL) {
336 unsigned pages_in_stripe = ios->layout->group_width *
337 (ios->layout->stripe_unit / PAGE_SIZE);
86093aaf 338 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
5d952b83
BH
339 ios->layout->group_width;
340
341 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
342 if (unlikely(!per_dev->bio)) {
8ff660ab 343 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
5d952b83
BH
344 bio_size);
345 return -ENOMEM;
346 }
347 }
348
349 while (cur_len > 0) {
86093aaf
BH
350 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
351 unsigned added_len;
5d952b83 352
86093aaf
BH
353 BUG_ON(ios->nr_pages <= pg);
354 cur_len -= pglen;
5d952b83 355
86093aaf
BH
356 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
357 pglen, pgbase);
358 if (unlikely(pglen != added_len))
5d952b83 359 return -ENOMEM;
86093aaf
BH
360 pgbase = 0;
361 ++pg;
5d952b83
BH
362 }
363 BUG_ON(cur_len);
364
86093aaf 365 *cur_pg = pg;
5d952b83
BH
366 return 0;
367}
368
8ff660ab 369static int _prepare_one_group(struct ore_io_state *ios, u64 length,
6e31609b 370 struct _striping_info *si)
5d952b83 371{
5d952b83 372 unsigned stripe_unit = ios->layout->stripe_unit;
b367e78b 373 unsigned mirrors_p1 = ios->layout->mirrors_p1;
50a76fd3 374 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
b367e78b 375 unsigned dev = si->dev;
50a76fd3 376 unsigned first_dev = dev - (dev % devs_in_group);
50a76fd3
BH
377 unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
378 unsigned cur_pg = ios->pages_consumed;
86093aaf 379 int ret = 0;
5d952b83 380
5d952b83 381 while (length) {
8ff660ab 382 struct ore_per_dev_state *per_dev = &ios->per_dev[dev];
b367e78b 383 unsigned cur_len, page_off = 0;
5d952b83
BH
384
385 if (!per_dev->length) {
b367e78b
BH
386 per_dev->dev = dev;
387 if (dev < si->dev) {
388 per_dev->offset = si->obj_offset + stripe_unit -
389 si->unit_off;
390 cur_len = stripe_unit;
391 } else if (dev == si->dev) {
392 per_dev->offset = si->obj_offset;
393 cur_len = stripe_unit - si->unit_off;
394 page_off = si->unit_off & ~PAGE_MASK;
395 BUG_ON(page_off && (page_off != ios->pgbase));
396 } else { /* dev > si->dev */
397 per_dev->offset = si->obj_offset - si->unit_off;
398 cur_len = stripe_unit;
399 }
5d952b83 400
6e31609b
BH
401 if (max_comp < dev)
402 max_comp = dev;
5d952b83 403 } else {
b367e78b 404 cur_len = stripe_unit;
5d952b83 405 }
b367e78b
BH
406 if (cur_len >= length)
407 cur_len = length;
5d952b83 408
86093aaf
BH
409 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
410 cur_len);
5d952b83
BH
411 if (unlikely(ret))
412 goto out;
413
6e31609b
BH
414 dev += mirrors_p1;
415 dev = (dev % devs_in_group) + first_dev;
5d952b83
BH
416
417 length -= cur_len;
418 }
419out:
50a76fd3
BH
420 ios->numdevs = max_comp + mirrors_p1;
421 ios->pages_consumed = cur_pg;
5d952b83
BH
422 return ret;
423}
424
8ff660ab 425static int _prepare_for_striping(struct ore_io_state *ios)
b367e78b 426{
50a76fd3 427 u64 length = ios->length;
5002dd18 428 u64 offset = ios->offset;
b367e78b 429 struct _striping_info si;
50a76fd3 430 int ret = 0;
b367e78b 431
b367e78b
BH
432 if (!ios->pages) {
433 if (ios->kern_buff) {
8ff660ab 434 struct ore_per_dev_state *per_dev = &ios->per_dev[0];
b367e78b 435
16f75bb3 436 _calc_stripe_info(ios->layout, ios->offset, &si);
b367e78b
BH
437 per_dev->offset = si.obj_offset;
438 per_dev->dev = si.dev;
439
440 /* no cross device without page array */
441 BUG_ON((ios->layout->group_width > 1) &&
442 (si.unit_off + ios->length >
443 ios->layout->stripe_unit));
444 }
445 ios->numdevs = ios->layout->mirrors_p1;
446 return 0;
447 }
448
50a76fd3 449 while (length) {
16f75bb3 450 _calc_stripe_info(ios->layout, offset, &si);
5002dd18 451
50a76fd3
BH
452 if (length < si.group_length)
453 si.group_length = length;
454
6e31609b 455 ret = _prepare_one_group(ios, si.group_length, &si);
50a76fd3
BH
456 if (unlikely(ret))
457 goto out;
458
5002dd18 459 offset += si.group_length;
50a76fd3 460 length -= si.group_length;
50a76fd3
BH
461 }
462
463out:
464 return ret;
b367e78b
BH
465}
466
8ff660ab 467int ore_create(struct ore_io_state *ios)
06886a5a
BH
468{
469 int i, ret;
470
9e9db456 471 for (i = 0; i < ios->comps->numdevs; i++) {
06886a5a
BH
472 struct osd_request *or;
473
9e9db456 474 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
06886a5a 475 if (unlikely(!or)) {
8ff660ab 476 ORE_ERR("%s: osd_start_request failed\n", __func__);
06886a5a
BH
477 ret = -ENOMEM;
478 goto out;
479 }
480 ios->per_dev[i].or = or;
481 ios->numdevs++;
482
9e9db456 483 osd_req_create_object(or, _ios_obj(ios, i));
06886a5a 484 }
8ff660ab 485 ret = ore_io_execute(ios);
06886a5a
BH
486
487out:
488 return ret;
489}
490
8ff660ab 491int ore_remove(struct ore_io_state *ios)
06886a5a
BH
492{
493 int i, ret;
494
9e9db456 495 for (i = 0; i < ios->comps->numdevs; i++) {
06886a5a
BH
496 struct osd_request *or;
497
9e9db456 498 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
06886a5a 499 if (unlikely(!or)) {
8ff660ab 500 ORE_ERR("%s: osd_start_request failed\n", __func__);
06886a5a
BH
501 ret = -ENOMEM;
502 goto out;
503 }
504 ios->per_dev[i].or = or;
505 ios->numdevs++;
506
9e9db456 507 osd_req_remove_object(or, _ios_obj(ios, i));
06886a5a 508 }
8ff660ab 509 ret = ore_io_execute(ios);
06886a5a
BH
510
511out:
512 return ret;
513}
514
8ff660ab 515static int _write_mirror(struct ore_io_state *ios, int cur_comp)
06886a5a 516{
8ff660ab 517 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
5d952b83
BH
518 unsigned dev = ios->per_dev[cur_comp].dev;
519 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
520 int ret = 0;
06886a5a 521
50a76fd3
BH
522 if (ios->pages && !master_dev->length)
523 return 0; /* Just an empty slot */
524
5d952b83 525 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
8ff660ab 526 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
06886a5a
BH
527 struct osd_request *or;
528
9e9db456 529 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
06886a5a 530 if (unlikely(!or)) {
8ff660ab 531 ORE_ERR("%s: osd_start_request failed\n", __func__);
06886a5a
BH
532 ret = -ENOMEM;
533 goto out;
534 }
5d952b83
BH
535 per_dev->or = or;
536 per_dev->offset = master_dev->offset;
06886a5a 537
86093aaf 538 if (ios->pages) {
06886a5a
BH
539 struct bio *bio;
540
5d952b83 541 if (per_dev != master_dev) {
04dc1e88 542 bio = bio_kmalloc(GFP_KERNEL,
5d952b83 543 master_dev->bio->bi_max_vecs);
04dc1e88 544 if (unlikely(!bio)) {
8ff660ab 545 ORE_DBGMSG(
426d3107 546 "Failed to allocate BIO size=%u\n",
5d952b83 547 master_dev->bio->bi_max_vecs);
04dc1e88
BH
548 ret = -ENOMEM;
549 goto out;
550 }
551
5d952b83 552 __bio_clone(bio, master_dev->bio);
04dc1e88
BH
553 bio->bi_bdev = NULL;
554 bio->bi_next = NULL;
5d952b83
BH
555 per_dev->length = master_dev->length;
556 per_dev->bio = bio;
557 per_dev->dev = dev;
04dc1e88 558 } else {
5d952b83
BH
559 bio = master_dev->bio;
560 /* FIXME: bio_set_dir() */
7b6d91da 561 bio->bi_rw |= REQ_WRITE;
04dc1e88 562 }
06886a5a 563
9e9db456
BH
564 osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
565 bio, per_dev->length);
8ff660ab 566 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
34ce4e7c 567 "length=0x%llx dev=%d\n",
9e9db456
BH
568 _LLU(_ios_obj(ios, dev)->id),
569 _LLU(per_dev->offset),
5d952b83 570 _LLU(per_dev->length), dev);
06886a5a 571 } else if (ios->kern_buff) {
9e9db456
BH
572 ret = osd_req_write_kern(or, _ios_obj(ios, dev),
573 per_dev->offset,
574 ios->kern_buff, ios->length);
5d952b83
BH
575 if (unlikely(ret))
576 goto out;
8ff660ab 577 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
34ce4e7c 578 "length=0x%llx dev=%d\n",
9e9db456
BH
579 _LLU(_ios_obj(ios, dev)->id),
580 _LLU(per_dev->offset),
5d952b83 581 _LLU(ios->length), dev);
06886a5a 582 } else {
9e9db456 583 osd_req_set_attributes(or, _ios_obj(ios, dev));
8ff660ab 584 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
9e9db456
BH
585 _LLU(_ios_obj(ios, dev)->id),
586 ios->out_attr_len, dev);
06886a5a
BH
587 }
588
589 if (ios->out_attr)
590 osd_req_add_set_attr_list(or, ios->out_attr,
591 ios->out_attr_len);
592
593 if (ios->in_attr)
594 osd_req_add_get_attr_list(or, ios->in_attr,
595 ios->in_attr_len);
b14f8ab2 596 }
06886a5a
BH
597
598out:
599 return ret;
600}
601
8ff660ab 602int ore_write(struct ore_io_state *ios)
5d952b83
BH
603{
604 int i;
605 int ret;
606
607 ret = _prepare_for_striping(ios);
608 if (unlikely(ret))
609 return ret;
610
611 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
8ff660ab 612 ret = _write_mirror(ios, i);
5d952b83
BH
613 if (unlikely(ret))
614 return ret;
615 }
616
8ff660ab 617 ret = ore_io_execute(ios);
5d952b83
BH
618 return ret;
619}
620
8ff660ab 621static int _read_mirror(struct ore_io_state *ios, unsigned cur_comp)
06886a5a 622{
46f4d973 623 struct osd_request *or;
8ff660ab 624 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
9e9db456
BH
625 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
626 unsigned first_dev = (unsigned)obj->id;
06886a5a 627
50a76fd3
BH
628 if (ios->pages && !per_dev->length)
629 return 0; /* Just an empty slot */
630
5d952b83 631 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
9e9db456 632 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
46f4d973 633 if (unlikely(!or)) {
8ff660ab 634 ORE_ERR("%s: osd_start_request failed\n", __func__);
46f4d973
BH
635 return -ENOMEM;
636 }
637 per_dev->or = or;
46f4d973 638
86093aaf 639 if (ios->pages) {
9e9db456 640 osd_req_read(or, obj, per_dev->offset,
5d952b83 641 per_dev->bio, per_dev->length);
8ff660ab 642 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
9e9db456 643 " dev=%d\n", _LLU(obj->id),
5d952b83 644 _LLU(per_dev->offset), _LLU(per_dev->length),
46f4d973
BH
645 first_dev);
646 } else if (ios->kern_buff) {
9e9db456 647 int ret = osd_req_read_kern(or, obj, per_dev->offset,
46f4d973 648 ios->kern_buff, ios->length);
8ff660ab 649 ORE_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
46f4d973 650 "length=0x%llx dev=%d ret=>%d\n",
9e9db456 651 _LLU(obj->id), _LLU(per_dev->offset),
46f4d973
BH
652 _LLU(ios->length), first_dev, ret);
653 if (unlikely(ret))
654 return ret;
655 } else {
9e9db456 656 osd_req_get_attributes(or, obj);
8ff660ab 657 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
9e9db456
BH
658 _LLU(obj->id),
659 ios->in_attr_len, first_dev);
46f4d973 660 }
46f4d973
BH
661 if (ios->out_attr)
662 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
b14f8ab2 663
46f4d973
BH
664 if (ios->in_attr)
665 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
b14f8ab2 666
5d952b83
BH
667 return 0;
668}
669
8ff660ab 670int ore_read(struct ore_io_state *ios)
5d952b83
BH
671{
672 int i;
673 int ret;
674
675 ret = _prepare_for_striping(ios);
676 if (unlikely(ret))
677 return ret;
678
679 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
8ff660ab 680 ret = _read_mirror(ios, i);
5d952b83
BH
681 if (unlikely(ret))
682 return ret;
683 }
684
8ff660ab 685 ret = ore_io_execute(ios);
5d952b83 686 return ret;
b14f8ab2
BH
687}
688
8ff660ab 689int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
b14f8ab2
BH
690{
691 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
692 void *iter = NULL;
693 int nelem;
694
695 do {
696 nelem = 1;
06886a5a
BH
697 osd_req_decode_get_attr_list(ios->per_dev[0].or,
698 &cur_attr, &nelem, &iter);
b14f8ab2
BH
699 if ((cur_attr.attr_page == attr->attr_page) &&
700 (cur_attr.attr_id == attr->attr_id)) {
701 attr->len = cur_attr.len;
702 attr->val_ptr = cur_attr.val_ptr;
703 return 0;
704 }
705 } while (iter);
706
707 return -EIO;
708}
06886a5a 709
8ff660ab 710static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
5d952b83
BH
711 struct osd_attr *attr)
712{
713 int last_comp = cur_comp + ios->layout->mirrors_p1;
714
715 for (; cur_comp < last_comp; ++cur_comp) {
8ff660ab 716 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
5d952b83
BH
717 struct osd_request *or;
718
9e9db456 719 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
5d952b83 720 if (unlikely(!or)) {
8ff660ab 721 ORE_ERR("%s: osd_start_request failed\n", __func__);
5d952b83
BH
722 return -ENOMEM;
723 }
724 per_dev->or = or;
725
9e9db456 726 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
5d952b83
BH
727 osd_req_add_set_attr_list(or, attr, 1);
728 }
729
730 return 0;
731}
732
16f75bb3
BH
733struct _trunc_info {
734 struct _striping_info si;
735 u64 prev_group_obj_off;
736 u64 next_group_obj_off;
737
738 unsigned first_group_dev;
739 unsigned nex_group_dev;
740 unsigned max_devs;
741};
742
8ff660ab 743void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
16f75bb3
BH
744 struct _trunc_info *ti)
745{
746 unsigned stripe_unit = layout->stripe_unit;
747
748 _calc_stripe_info(layout, file_offset, &ti->si);
749
750 ti->prev_group_obj_off = ti->si.M * stripe_unit;
751 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
752
753 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
754 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
755 ti->max_devs = layout->group_width * layout->group_count;
756}
757
8ff660ab 758int ore_truncate(struct ore_layout *layout, struct ore_components *comps,
9e9db456 759 u64 size)
06886a5a 760{
8ff660ab 761 struct ore_io_state *ios;
5d952b83
BH
762 struct exofs_trunc_attr {
763 struct osd_attr attr;
764 __be64 newsize;
765 } *size_attrs;
16f75bb3 766 struct _trunc_info ti;
06886a5a
BH
767 int i, ret;
768
8ff660ab 769 ret = ore_get_io_state(layout, comps, &ios);
5d952b83
BH
770 if (unlikely(ret))
771 return ret;
772
16f75bb3
BH
773 _calc_trunk_info(ios->layout, size, &ti);
774
775 size_attrs = kcalloc(ti.max_devs, sizeof(*size_attrs),
5d952b83
BH
776 GFP_KERNEL);
777 if (unlikely(!size_attrs)) {
778 ret = -ENOMEM;
779 goto out;
780 }
06886a5a 781
9e9db456 782 ios->numdevs = ios->comps->numdevs;
06886a5a 783
16f75bb3 784 for (i = 0; i < ti.max_devs; ++i) {
5d952b83
BH
785 struct exofs_trunc_attr *size_attr = &size_attrs[i];
786 u64 obj_size;
06886a5a 787
16f75bb3
BH
788 if (i < ti.first_group_dev)
789 obj_size = ti.prev_group_obj_off;
790 else if (i >= ti.nex_group_dev)
791 obj_size = ti.next_group_obj_off;
792 else if (i < ti.si.dev) /* dev within this group */
793 obj_size = ti.si.obj_offset +
794 ios->layout->stripe_unit - ti.si.unit_off;
795 else if (i == ti.si.dev)
796 obj_size = ti.si.obj_offset;
797 else /* i > ti.dev */
798 obj_size = ti.si.obj_offset - ti.si.unit_off;
06886a5a 799
5d952b83
BH
800 size_attr->newsize = cpu_to_be64(obj_size);
801 size_attr->attr = g_attr_logical_length;
802 size_attr->attr.val_ptr = &size_attr->newsize;
803
8ff660ab 804 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
9e9db456 805 _LLU(comps->comps->obj.id), _LLU(obj_size), i);
5d952b83
BH
806 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
807 &size_attr->attr);
808 if (unlikely(ret))
809 goto out;
06886a5a 810 }
8ff660ab 811 ret = ore_io_execute(ios);
06886a5a
BH
812
813out:
5d952b83 814 kfree(size_attrs);
8ff660ab 815 ore_put_io_state(ios);
06886a5a
BH
816 return ret;
817}
85e44df4
BH
818
819const struct osd_attr g_attr_logical_length = ATTR_DEF(
820 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);