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