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