ore: Fix wrong math in allocation of per device BIO
[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>
143cb494 26#include <linux/module.h>
5d952b83 27#include <asm/div64.h>
a1fec1db 28#include <linux/lcm.h>
b14f8ab2 29
a1fec1db 30#include "ore_raid.h"
8ff660ab 31
cf283ade
BH
32MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
33MODULE_DESCRIPTION("Objects Raid Engine ore.ko");
34MODULE_LICENSE("GPL");
35
5a51c0c7
BH
36/* ore_verify_layout does a couple of things:
37 * 1. Given a minimum number of needed parameters fixes up the rest of the
38 * members to be operatonals for the ore. The needed parameters are those
39 * that are defined by the pnfs-objects layout STD.
40 * 2. Check to see if the current ore code actually supports these parameters
41 * for example stripe_unit must be a multple of the system PAGE_SIZE,
42 * and etc...
43 * 3. Cache some havily used calculations that will be needed by users.
44 */
45
5a51c0c7
BH
46enum { BIO_MAX_PAGES_KMALLOC =
47 (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),};
48
49int ore_verify_layout(unsigned total_comps, struct ore_layout *layout)
50{
51 u64 stripe_length;
52
44231e68
BH
53 switch (layout->raid_algorithm) {
54 case PNFS_OSD_RAID_0:
55 layout->parity = 0;
56 break;
57 case PNFS_OSD_RAID_5:
58 layout->parity = 1;
59 break;
60 case PNFS_OSD_RAID_PQ:
61 case PNFS_OSD_RAID_4:
62 default:
63 ORE_ERR("Only RAID_0/5 for now\n");
5a51c0c7
BH
64 return -EINVAL;
65 }
66 if (0 != (layout->stripe_unit & ~PAGE_MASK)) {
67 ORE_ERR("Stripe Unit(0x%llx)"
68 " must be Multples of PAGE_SIZE(0x%lx)\n",
69 _LLU(layout->stripe_unit), PAGE_SIZE);
70 return -EINVAL;
71 }
72 if (layout->group_width) {
73 if (!layout->group_depth) {
74 ORE_ERR("group_depth == 0 && group_width != 0\n");
75 return -EINVAL;
76 }
77 if (total_comps < (layout->group_width * layout->mirrors_p1)) {
78 ORE_ERR("Data Map wrong, "
79 "numdevs=%d < group_width=%d * mirrors=%d\n",
80 total_comps, layout->group_width,
81 layout->mirrors_p1);
82 return -EINVAL;
83 }
84 layout->group_count = total_comps / layout->mirrors_p1 /
85 layout->group_width;
86 } else {
87 if (layout->group_depth) {
88 printk(KERN_NOTICE "Warning: group_depth ignored "
89 "group_width == 0 && group_depth == %lld\n",
90 _LLU(layout->group_depth));
91 }
92 layout->group_width = total_comps / layout->mirrors_p1;
93 layout->group_depth = -1;
94 layout->group_count = 1;
95 }
96
97 stripe_length = (u64)layout->group_width * layout->stripe_unit;
98 if (stripe_length >= (1ULL << 32)) {
99 ORE_ERR("Stripe_length(0x%llx) >= 32bit is not supported\n",
100 _LLU(stripe_length));
101 return -EINVAL;
102 }
103
104 layout->max_io_length =
105 (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE - layout->stripe_unit) *
aad560b7 106 (layout->group_width - layout->parity);
769ba8d9
BH
107 if (layout->parity) {
108 unsigned stripe_length =
109 (layout->group_width - layout->parity) *
110 layout->stripe_unit;
111
112 layout->max_io_length /= stripe_length;
113 layout->max_io_length *= stripe_length;
114 }
5a51c0c7
BH
115 return 0;
116}
117EXPORT_SYMBOL(ore_verify_layout);
118
8ff660ab 119static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
9e9db456 120{
5bf696da 121 return ios->oc->comps[index & ios->oc->single_comp].cred;
9e9db456
BH
122}
123
8ff660ab 124static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
9e9db456 125{
5bf696da 126 return &ios->oc->comps[index & ios->oc->single_comp].obj;
9e9db456
BH
127}
128
8ff660ab 129static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
9e9db456 130{
3bd98568
BH
131 ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n",
132 ios->oc->first_dev, ios->oc->numdevs, index,
133 ios->oc->ods);
134
d866d875 135 return ore_comp_dev(ios->oc, index);
9e9db456
BH
136}
137
769ba8d9 138int _ore_get_io_state(struct ore_layout *layout,
a1fec1db
BH
139 struct ore_components *oc, unsigned numdevs,
140 unsigned sgs_per_dev, unsigned num_par_pages,
141 struct ore_io_state **pios)
b14f8ab2 142{
8ff660ab 143 struct ore_io_state *ios;
a1fec1db
BH
144 struct page **pages;
145 struct osd_sg_entry *sgilist;
146 struct __alloc_all_io_state {
147 struct ore_io_state ios;
148 struct ore_per_dev_state per_dev[numdevs];
149 union {
150 struct osd_sg_entry sglist[sgs_per_dev * numdevs];
151 struct page *pages[num_par_pages];
152 };
153 } *_aios;
154
155 if (likely(sizeof(*_aios) <= PAGE_SIZE)) {
156 _aios = kzalloc(sizeof(*_aios), GFP_KERNEL);
157 if (unlikely(!_aios)) {
158 ORE_DBGMSG("Failed kzalloc bytes=%zd\n",
159 sizeof(*_aios));
160 *pios = NULL;
161 return -ENOMEM;
162 }
163 pages = num_par_pages ? _aios->pages : NULL;
164 sgilist = sgs_per_dev ? _aios->sglist : NULL;
165 ios = &_aios->ios;
166 } else {
167 struct __alloc_small_io_state {
168 struct ore_io_state ios;
169 struct ore_per_dev_state per_dev[numdevs];
170 } *_aio_small;
171 union __extra_part {
172 struct osd_sg_entry sglist[sgs_per_dev * numdevs];
173 struct page *pages[num_par_pages];
174 } *extra_part;
175
176 _aio_small = kzalloc(sizeof(*_aio_small), GFP_KERNEL);
177 if (unlikely(!_aio_small)) {
178 ORE_DBGMSG("Failed alloc first part bytes=%zd\n",
179 sizeof(*_aio_small));
180 *pios = NULL;
181 return -ENOMEM;
182 }
183 extra_part = kzalloc(sizeof(*extra_part), GFP_KERNEL);
184 if (unlikely(!extra_part)) {
185 ORE_DBGMSG("Failed alloc second part bytes=%zd\n",
186 sizeof(*extra_part));
187 kfree(_aio_small);
188 *pios = NULL;
189 return -ENOMEM;
190 }
06886a5a 191
a1fec1db
BH
192 pages = num_par_pages ? extra_part->pages : NULL;
193 sgilist = sgs_per_dev ? extra_part->sglist : NULL;
194 /* In this case the per_dev[0].sgilist holds the pointer to
195 * be freed
196 */
197 ios = &_aio_small->ios;
198 ios->extra_part_alloc = true;
199 }
200
201 if (pages) {
202 ios->parity_pages = pages;
203 ios->max_par_pages = num_par_pages;
204 }
205 if (sgilist) {
206 unsigned d;
207
208 for (d = 0; d < numdevs; ++d) {
209 ios->per_dev[d].sglist = sgilist;
210 sgilist += sgs_per_dev;
211 }
212 ios->sgs_per_dev = sgs_per_dev;
06886a5a
BH
213 }
214
45d3abcb 215 ios->layout = layout;
5bf696da 216 ios->oc = oc;
b916c5cd
BH
217 *pios = ios;
218 return 0;
219}
220
221/* Allocate an io_state for only a single group of devices
222 *
223 * If a user needs to call ore_read/write() this version must be used becase it
224 * allocates extra stuff for striping and raid.
225 * The ore might decide to only IO less then @length bytes do to alignmets
226 * and constrains as follows:
227 * - The IO cannot cross group boundary.
228 * - In raid5/6 The end of the IO must align at end of a stripe eg.
229 * (@offset + @length) % strip_size == 0. Or the complete range is within a
230 * single stripe.
231 * - Memory condition only permitted a shorter IO. (A user can use @length=~0
232 * And check the returned ios->length for max_io_size.)
233 *
234 * The caller must check returned ios->length (and/or ios->nr_pages) and
235 * re-issue these pages that fall outside of ios->length
236 */
237int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
238 bool is_reading, u64 offset, u64 length,
239 struct ore_io_state **pios)
240{
241 struct ore_io_state *ios;
242 unsigned numdevs = layout->group_width * layout->mirrors_p1;
a1fec1db 243 unsigned sgs_per_dev = 0, max_par_pages = 0;
b916c5cd
BH
244 int ret;
245
a1fec1db
BH
246 if (layout->parity && length) {
247 unsigned data_devs = layout->group_width - layout->parity;
248 unsigned stripe_size = layout->stripe_unit * data_devs;
249 unsigned pages_in_unit = layout->stripe_unit / PAGE_SIZE;
250 u32 remainder;
251 u64 num_stripes;
252 u64 num_raid_units;
253
254 num_stripes = div_u64_rem(length, stripe_size, &remainder);
255 if (remainder)
256 ++num_stripes;
257
258 num_raid_units = num_stripes * layout->parity;
259
260 if (is_reading) {
261 /* For reads add per_dev sglist array */
262 /* TODO: Raid 6 we need twice more. Actually:
263 * num_stripes / LCMdP(W,P);
264 * if (W%P != 0) num_stripes *= parity;
265 */
266
267 /* first/last seg is split */
268 num_raid_units += layout->group_width;
361aba56 269 sgs_per_dev = div_u64(num_raid_units, data_devs) + 2;
a1fec1db
BH
270 } else {
271 /* For Writes add parity pages array. */
272 max_par_pages = num_raid_units * pages_in_unit *
273 sizeof(struct page *);
274 }
275 }
276
277 ret = _ore_get_io_state(layout, oc, numdevs, sgs_per_dev, max_par_pages,
278 pios);
b916c5cd
BH
279 if (unlikely(ret))
280 return ret;
281
282 ios = *pios;
e1042ba0 283 ios->reading = is_reading;
b916c5cd
BH
284 ios->offset = offset;
285
286 if (length) {
a1fec1db
BH
287 ore_calc_stripe_info(layout, offset, length, &ios->si);
288 ios->length = ios->si.length;
aad560b7
BH
289 ios->nr_pages = ((ios->offset & (PAGE_SIZE - 1)) +
290 ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
a1fec1db
BH
291 if (layout->parity)
292 _ore_post_alloc_raid_stuff(ios);
b916c5cd 293 }
e1042ba0 294
06886a5a 295 return 0;
b14f8ab2 296}
cf283ade 297EXPORT_SYMBOL(ore_get_rw_state);
b14f8ab2 298
b916c5cd
BH
299/* Allocate an io_state for all the devices in the comps array
300 *
301 * This version of io_state allocation is used mostly by create/remove
302 * and trunc where we currently need all the devices. The only wastful
303 * bit is the read/write_attributes with no IO. Those sites should
304 * be converted to use ore_get_rw_state() with length=0
305 */
5bf696da 306int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
b916c5cd 307 struct ore_io_state **pios)
e1042ba0 308{
a1fec1db 309 return _ore_get_io_state(layout, oc, oc->numdevs, 0, 0, pios);
e1042ba0 310}
cf283ade 311EXPORT_SYMBOL(ore_get_io_state);
e1042ba0 312
8ff660ab 313void ore_put_io_state(struct ore_io_state *ios)
b14f8ab2 314{
06886a5a
BH
315 if (ios) {
316 unsigned i;
b14f8ab2 317
06886a5a 318 for (i = 0; i < ios->numdevs; i++) {
8ff660ab 319 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
06886a5a
BH
320
321 if (per_dev->or)
322 osd_end_request(per_dev->or);
323 if (per_dev->bio)
324 bio_put(per_dev->bio);
325 }
326
a1fec1db 327 _ore_free_raid_stuff(ios);
06886a5a 328 kfree(ios);
b14f8ab2 329 }
06886a5a 330}
cf283ade 331EXPORT_SYMBOL(ore_put_io_state);
b14f8ab2 332
8ff660ab 333static void _sync_done(struct ore_io_state *ios, void *p)
06886a5a
BH
334{
335 struct completion *waiting = p;
b14f8ab2 336
06886a5a
BH
337 complete(waiting);
338}
339
340static void _last_io(struct kref *kref)
341{
8ff660ab
BH
342 struct ore_io_state *ios = container_of(
343 kref, struct ore_io_state, kref);
06886a5a
BH
344
345 ios->done(ios, ios->private);
346}
347
348static void _done_io(struct osd_request *or, void *p)
349{
8ff660ab 350 struct ore_io_state *ios = p;
06886a5a
BH
351
352 kref_put(&ios->kref, _last_io);
353}
354
769ba8d9 355int ore_io_execute(struct ore_io_state *ios)
06886a5a
BH
356{
357 DECLARE_COMPLETION_ONSTACK(wait);
358 bool sync = (ios->done == NULL);
359 int i, ret;
360
361 if (sync) {
362 ios->done = _sync_done;
363 ios->private = &wait;
364 }
365
366 for (i = 0; i < ios->numdevs; i++) {
367 struct osd_request *or = ios->per_dev[i].or;
368 if (unlikely(!or))
369 continue;
370
9e9db456 371 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
06886a5a 372 if (unlikely(ret)) {
8ff660ab 373 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
06886a5a
BH
374 ret);
375 return ret;
376 }
377 }
378
379 kref_init(&ios->kref);
380
381 for (i = 0; i < ios->numdevs; i++) {
382 struct osd_request *or = ios->per_dev[i].or;
383 if (unlikely(!or))
384 continue;
385
386 kref_get(&ios->kref);
387 osd_execute_request_async(or, _done_io, ios);
388 }
389
390 kref_put(&ios->kref, _last_io);
391 ret = 0;
392
393 if (sync) {
394 wait_for_completion(&wait);
8ff660ab 395 ret = ore_check_io(ios, NULL);
06886a5a 396 }
b14f8ab2
BH
397 return ret;
398}
399
22ddc556
BH
400static void _clear_bio(struct bio *bio)
401{
402 struct bio_vec *bv;
403 unsigned i;
404
d74c6d51 405 bio_for_each_segment_all(bv, bio, i) {
22ddc556
BH
406 unsigned this_count = bv->bv_len;
407
408 if (likely(PAGE_SIZE == this_count))
409 clear_highpage(bv->bv_page);
410 else
411 zero_user(bv->bv_page, bv->bv_offset, this_count);
412 }
413}
414
4b46c9f5 415int ore_check_io(struct ore_io_state *ios, ore_on_dev_error on_dev_error)
b14f8ab2 416{
06886a5a
BH
417 enum osd_err_priority acumulated_osd_err = 0;
418 int acumulated_lin_err = 0;
419 int i;
b14f8ab2 420
06886a5a
BH
421 for (i = 0; i < ios->numdevs; i++) {
422 struct osd_sense_info osi;
4b46c9f5
BH
423 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
424 struct osd_request *or = per_dev->or;
22ddc556
BH
425 int ret;
426
427 if (unlikely(!or))
428 continue;
06886a5a 429
22ddc556 430 ret = osd_req_decode_sense(or, &osi);
06886a5a
BH
431 if (likely(!ret))
432 continue;
433
22ddc556
BH
434 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
435 /* start read offset passed endof file */
4b46c9f5 436 _clear_bio(per_dev->bio);
8ff660ab 437 ORE_DBGMSG("start read offset passed end of file "
22ddc556 438 "offset=0x%llx, length=0x%llx\n",
4b46c9f5
BH
439 _LLU(per_dev->offset),
440 _LLU(per_dev->length));
22ddc556
BH
441
442 continue; /* we recovered */
06886a5a
BH
443 }
444
4b46c9f5
BH
445 if (on_dev_error) {
446 u64 residual = ios->reading ?
447 or->in.residual : or->out.residual;
448 u64 offset = (ios->offset + ios->length) - residual;
ffefb8ea
BH
449 unsigned dev = per_dev->dev - ios->oc->first_dev;
450 struct ore_dev *od = ios->oc->ods[dev];
4b46c9f5 451
ffefb8ea 452 on_dev_error(ios, od, dev, osi.osd_err_pri,
4b46c9f5
BH
453 offset, residual);
454 }
06886a5a
BH
455 if (osi.osd_err_pri >= acumulated_osd_err) {
456 acumulated_osd_err = osi.osd_err_pri;
457 acumulated_lin_err = ret;
458 }
459 }
460
06886a5a
BH
461 return acumulated_lin_err;
462}
cf283ade 463EXPORT_SYMBOL(ore_check_io);
06886a5a 464
b367e78b
BH
465/*
466 * L - logical offset into the file
467 *
a1fec1db
BH
468 * D - number of Data devices
469 * D = group_width - parity
b367e78b 470 *
a1fec1db
BH
471 * U - The number of bytes in a stripe within a group
472 * U = stripe_unit * D
b367e78b 473 *
50a76fd3
BH
474 * T - The number of bytes striped within a group of component objects
475 * (before advancing to the next group)
a1fec1db 476 * T = U * group_depth
50a76fd3
BH
477 *
478 * S - The number of bytes striped across all component objects
479 * before the pattern repeats
a1fec1db 480 * S = T * group_count
50a76fd3 481 *
a1fec1db 482 * M - The "major" (i.e., across all components) cycle number
50a76fd3
BH
483 * M = L / S
484 *
a1fec1db 485 * G - Counts the groups from the beginning of the major cycle
50a76fd3
BH
486 * G = (L - (M * S)) / T [or (L % S) / T]
487 *
488 * H - The byte offset within the group
50a76fd3
BH
489 * H = (L - (M * S)) % T [or (L % S) % T]
490 *
491 * N - The "minor" (i.e., across the group) stripe number
50a76fd3 492 * N = H / U
b367e78b
BH
493 *
494 * C - The component index coresponding to L
495 *
a1fec1db
BH
496 * C = (H - (N * U)) / stripe_unit + G * D
497 * [or (L % U) / stripe_unit + G * D]
b367e78b
BH
498 *
499 * O - The component offset coresponding to L
50a76fd3 500 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
a1fec1db
BH
501 *
502 * LCMdP – Parity cycle: Lowest Common Multiple of group_width, parity
503 * divide by parity
504 * LCMdP = lcm(group_width, parity) / parity
505 *
506 * R - The parity Rotation stripe
507 * (Note parity cycle always starts at a group's boundary)
508 * R = N % LCMdP
509 *
510 * I = the first parity device index
511 * I = (group_width + group_width - R*parity - parity) % group_width
512 *
513 * Craid - The component index Rotated
514 * Craid = (group_width + C - R*parity) % group_width
515 * (We add the group_width to avoid negative numbers modulo math)
b367e78b 516 */
611d7a5d 517void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
a1fec1db 518 u64 length, struct ore_striping_info *si)
5d952b83 519{
16f75bb3
BH
520 u32 stripe_unit = layout->stripe_unit;
521 u32 group_width = layout->group_width;
522 u64 group_depth = layout->group_depth;
a1fec1db 523 u32 parity = layout->parity;
50a76fd3 524
a1fec1db
BH
525 u32 D = group_width - parity;
526 u32 U = D * stripe_unit;
50a76fd3 527 u64 T = U * group_depth;
16f75bb3 528 u64 S = T * layout->group_count;
50a76fd3
BH
529 u64 M = div64_u64(file_offset, S);
530
531 /*
532 G = (L - (M * S)) / T
533 H = (L - (M * S)) % T
534 */
535 u64 LmodS = file_offset - M * S;
536 u32 G = div64_u64(LmodS, T);
537 u64 H = LmodS - G * T;
538
539 u32 N = div_u64(H, U);
aad560b7 540 u32 Nlast;
50a76fd3
BH
541
542 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
a1fec1db 543 u32 C = (u32)(H - (N * U)) / stripe_unit + G * group_width;
b367e78b 544
50a76fd3 545 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
5d952b83 546
50a76fd3
BH
547 si->obj_offset = si->unit_off + (N * stripe_unit) +
548 (M * group_depth * stripe_unit);
549
a1fec1db
BH
550 if (parity) {
551 u32 LCMdP = lcm(group_width, parity) / parity;
552 /* R = N % LCMdP; */
553 u32 RxP = (N % LCMdP) * parity;
554 u32 first_dev = C - C % group_width;
555
556 si->par_dev = (group_width + group_width - parity - RxP) %
557 group_width + first_dev;
558 si->dev = (group_width + C - RxP) % group_width + first_dev;
559 si->bytes_in_stripe = U;
560 si->first_stripe_start = M * S + G * T + N * U;
561 } else {
562 /* Make the math correct see _prepare_one_group */
563 si->par_dev = group_width;
564 si->dev = C;
565 }
566
567 si->dev *= layout->mirrors_p1;
568 si->par_dev *= layout->mirrors_p1;
569 si->offset = file_offset;
570 si->length = T - H;
571 if (si->length > length)
572 si->length = length;
aad560b7
BH
573
574 Nlast = div_u64(H + si->length + U - 1, U);
575 si->maxdevUnits = Nlast - N;
576
16f75bb3 577 si->M = M;
5d952b83 578}
611d7a5d 579EXPORT_SYMBOL(ore_calc_stripe_info);
5d952b83 580
a1fec1db
BH
581int _ore_add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
582 unsigned pgbase, struct page **pages,
583 struct ore_per_dev_state *per_dev, int cur_len)
5d952b83 584{
86093aaf 585 unsigned pg = *cur_pg;
5d952b83 586 struct request_queue *q =
9e9db456 587 osd_request_queue(_ios_od(ios, per_dev->dev));
bbf9a31b
BH
588 unsigned len = cur_len;
589 int ret;
5d952b83
BH
590
591 if (per_dev->bio == NULL) {
aad560b7
BH
592 unsigned bio_size;
593
594 if (!ios->reading) {
595 bio_size = ios->si.maxdevUnits;
596 } else {
597 bio_size = (ios->si.maxdevUnits + 1) *
598 (ios->layout->group_width - ios->layout->parity) /
599 ios->layout->group_width;
600 }
601 bio_size *= (ios->layout->stripe_unit / PAGE_SIZE);
5d952b83
BH
602
603 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
604 if (unlikely(!per_dev->bio)) {
8ff660ab 605 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
5d952b83 606 bio_size);
bbf9a31b
BH
607 ret = -ENOMEM;
608 goto out;
5d952b83
BH
609 }
610 }
611
612 while (cur_len > 0) {
86093aaf
BH
613 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
614 unsigned added_len;
5d952b83 615
86093aaf 616 cur_len -= pglen;
5d952b83 617
a1fec1db 618 added_len = bio_add_pc_page(q, per_dev->bio, pages[pg],
86093aaf 619 pglen, pgbase);
bbf9a31b 620 if (unlikely(pglen != added_len)) {
aad560b7
BH
621 /* If bi_vcnt == bi_max then this is a SW BUG */
622 ORE_DBGMSG("Failed bio_add_pc_page bi_vcnt=0x%x "
623 "bi_max=0x%x BIO_MAX=0x%x cur_len=0x%x\n",
624 per_dev->bio->bi_vcnt,
625 per_dev->bio->bi_max_vecs,
626 BIO_MAX_PAGES_KMALLOC, cur_len);
bbf9a31b
BH
627 ret = -ENOMEM;
628 goto out;
629 }
769ba8d9
BH
630 _add_stripe_page(ios->sp2d, &ios->si, pages[pg]);
631
86093aaf
BH
632 pgbase = 0;
633 ++pg;
5d952b83
BH
634 }
635 BUG_ON(cur_len);
636
bbf9a31b 637 per_dev->length += len;
86093aaf 638 *cur_pg = pg;
bbf9a31b
BH
639 ret = 0;
640out: /* we fail the complete unit on an error eg don't advance
641 * per_dev->length and cur_pg. This means that we might have a bigger
642 * bio than the CDB requested length (per_dev->length). That's fine
643 * only the oposite is fatal.
644 */
645 return ret;
5d952b83
BH
646}
647
98260754 648static int _prepare_for_striping(struct ore_io_state *ios)
5d952b83 649{
98260754 650 struct ore_striping_info *si = &ios->si;
5d952b83 651 unsigned stripe_unit = ios->layout->stripe_unit;
b367e78b 652 unsigned mirrors_p1 = ios->layout->mirrors_p1;
a1fec1db
BH
653 unsigned group_width = ios->layout->group_width;
654 unsigned devs_in_group = group_width * mirrors_p1;
b367e78b 655 unsigned dev = si->dev;
50a76fd3 656 unsigned first_dev = dev - (dev % devs_in_group);
a1fec1db 657 unsigned dev_order;
50a76fd3 658 unsigned cur_pg = ios->pages_consumed;
98260754 659 u64 length = ios->length;
86093aaf 660 int ret = 0;
5d952b83 661
98260754 662 if (!ios->pages) {
98260754
BH
663 ios->numdevs = ios->layout->mirrors_p1;
664 return 0;
665 }
666
a1fec1db
BH
667 BUG_ON(length > si->length);
668
669 dev_order = _dev_order(devs_in_group, mirrors_p1, si->par_dev, dev);
670 si->cur_comp = dev_order;
769ba8d9 671 si->cur_pg = si->unit_off / PAGE_SIZE;
98260754 672
5d952b83 673 while (length) {
b916c5cd
BH
674 unsigned comp = dev - first_dev;
675 struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
b367e78b 676 unsigned cur_len, page_off = 0;
5d952b83
BH
677
678 if (!per_dev->length) {
b367e78b 679 per_dev->dev = dev;
a1fec1db
BH
680 if (dev == si->dev) {
681 WARN_ON(dev == si->par_dev);
b367e78b
BH
682 per_dev->offset = si->obj_offset;
683 cur_len = stripe_unit - si->unit_off;
684 page_off = si->unit_off & ~PAGE_MASK;
685 BUG_ON(page_off && (page_off != ios->pgbase));
a1fec1db
BH
686 } else {
687 if (si->cur_comp > dev_order)
688 per_dev->offset =
689 si->obj_offset - si->unit_off;
690 else /* si->cur_comp < dev_order */
691 per_dev->offset =
692 si->obj_offset + stripe_unit -
693 si->unit_off;
b367e78b
BH
694 cur_len = stripe_unit;
695 }
5d952b83 696 } else {
b367e78b 697 cur_len = stripe_unit;
5d952b83 698 }
b367e78b
BH
699 if (cur_len >= length)
700 cur_len = length;
5d952b83 701
a1fec1db
BH
702 ret = _ore_add_stripe_unit(ios, &cur_pg, page_off, ios->pages,
703 per_dev, cur_len);
5d952b83
BH
704 if (unlikely(ret))
705 goto out;
706
6e31609b
BH
707 dev += mirrors_p1;
708 dev = (dev % devs_in_group) + first_dev;
5d952b83
BH
709
710 length -= cur_len;
a1fec1db
BH
711
712 si->cur_comp = (si->cur_comp + 1) % group_width;
769ba8d9
BH
713 if (unlikely((dev == si->par_dev) || (!length && ios->sp2d))) {
714 if (!length && ios->sp2d) {
a1fec1db
BH
715 /* If we are writing and this is the very last
716 * stripe. then operate on parity dev.
717 */
718 dev = si->par_dev;
769ba8d9
BH
719 }
720 if (ios->sp2d)
a1fec1db
BH
721 /* In writes cur_len just means if it's the
722 * last one. See _ore_add_parity_unit.
723 */
724 cur_len = length;
725 per_dev = &ios->per_dev[dev - first_dev];
726 if (!per_dev->length) {
727 /* Only/always the parity unit of the first
728 * stripe will be empty. So this is a chance to
729 * initialize the per_dev info.
730 */
731 per_dev->dev = dev;
732 per_dev->offset = si->obj_offset - si->unit_off;
733 }
734
735 ret = _ore_add_parity_unit(ios, si, per_dev, cur_len);
736 if (unlikely(ret))
737 goto out;
738
739 /* Rotate next par_dev backwards with wraping */
740 si->par_dev = (devs_in_group + si->par_dev -
741 ios->layout->parity * mirrors_p1) %
742 devs_in_group + first_dev;
743 /* Next stripe, start fresh */
744 si->cur_comp = 0;
769ba8d9 745 si->cur_pg = 0;
a1fec1db 746 }
5d952b83
BH
747 }
748out:
b916c5cd 749 ios->numdevs = devs_in_group;
50a76fd3 750 ios->pages_consumed = cur_pg;
62b62ad8 751 return ret;
5d952b83
BH
752}
753
8ff660ab 754int ore_create(struct ore_io_state *ios)
06886a5a
BH
755{
756 int i, ret;
757
5bf696da 758 for (i = 0; i < ios->oc->numdevs; i++) {
06886a5a
BH
759 struct osd_request *or;
760
9e9db456 761 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
06886a5a 762 if (unlikely(!or)) {
8ff660ab 763 ORE_ERR("%s: osd_start_request failed\n", __func__);
06886a5a
BH
764 ret = -ENOMEM;
765 goto out;
766 }
767 ios->per_dev[i].or = or;
768 ios->numdevs++;
769
9e9db456 770 osd_req_create_object(or, _ios_obj(ios, i));
06886a5a 771 }
8ff660ab 772 ret = ore_io_execute(ios);
06886a5a
BH
773
774out:
775 return ret;
776}
cf283ade 777EXPORT_SYMBOL(ore_create);
06886a5a 778
8ff660ab 779int ore_remove(struct ore_io_state *ios)
06886a5a
BH
780{
781 int i, ret;
782
5bf696da 783 for (i = 0; i < ios->oc->numdevs; i++) {
06886a5a
BH
784 struct osd_request *or;
785
9e9db456 786 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
06886a5a 787 if (unlikely(!or)) {
8ff660ab 788 ORE_ERR("%s: osd_start_request failed\n", __func__);
06886a5a
BH
789 ret = -ENOMEM;
790 goto out;
791 }
792 ios->per_dev[i].or = or;
793 ios->numdevs++;
794
9e9db456 795 osd_req_remove_object(or, _ios_obj(ios, i));
06886a5a 796 }
8ff660ab 797 ret = ore_io_execute(ios);
06886a5a
BH
798
799out:
800 return ret;
801}
cf283ade 802EXPORT_SYMBOL(ore_remove);
06886a5a 803
8ff660ab 804static int _write_mirror(struct ore_io_state *ios, int cur_comp)
06886a5a 805{
8ff660ab 806 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
5d952b83
BH
807 unsigned dev = ios->per_dev[cur_comp].dev;
808 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
809 int ret = 0;
06886a5a 810
50a76fd3
BH
811 if (ios->pages && !master_dev->length)
812 return 0; /* Just an empty slot */
813
5d952b83 814 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
8ff660ab 815 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
06886a5a
BH
816 struct osd_request *or;
817
9e9db456 818 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
06886a5a 819 if (unlikely(!or)) {
8ff660ab 820 ORE_ERR("%s: osd_start_request failed\n", __func__);
06886a5a
BH
821 ret = -ENOMEM;
822 goto out;
823 }
5d952b83 824 per_dev->or = or;
06886a5a 825
86093aaf 826 if (ios->pages) {
06886a5a
BH
827 struct bio *bio;
828
5d952b83 829 if (per_dev != master_dev) {
bf800ef1
KO
830 bio = bio_clone_kmalloc(master_dev->bio,
831 GFP_KERNEL);
04dc1e88 832 if (unlikely(!bio)) {
8ff660ab 833 ORE_DBGMSG(
426d3107 834 "Failed to allocate BIO size=%u\n",
5d952b83 835 master_dev->bio->bi_max_vecs);
04dc1e88
BH
836 ret = -ENOMEM;
837 goto out;
838 }
839
04dc1e88
BH
840 bio->bi_bdev = NULL;
841 bio->bi_next = NULL;
6851a5e5 842 per_dev->offset = master_dev->offset;
5d952b83
BH
843 per_dev->length = master_dev->length;
844 per_dev->bio = bio;
845 per_dev->dev = dev;
04dc1e88 846 } else {
5d952b83
BH
847 bio = master_dev->bio;
848 /* FIXME: bio_set_dir() */
7b6d91da 849 bio->bi_rw |= REQ_WRITE;
04dc1e88 850 }
06886a5a 851
9e62bb44
BH
852 osd_req_write(or, _ios_obj(ios, cur_comp),
853 per_dev->offset, bio, per_dev->length);
8ff660ab 854 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
34ce4e7c 855 "length=0x%llx dev=%d\n",
9e62bb44 856 _LLU(_ios_obj(ios, cur_comp)->id),
9e9db456 857 _LLU(per_dev->offset),
5d952b83 858 _LLU(per_dev->length), dev);
06886a5a 859 } else if (ios->kern_buff) {
6851a5e5
BH
860 per_dev->offset = ios->si.obj_offset;
861 per_dev->dev = ios->si.dev + dev;
862
863 /* no cross device without page array */
864 BUG_ON((ios->layout->group_width > 1) &&
865 (ios->si.unit_off + ios->length >
866 ios->layout->stripe_unit));
867
9e62bb44 868 ret = osd_req_write_kern(or, _ios_obj(ios, cur_comp),
9e9db456
BH
869 per_dev->offset,
870 ios->kern_buff, ios->length);
5d952b83
BH
871 if (unlikely(ret))
872 goto out;
8ff660ab 873 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
34ce4e7c 874 "length=0x%llx dev=%d\n",
9e62bb44 875 _LLU(_ios_obj(ios, cur_comp)->id),
9e9db456 876 _LLU(per_dev->offset),
6851a5e5 877 _LLU(ios->length), per_dev->dev);
06886a5a 878 } else {
9e62bb44 879 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
8ff660ab 880 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
9e62bb44 881 _LLU(_ios_obj(ios, cur_comp)->id),
9e9db456 882 ios->out_attr_len, dev);
06886a5a
BH
883 }
884
885 if (ios->out_attr)
886 osd_req_add_set_attr_list(or, ios->out_attr,
887 ios->out_attr_len);
888
889 if (ios->in_attr)
890 osd_req_add_get_attr_list(or, ios->in_attr,
891 ios->in_attr_len);
b14f8ab2 892 }
06886a5a
BH
893
894out:
895 return ret;
896}
897
8ff660ab 898int ore_write(struct ore_io_state *ios)
5d952b83
BH
899{
900 int i;
901 int ret;
902
769ba8d9
BH
903 if (unlikely(ios->sp2d && !ios->r4w)) {
904 /* A library is attempting a RAID-write without providing
905 * a pages lock interface.
906 */
907 WARN_ON_ONCE(1);
908 return -ENOTSUPP;
909 }
910
5d952b83
BH
911 ret = _prepare_for_striping(ios);
912 if (unlikely(ret))
913 return ret;
914
915 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
8ff660ab 916 ret = _write_mirror(ios, i);
5d952b83
BH
917 if (unlikely(ret))
918 return ret;
919 }
920
8ff660ab 921 ret = ore_io_execute(ios);
5d952b83
BH
922 return ret;
923}
cf283ade 924EXPORT_SYMBOL(ore_write);
5d952b83 925
769ba8d9 926int _ore_read_mirror(struct ore_io_state *ios, unsigned cur_comp)
06886a5a 927{
46f4d973 928 struct osd_request *or;
8ff660ab 929 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
9e9db456
BH
930 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
931 unsigned first_dev = (unsigned)obj->id;
06886a5a 932
50a76fd3
BH
933 if (ios->pages && !per_dev->length)
934 return 0; /* Just an empty slot */
935
5d952b83 936 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
9e9db456 937 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
46f4d973 938 if (unlikely(!or)) {
8ff660ab 939 ORE_ERR("%s: osd_start_request failed\n", __func__);
46f4d973
BH
940 return -ENOMEM;
941 }
942 per_dev->or = or;
46f4d973 943
86093aaf 944 if (ios->pages) {
a1fec1db
BH
945 if (per_dev->cur_sg) {
946 /* finalize the last sg_entry */
947 _ore_add_sg_seg(per_dev, 0, false);
948 if (unlikely(!per_dev->cur_sg))
949 return 0; /* Skip parity only device */
950
951 osd_req_read_sg(or, obj, per_dev->bio,
952 per_dev->sglist, per_dev->cur_sg);
953 } else {
954 /* The no raid case */
955 osd_req_read(or, obj, per_dev->offset,
956 per_dev->bio, per_dev->length);
957 }
958
8ff660ab 959 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
a1fec1db 960 " dev=%d sg_len=%d\n", _LLU(obj->id),
5d952b83 961 _LLU(per_dev->offset), _LLU(per_dev->length),
a1fec1db 962 first_dev, per_dev->cur_sg);
46f4d973 963 } else {
6851a5e5
BH
964 BUG_ON(ios->kern_buff);
965
9e9db456 966 osd_req_get_attributes(or, obj);
8ff660ab 967 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
9e9db456
BH
968 _LLU(obj->id),
969 ios->in_attr_len, first_dev);
46f4d973 970 }
46f4d973
BH
971 if (ios->out_attr)
972 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
b14f8ab2 973
46f4d973
BH
974 if (ios->in_attr)
975 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
b14f8ab2 976
5d952b83
BH
977 return 0;
978}
979
8ff660ab 980int ore_read(struct ore_io_state *ios)
5d952b83
BH
981{
982 int i;
983 int ret;
984
985 ret = _prepare_for_striping(ios);
986 if (unlikely(ret))
987 return ret;
988
989 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
769ba8d9 990 ret = _ore_read_mirror(ios, i);
5d952b83
BH
991 if (unlikely(ret))
992 return ret;
993 }
994
8ff660ab 995 ret = ore_io_execute(ios);
5d952b83 996 return ret;
b14f8ab2 997}
cf283ade 998EXPORT_SYMBOL(ore_read);
b14f8ab2 999
8ff660ab 1000int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
b14f8ab2
BH
1001{
1002 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
1003 void *iter = NULL;
1004 int nelem;
1005
1006 do {
1007 nelem = 1;
06886a5a
BH
1008 osd_req_decode_get_attr_list(ios->per_dev[0].or,
1009 &cur_attr, &nelem, &iter);
b14f8ab2
BH
1010 if ((cur_attr.attr_page == attr->attr_page) &&
1011 (cur_attr.attr_id == attr->attr_id)) {
1012 attr->len = cur_attr.len;
1013 attr->val_ptr = cur_attr.val_ptr;
1014 return 0;
1015 }
1016 } while (iter);
1017
1018 return -EIO;
1019}
cf283ade 1020EXPORT_SYMBOL(extract_attr_from_ios);
06886a5a 1021
8ff660ab 1022static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
5d952b83
BH
1023 struct osd_attr *attr)
1024{
1025 int last_comp = cur_comp + ios->layout->mirrors_p1;
1026
1027 for (; cur_comp < last_comp; ++cur_comp) {
8ff660ab 1028 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
5d952b83
BH
1029 struct osd_request *or;
1030
9e9db456 1031 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
5d952b83 1032 if (unlikely(!or)) {
8ff660ab 1033 ORE_ERR("%s: osd_start_request failed\n", __func__);
5d952b83
BH
1034 return -ENOMEM;
1035 }
1036 per_dev->or = or;
1037
9e9db456 1038 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
5d952b83
BH
1039 osd_req_add_set_attr_list(or, attr, 1);
1040 }
1041
1042 return 0;
1043}
1044
16f75bb3 1045struct _trunc_info {
eb507bc1 1046 struct ore_striping_info si;
16f75bb3
BH
1047 u64 prev_group_obj_off;
1048 u64 next_group_obj_off;
1049
1050 unsigned first_group_dev;
1051 unsigned nex_group_dev;
16f75bb3
BH
1052};
1053
1958c7c2
HS
1054static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
1055 struct _trunc_info *ti)
16f75bb3
BH
1056{
1057 unsigned stripe_unit = layout->stripe_unit;
1058
a1fec1db 1059 ore_calc_stripe_info(layout, file_offset, 0, &ti->si);
16f75bb3
BH
1060
1061 ti->prev_group_obj_off = ti->si.M * stripe_unit;
1062 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
1063
1064 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
1065 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
16f75bb3
BH
1066}
1067
5bf696da 1068int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
9e9db456 1069 u64 size)
06886a5a 1070{
8ff660ab 1071 struct ore_io_state *ios;
5d952b83
BH
1072 struct exofs_trunc_attr {
1073 struct osd_attr attr;
1074 __be64 newsize;
1075 } *size_attrs;
16f75bb3 1076 struct _trunc_info ti;
06886a5a
BH
1077 int i, ret;
1078
5bf696da 1079 ret = ore_get_io_state(layout, oc, &ios);
5d952b83
BH
1080 if (unlikely(ret))
1081 return ret;
1082
16f75bb3
BH
1083 _calc_trunk_info(ios->layout, size, &ti);
1084
b916c5cd 1085 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
5d952b83
BH
1086 GFP_KERNEL);
1087 if (unlikely(!size_attrs)) {
1088 ret = -ENOMEM;
1089 goto out;
1090 }
06886a5a 1091
5bf696da 1092 ios->numdevs = ios->oc->numdevs;
06886a5a 1093
b916c5cd 1094 for (i = 0; i < ios->numdevs; ++i) {
5d952b83
BH
1095 struct exofs_trunc_attr *size_attr = &size_attrs[i];
1096 u64 obj_size;
06886a5a 1097
16f75bb3
BH
1098 if (i < ti.first_group_dev)
1099 obj_size = ti.prev_group_obj_off;
1100 else if (i >= ti.nex_group_dev)
1101 obj_size = ti.next_group_obj_off;
1102 else if (i < ti.si.dev) /* dev within this group */
1103 obj_size = ti.si.obj_offset +
1104 ios->layout->stripe_unit - ti.si.unit_off;
1105 else if (i == ti.si.dev)
1106 obj_size = ti.si.obj_offset;
1107 else /* i > ti.dev */
1108 obj_size = ti.si.obj_offset - ti.si.unit_off;
06886a5a 1109
5d952b83
BH
1110 size_attr->newsize = cpu_to_be64(obj_size);
1111 size_attr->attr = g_attr_logical_length;
1112 size_attr->attr.val_ptr = &size_attr->newsize;
1113
aad560b7 1114 ORE_DBGMSG2("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
5bf696da 1115 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
5d952b83
BH
1116 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
1117 &size_attr->attr);
1118 if (unlikely(ret))
1119 goto out;
06886a5a 1120 }
8ff660ab 1121 ret = ore_io_execute(ios);
06886a5a
BH
1122
1123out:
5d952b83 1124 kfree(size_attrs);
8ff660ab 1125 ore_put_io_state(ios);
06886a5a
BH
1126 return ret;
1127}
cf283ade 1128EXPORT_SYMBOL(ore_truncate);
85e44df4
BH
1129
1130const struct osd_attr g_attr_logical_length = ATTR_DEF(
1131 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
cf283ade 1132EXPORT_SYMBOL(g_attr_logical_length);
This page took 0.517185 seconds and 5 git commands to generate.