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