fs: add module.h to files that were implicitly using it
[linux-2.6-block.git] / fs / exofs / ore.c
CommitLineData
b14f8ab2
BH
1/*
2 * Copyright (C) 2005, 2006
27d2e149 3 * Avishay Traeger (avishay@gmail.com)
b14f8ab2
BH
4 * Copyright (C) 2008, 2009
5 * Boaz Harrosh <bharrosh@panasas.com>
6 *
7 * This file is part of exofs.
8 *
9 * exofs is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation. Since it is based on ext2, and the only
12 * valid version of GPL for the Linux kernel is version 2, the only valid
13 * version of GPL for exofs is version 2.
14 *
15 * exofs is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with exofs; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
5a0e3ad6 25#include <linux/slab.h>
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) *
106 layout->group_width;
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;
269 sgs_per_dev = div_u64(num_raid_units, data_devs);
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;
b916c5cd 289 ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
a1fec1db
BH
290 if (layout->parity)
291 _ore_post_alloc_raid_stuff(ios);
b916c5cd 292 }
e1042ba0 293
06886a5a 294 return 0;
b14f8ab2 295}
cf283ade 296EXPORT_SYMBOL(ore_get_rw_state);
b14f8ab2 297
b916c5cd
BH
298/* Allocate an io_state for all the devices in the comps array
299 *
300 * This version of io_state allocation is used mostly by create/remove
301 * and trunc where we currently need all the devices. The only wastful
302 * bit is the read/write_attributes with no IO. Those sites should
303 * be converted to use ore_get_rw_state() with length=0
304 */
5bf696da 305int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
b916c5cd 306 struct ore_io_state **pios)
e1042ba0 307{
a1fec1db 308 return _ore_get_io_state(layout, oc, oc->numdevs, 0, 0, pios);
e1042ba0 309}
cf283ade 310EXPORT_SYMBOL(ore_get_io_state);
e1042ba0 311
8ff660ab 312void ore_put_io_state(struct ore_io_state *ios)
b14f8ab2 313{
06886a5a
BH
314 if (ios) {
315 unsigned i;
b14f8ab2 316
06886a5a 317 for (i = 0; i < ios->numdevs; i++) {
8ff660ab 318 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
06886a5a
BH
319
320 if (per_dev->or)
321 osd_end_request(per_dev->or);
322 if (per_dev->bio)
323 bio_put(per_dev->bio);
324 }
325
a1fec1db 326 _ore_free_raid_stuff(ios);
06886a5a 327 kfree(ios);
b14f8ab2 328 }
06886a5a 329}
cf283ade 330EXPORT_SYMBOL(ore_put_io_state);
b14f8ab2 331
8ff660ab 332static void _sync_done(struct ore_io_state *ios, void *p)
06886a5a
BH
333{
334 struct completion *waiting = p;
b14f8ab2 335
06886a5a
BH
336 complete(waiting);
337}
338
339static void _last_io(struct kref *kref)
340{
8ff660ab
BH
341 struct ore_io_state *ios = container_of(
342 kref, struct ore_io_state, kref);
06886a5a
BH
343
344 ios->done(ios, ios->private);
345}
346
347static void _done_io(struct osd_request *or, void *p)
348{
8ff660ab 349 struct ore_io_state *ios = p;
06886a5a
BH
350
351 kref_put(&ios->kref, _last_io);
352}
353
769ba8d9 354int ore_io_execute(struct ore_io_state *ios)
06886a5a
BH
355{
356 DECLARE_COMPLETION_ONSTACK(wait);
357 bool sync = (ios->done == NULL);
358 int i, ret;
359
360 if (sync) {
361 ios->done = _sync_done;
362 ios->private = &wait;
363 }
364
365 for (i = 0; i < ios->numdevs; i++) {
366 struct osd_request *or = ios->per_dev[i].or;
367 if (unlikely(!or))
368 continue;
369
9e9db456 370 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
06886a5a 371 if (unlikely(ret)) {
8ff660ab 372 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
06886a5a
BH
373 ret);
374 return ret;
375 }
376 }
377
378 kref_init(&ios->kref);
379
380 for (i = 0; i < ios->numdevs; i++) {
381 struct osd_request *or = ios->per_dev[i].or;
382 if (unlikely(!or))
383 continue;
384
385 kref_get(&ios->kref);
386 osd_execute_request_async(or, _done_io, ios);
387 }
388
389 kref_put(&ios->kref, _last_io);
390 ret = 0;
391
392 if (sync) {
393 wait_for_completion(&wait);
8ff660ab 394 ret = ore_check_io(ios, NULL);
06886a5a 395 }
b14f8ab2
BH
396 return ret;
397}
398
22ddc556
BH
399static void _clear_bio(struct bio *bio)
400{
401 struct bio_vec *bv;
402 unsigned i;
403
404 __bio_for_each_segment(bv, bio, i, 0) {
405 unsigned this_count = bv->bv_len;
406
407 if (likely(PAGE_SIZE == this_count))
408 clear_highpage(bv->bv_page);
409 else
410 zero_user(bv->bv_page, bv->bv_offset, this_count);
411 }
412}
413
4b46c9f5 414int ore_check_io(struct ore_io_state *ios, ore_on_dev_error on_dev_error)
b14f8ab2 415{
06886a5a
BH
416 enum osd_err_priority acumulated_osd_err = 0;
417 int acumulated_lin_err = 0;
418 int i;
b14f8ab2 419
06886a5a
BH
420 for (i = 0; i < ios->numdevs; i++) {
421 struct osd_sense_info osi;
4b46c9f5
BH
422 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
423 struct osd_request *or = per_dev->or;
22ddc556
BH
424 int ret;
425
426 if (unlikely(!or))
427 continue;
06886a5a 428
22ddc556 429 ret = osd_req_decode_sense(or, &osi);
06886a5a
BH
430 if (likely(!ret))
431 continue;
432
22ddc556
BH
433 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
434 /* start read offset passed endof file */
4b46c9f5 435 _clear_bio(per_dev->bio);
8ff660ab 436 ORE_DBGMSG("start read offset passed end of file "
22ddc556 437 "offset=0x%llx, length=0x%llx\n",
4b46c9f5
BH
438 _LLU(per_dev->offset),
439 _LLU(per_dev->length));
22ddc556
BH
440
441 continue; /* we recovered */
06886a5a
BH
442 }
443
4b46c9f5
BH
444 if (on_dev_error) {
445 u64 residual = ios->reading ?
446 or->in.residual : or->out.residual;
447 u64 offset = (ios->offset + ios->length) - residual;
448 struct ore_dev *od = ios->oc->ods[
449 per_dev->dev - ios->oc->first_dev];
450
451 on_dev_error(ios, od, per_dev->dev, osi.osd_err_pri,
452 offset, residual);
453 }
06886a5a
BH
454 if (osi.osd_err_pri >= acumulated_osd_err) {
455 acumulated_osd_err = osi.osd_err_pri;
456 acumulated_lin_err = ret;
457 }
458 }
459
06886a5a
BH
460 return acumulated_lin_err;
461}
cf283ade 462EXPORT_SYMBOL(ore_check_io);
06886a5a 463
b367e78b
BH
464/*
465 * L - logical offset into the file
466 *
a1fec1db
BH
467 * D - number of Data devices
468 * D = group_width - parity
b367e78b 469 *
a1fec1db
BH
470 * U - The number of bytes in a stripe within a group
471 * U = stripe_unit * D
b367e78b 472 *
50a76fd3
BH
473 * T - The number of bytes striped within a group of component objects
474 * (before advancing to the next group)
a1fec1db 475 * T = U * group_depth
50a76fd3
BH
476 *
477 * S - The number of bytes striped across all component objects
478 * before the pattern repeats
a1fec1db 479 * S = T * group_count
50a76fd3 480 *
a1fec1db 481 * M - The "major" (i.e., across all components) cycle number
50a76fd3
BH
482 * M = L / S
483 *
a1fec1db 484 * G - Counts the groups from the beginning of the major cycle
50a76fd3
BH
485 * G = (L - (M * S)) / T [or (L % S) / T]
486 *
487 * H - The byte offset within the group
50a76fd3
BH
488 * H = (L - (M * S)) % T [or (L % S) % T]
489 *
490 * N - The "minor" (i.e., across the group) stripe number
50a76fd3 491 * N = H / U
b367e78b
BH
492 *
493 * C - The component index coresponding to L
494 *
a1fec1db
BH
495 * C = (H - (N * U)) / stripe_unit + G * D
496 * [or (L % U) / stripe_unit + G * D]
b367e78b
BH
497 *
498 * O - The component offset coresponding to L
50a76fd3 499 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
a1fec1db
BH
500 *
501 * LCMdP – Parity cycle: Lowest Common Multiple of group_width, parity
502 * divide by parity
503 * LCMdP = lcm(group_width, parity) / parity
504 *
505 * R - The parity Rotation stripe
506 * (Note parity cycle always starts at a group's boundary)
507 * R = N % LCMdP
508 *
509 * I = the first parity device index
510 * I = (group_width + group_width - R*parity - parity) % group_width
511 *
512 * Craid - The component index Rotated
513 * Craid = (group_width + C - R*parity) % group_width
514 * (We add the group_width to avoid negative numbers modulo math)
b367e78b 515 */
611d7a5d 516void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
a1fec1db 517 u64 length, struct ore_striping_info *si)
5d952b83 518{
16f75bb3
BH
519 u32 stripe_unit = layout->stripe_unit;
520 u32 group_width = layout->group_width;
521 u64 group_depth = layout->group_depth;
a1fec1db 522 u32 parity = layout->parity;
50a76fd3 523
a1fec1db
BH
524 u32 D = group_width - parity;
525 u32 U = D * stripe_unit;
50a76fd3 526 u64 T = U * group_depth;
16f75bb3 527 u64 S = T * layout->group_count;
50a76fd3
BH
528 u64 M = div64_u64(file_offset, S);
529
530 /*
531 G = (L - (M * S)) / T
532 H = (L - (M * S)) % T
533 */
534 u64 LmodS = file_offset - M * S;
535 u32 G = div64_u64(LmodS, T);
536 u64 H = LmodS - G * T;
537
538 u32 N = div_u64(H, U);
539
540 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
a1fec1db 541 u32 C = (u32)(H - (N * U)) / stripe_unit + G * group_width;
b367e78b 542
50a76fd3 543 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
5d952b83 544
50a76fd3
BH
545 si->obj_offset = si->unit_off + (N * stripe_unit) +
546 (M * group_depth * stripe_unit);
547
a1fec1db
BH
548 if (parity) {
549 u32 LCMdP = lcm(group_width, parity) / parity;
550 /* R = N % LCMdP; */
551 u32 RxP = (N % LCMdP) * parity;
552 u32 first_dev = C - C % group_width;
553
554 si->par_dev = (group_width + group_width - parity - RxP) %
555 group_width + first_dev;
556 si->dev = (group_width + C - RxP) % group_width + first_dev;
557 si->bytes_in_stripe = U;
558 si->first_stripe_start = M * S + G * T + N * U;
559 } else {
560 /* Make the math correct see _prepare_one_group */
561 si->par_dev = group_width;
562 si->dev = C;
563 }
564
565 si->dev *= layout->mirrors_p1;
566 si->par_dev *= layout->mirrors_p1;
567 si->offset = file_offset;
568 si->length = T - H;
569 if (si->length > length)
570 si->length = length;
16f75bb3 571 si->M = M;
5d952b83 572}
611d7a5d 573EXPORT_SYMBOL(ore_calc_stripe_info);
5d952b83 574
a1fec1db
BH
575int _ore_add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
576 unsigned pgbase, struct page **pages,
577 struct ore_per_dev_state *per_dev, int cur_len)
5d952b83 578{
86093aaf 579 unsigned pg = *cur_pg;
5d952b83 580 struct request_queue *q =
9e9db456 581 osd_request_queue(_ios_od(ios, per_dev->dev));
bbf9a31b
BH
582 unsigned len = cur_len;
583 int ret;
5d952b83
BH
584
585 if (per_dev->bio == NULL) {
586 unsigned pages_in_stripe = ios->layout->group_width *
587 (ios->layout->stripe_unit / PAGE_SIZE);
a1fec1db
BH
588 unsigned nr_pages = ios->nr_pages * ios->layout->group_width /
589 (ios->layout->group_width -
590 ios->layout->parity);
591 unsigned bio_size = (nr_pages + pages_in_stripe) /
592 ios->layout->group_width;
5d952b83
BH
593
594 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
595 if (unlikely(!per_dev->bio)) {
8ff660ab 596 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
5d952b83 597 bio_size);
bbf9a31b
BH
598 ret = -ENOMEM;
599 goto out;
5d952b83
BH
600 }
601 }
602
603 while (cur_len > 0) {
86093aaf
BH
604 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
605 unsigned added_len;
5d952b83 606
86093aaf 607 cur_len -= pglen;
5d952b83 608
a1fec1db 609 added_len = bio_add_pc_page(q, per_dev->bio, pages[pg],
86093aaf 610 pglen, pgbase);
bbf9a31b 611 if (unlikely(pglen != added_len)) {
a1fec1db
BH
612 ORE_DBGMSG("Failed bio_add_pc_page bi_vcnt=%u\n",
613 per_dev->bio->bi_vcnt);
bbf9a31b
BH
614 ret = -ENOMEM;
615 goto out;
616 }
769ba8d9
BH
617 _add_stripe_page(ios->sp2d, &ios->si, pages[pg]);
618
86093aaf
BH
619 pgbase = 0;
620 ++pg;
5d952b83
BH
621 }
622 BUG_ON(cur_len);
623
bbf9a31b 624 per_dev->length += len;
86093aaf 625 *cur_pg = pg;
bbf9a31b
BH
626 ret = 0;
627out: /* we fail the complete unit on an error eg don't advance
628 * per_dev->length and cur_pg. This means that we might have a bigger
629 * bio than the CDB requested length (per_dev->length). That's fine
630 * only the oposite is fatal.
631 */
632 return ret;
5d952b83
BH
633}
634
98260754 635static int _prepare_for_striping(struct ore_io_state *ios)
5d952b83 636{
98260754 637 struct ore_striping_info *si = &ios->si;
5d952b83 638 unsigned stripe_unit = ios->layout->stripe_unit;
b367e78b 639 unsigned mirrors_p1 = ios->layout->mirrors_p1;
a1fec1db
BH
640 unsigned group_width = ios->layout->group_width;
641 unsigned devs_in_group = group_width * mirrors_p1;
b367e78b 642 unsigned dev = si->dev;
50a76fd3 643 unsigned first_dev = dev - (dev % devs_in_group);
a1fec1db 644 unsigned dev_order;
50a76fd3 645 unsigned cur_pg = ios->pages_consumed;
98260754 646 u64 length = ios->length;
86093aaf 647 int ret = 0;
5d952b83 648
98260754 649 if (!ios->pages) {
98260754
BH
650 ios->numdevs = ios->layout->mirrors_p1;
651 return 0;
652 }
653
a1fec1db
BH
654 BUG_ON(length > si->length);
655
656 dev_order = _dev_order(devs_in_group, mirrors_p1, si->par_dev, dev);
657 si->cur_comp = dev_order;
769ba8d9 658 si->cur_pg = si->unit_off / PAGE_SIZE;
98260754 659
5d952b83 660 while (length) {
b916c5cd
BH
661 unsigned comp = dev - first_dev;
662 struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
b367e78b 663 unsigned cur_len, page_off = 0;
5d952b83
BH
664
665 if (!per_dev->length) {
b367e78b 666 per_dev->dev = dev;
a1fec1db
BH
667 if (dev == si->dev) {
668 WARN_ON(dev == si->par_dev);
b367e78b
BH
669 per_dev->offset = si->obj_offset;
670 cur_len = stripe_unit - si->unit_off;
671 page_off = si->unit_off & ~PAGE_MASK;
672 BUG_ON(page_off && (page_off != ios->pgbase));
a1fec1db
BH
673 } else {
674 if (si->cur_comp > dev_order)
675 per_dev->offset =
676 si->obj_offset - si->unit_off;
677 else /* si->cur_comp < dev_order */
678 per_dev->offset =
679 si->obj_offset + stripe_unit -
680 si->unit_off;
b367e78b
BH
681 cur_len = stripe_unit;
682 }
5d952b83 683 } else {
b367e78b 684 cur_len = stripe_unit;
5d952b83 685 }
b367e78b
BH
686 if (cur_len >= length)
687 cur_len = length;
5d952b83 688
a1fec1db
BH
689 ret = _ore_add_stripe_unit(ios, &cur_pg, page_off, ios->pages,
690 per_dev, cur_len);
5d952b83
BH
691 if (unlikely(ret))
692 goto out;
693
6e31609b
BH
694 dev += mirrors_p1;
695 dev = (dev % devs_in_group) + first_dev;
5d952b83
BH
696
697 length -= cur_len;
a1fec1db
BH
698
699 si->cur_comp = (si->cur_comp + 1) % group_width;
769ba8d9
BH
700 if (unlikely((dev == si->par_dev) || (!length && ios->sp2d))) {
701 if (!length && ios->sp2d) {
a1fec1db
BH
702 /* If we are writing and this is the very last
703 * stripe. then operate on parity dev.
704 */
705 dev = si->par_dev;
769ba8d9
BH
706 }
707 if (ios->sp2d)
a1fec1db
BH
708 /* In writes cur_len just means if it's the
709 * last one. See _ore_add_parity_unit.
710 */
711 cur_len = length;
712 per_dev = &ios->per_dev[dev - first_dev];
713 if (!per_dev->length) {
714 /* Only/always the parity unit of the first
715 * stripe will be empty. So this is a chance to
716 * initialize the per_dev info.
717 */
718 per_dev->dev = dev;
719 per_dev->offset = si->obj_offset - si->unit_off;
720 }
721
722 ret = _ore_add_parity_unit(ios, si, per_dev, cur_len);
723 if (unlikely(ret))
724 goto out;
725
726 /* Rotate next par_dev backwards with wraping */
727 si->par_dev = (devs_in_group + si->par_dev -
728 ios->layout->parity * mirrors_p1) %
729 devs_in_group + first_dev;
730 /* Next stripe, start fresh */
731 si->cur_comp = 0;
769ba8d9 732 si->cur_pg = 0;
a1fec1db 733 }
5d952b83
BH
734 }
735out:
b916c5cd 736 ios->numdevs = devs_in_group;
50a76fd3 737 ios->pages_consumed = cur_pg;
bbf9a31b
BH
738 if (unlikely(ret)) {
739 if (length == ios->length)
740 return ret;
741 else
742 ios->length -= length;
743 }
744 return 0;
5d952b83
BH
745}
746
8ff660ab 747int ore_create(struct ore_io_state *ios)
06886a5a
BH
748{
749 int i, ret;
750
5bf696da 751 for (i = 0; i < ios->oc->numdevs; i++) {
06886a5a
BH
752 struct osd_request *or;
753
9e9db456 754 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
06886a5a 755 if (unlikely(!or)) {
8ff660ab 756 ORE_ERR("%s: osd_start_request failed\n", __func__);
06886a5a
BH
757 ret = -ENOMEM;
758 goto out;
759 }
760 ios->per_dev[i].or = or;
761 ios->numdevs++;
762
9e9db456 763 osd_req_create_object(or, _ios_obj(ios, i));
06886a5a 764 }
8ff660ab 765 ret = ore_io_execute(ios);
06886a5a
BH
766
767out:
768 return ret;
769}
cf283ade 770EXPORT_SYMBOL(ore_create);
06886a5a 771
8ff660ab 772int ore_remove(struct ore_io_state *ios)
06886a5a
BH
773{
774 int i, ret;
775
5bf696da 776 for (i = 0; i < ios->oc->numdevs; i++) {
06886a5a
BH
777 struct osd_request *or;
778
9e9db456 779 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
06886a5a 780 if (unlikely(!or)) {
8ff660ab 781 ORE_ERR("%s: osd_start_request failed\n", __func__);
06886a5a
BH
782 ret = -ENOMEM;
783 goto out;
784 }
785 ios->per_dev[i].or = or;
786 ios->numdevs++;
787
9e9db456 788 osd_req_remove_object(or, _ios_obj(ios, i));
06886a5a 789 }
8ff660ab 790 ret = ore_io_execute(ios);
06886a5a
BH
791
792out:
793 return ret;
794}
cf283ade 795EXPORT_SYMBOL(ore_remove);
06886a5a 796
8ff660ab 797static int _write_mirror(struct ore_io_state *ios, int cur_comp)
06886a5a 798{
8ff660ab 799 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
5d952b83
BH
800 unsigned dev = ios->per_dev[cur_comp].dev;
801 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
802 int ret = 0;
06886a5a 803
50a76fd3
BH
804 if (ios->pages && !master_dev->length)
805 return 0; /* Just an empty slot */
806
5d952b83 807 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
8ff660ab 808 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
06886a5a
BH
809 struct osd_request *or;
810
9e9db456 811 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
06886a5a 812 if (unlikely(!or)) {
8ff660ab 813 ORE_ERR("%s: osd_start_request failed\n", __func__);
06886a5a
BH
814 ret = -ENOMEM;
815 goto out;
816 }
5d952b83 817 per_dev->or = or;
06886a5a 818
86093aaf 819 if (ios->pages) {
06886a5a
BH
820 struct bio *bio;
821
5d952b83 822 if (per_dev != master_dev) {
04dc1e88 823 bio = bio_kmalloc(GFP_KERNEL,
5d952b83 824 master_dev->bio->bi_max_vecs);
04dc1e88 825 if (unlikely(!bio)) {
8ff660ab 826 ORE_DBGMSG(
426d3107 827 "Failed to allocate BIO size=%u\n",
5d952b83 828 master_dev->bio->bi_max_vecs);
04dc1e88
BH
829 ret = -ENOMEM;
830 goto out;
831 }
832
5d952b83 833 __bio_clone(bio, master_dev->bio);
04dc1e88
BH
834 bio->bi_bdev = NULL;
835 bio->bi_next = NULL;
6851a5e5 836 per_dev->offset = master_dev->offset;
5d952b83
BH
837 per_dev->length = master_dev->length;
838 per_dev->bio = bio;
839 per_dev->dev = dev;
04dc1e88 840 } else {
5d952b83
BH
841 bio = master_dev->bio;
842 /* FIXME: bio_set_dir() */
7b6d91da 843 bio->bi_rw |= REQ_WRITE;
04dc1e88 844 }
06886a5a 845
9e9db456
BH
846 osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
847 bio, per_dev->length);
8ff660ab 848 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
34ce4e7c 849 "length=0x%llx dev=%d\n",
9e9db456
BH
850 _LLU(_ios_obj(ios, dev)->id),
851 _LLU(per_dev->offset),
5d952b83 852 _LLU(per_dev->length), dev);
06886a5a 853 } else if (ios->kern_buff) {
6851a5e5
BH
854 per_dev->offset = ios->si.obj_offset;
855 per_dev->dev = ios->si.dev + dev;
856
857 /* no cross device without page array */
858 BUG_ON((ios->layout->group_width > 1) &&
859 (ios->si.unit_off + ios->length >
860 ios->layout->stripe_unit));
861
862 ret = osd_req_write_kern(or, _ios_obj(ios, per_dev->dev),
9e9db456
BH
863 per_dev->offset,
864 ios->kern_buff, ios->length);
5d952b83
BH
865 if (unlikely(ret))
866 goto out;
8ff660ab 867 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
34ce4e7c 868 "length=0x%llx dev=%d\n",
9e9db456
BH
869 _LLU(_ios_obj(ios, dev)->id),
870 _LLU(per_dev->offset),
6851a5e5 871 _LLU(ios->length), per_dev->dev);
06886a5a 872 } else {
9e9db456 873 osd_req_set_attributes(or, _ios_obj(ios, dev));
8ff660ab 874 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
9e9db456
BH
875 _LLU(_ios_obj(ios, dev)->id),
876 ios->out_attr_len, dev);
06886a5a
BH
877 }
878
879 if (ios->out_attr)
880 osd_req_add_set_attr_list(or, ios->out_attr,
881 ios->out_attr_len);
882
883 if (ios->in_attr)
884 osd_req_add_get_attr_list(or, ios->in_attr,
885 ios->in_attr_len);
b14f8ab2 886 }
06886a5a
BH
887
888out:
889 return ret;
890}
891
8ff660ab 892int ore_write(struct ore_io_state *ios)
5d952b83
BH
893{
894 int i;
895 int ret;
896
769ba8d9
BH
897 if (unlikely(ios->sp2d && !ios->r4w)) {
898 /* A library is attempting a RAID-write without providing
899 * a pages lock interface.
900 */
901 WARN_ON_ONCE(1);
902 return -ENOTSUPP;
903 }
904
5d952b83
BH
905 ret = _prepare_for_striping(ios);
906 if (unlikely(ret))
907 return ret;
908
909 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
8ff660ab 910 ret = _write_mirror(ios, i);
5d952b83
BH
911 if (unlikely(ret))
912 return ret;
913 }
914
8ff660ab 915 ret = ore_io_execute(ios);
5d952b83
BH
916 return ret;
917}
cf283ade 918EXPORT_SYMBOL(ore_write);
5d952b83 919
769ba8d9 920int _ore_read_mirror(struct ore_io_state *ios, unsigned cur_comp)
06886a5a 921{
46f4d973 922 struct osd_request *or;
8ff660ab 923 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
9e9db456
BH
924 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
925 unsigned first_dev = (unsigned)obj->id;
06886a5a 926
50a76fd3
BH
927 if (ios->pages && !per_dev->length)
928 return 0; /* Just an empty slot */
929
5d952b83 930 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
9e9db456 931 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
46f4d973 932 if (unlikely(!or)) {
8ff660ab 933 ORE_ERR("%s: osd_start_request failed\n", __func__);
46f4d973
BH
934 return -ENOMEM;
935 }
936 per_dev->or = or;
46f4d973 937
86093aaf 938 if (ios->pages) {
a1fec1db
BH
939 if (per_dev->cur_sg) {
940 /* finalize the last sg_entry */
941 _ore_add_sg_seg(per_dev, 0, false);
942 if (unlikely(!per_dev->cur_sg))
943 return 0; /* Skip parity only device */
944
945 osd_req_read_sg(or, obj, per_dev->bio,
946 per_dev->sglist, per_dev->cur_sg);
947 } else {
948 /* The no raid case */
949 osd_req_read(or, obj, per_dev->offset,
950 per_dev->bio, per_dev->length);
951 }
952
8ff660ab 953 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
a1fec1db 954 " dev=%d sg_len=%d\n", _LLU(obj->id),
5d952b83 955 _LLU(per_dev->offset), _LLU(per_dev->length),
a1fec1db 956 first_dev, per_dev->cur_sg);
46f4d973 957 } else {
6851a5e5
BH
958 BUG_ON(ios->kern_buff);
959
9e9db456 960 osd_req_get_attributes(or, obj);
8ff660ab 961 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
9e9db456
BH
962 _LLU(obj->id),
963 ios->in_attr_len, first_dev);
46f4d973 964 }
46f4d973
BH
965 if (ios->out_attr)
966 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
b14f8ab2 967
46f4d973
BH
968 if (ios->in_attr)
969 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
b14f8ab2 970
5d952b83
BH
971 return 0;
972}
973
8ff660ab 974int ore_read(struct ore_io_state *ios)
5d952b83
BH
975{
976 int i;
977 int ret;
978
979 ret = _prepare_for_striping(ios);
980 if (unlikely(ret))
981 return ret;
982
983 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
769ba8d9 984 ret = _ore_read_mirror(ios, i);
5d952b83
BH
985 if (unlikely(ret))
986 return ret;
987 }
988
8ff660ab 989 ret = ore_io_execute(ios);
5d952b83 990 return ret;
b14f8ab2 991}
cf283ade 992EXPORT_SYMBOL(ore_read);
b14f8ab2 993
8ff660ab 994int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
b14f8ab2
BH
995{
996 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
997 void *iter = NULL;
998 int nelem;
999
1000 do {
1001 nelem = 1;
06886a5a
BH
1002 osd_req_decode_get_attr_list(ios->per_dev[0].or,
1003 &cur_attr, &nelem, &iter);
b14f8ab2
BH
1004 if ((cur_attr.attr_page == attr->attr_page) &&
1005 (cur_attr.attr_id == attr->attr_id)) {
1006 attr->len = cur_attr.len;
1007 attr->val_ptr = cur_attr.val_ptr;
1008 return 0;
1009 }
1010 } while (iter);
1011
1012 return -EIO;
1013}
cf283ade 1014EXPORT_SYMBOL(extract_attr_from_ios);
06886a5a 1015
8ff660ab 1016static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
5d952b83
BH
1017 struct osd_attr *attr)
1018{
1019 int last_comp = cur_comp + ios->layout->mirrors_p1;
1020
1021 for (; cur_comp < last_comp; ++cur_comp) {
8ff660ab 1022 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
5d952b83
BH
1023 struct osd_request *or;
1024
9e9db456 1025 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
5d952b83 1026 if (unlikely(!or)) {
8ff660ab 1027 ORE_ERR("%s: osd_start_request failed\n", __func__);
5d952b83
BH
1028 return -ENOMEM;
1029 }
1030 per_dev->or = or;
1031
9e9db456 1032 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
5d952b83
BH
1033 osd_req_add_set_attr_list(or, attr, 1);
1034 }
1035
1036 return 0;
1037}
1038
16f75bb3 1039struct _trunc_info {
eb507bc1 1040 struct ore_striping_info si;
16f75bb3
BH
1041 u64 prev_group_obj_off;
1042 u64 next_group_obj_off;
1043
1044 unsigned first_group_dev;
1045 unsigned nex_group_dev;
16f75bb3
BH
1046};
1047
1958c7c2
HS
1048static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
1049 struct _trunc_info *ti)
16f75bb3
BH
1050{
1051 unsigned stripe_unit = layout->stripe_unit;
1052
a1fec1db 1053 ore_calc_stripe_info(layout, file_offset, 0, &ti->si);
16f75bb3
BH
1054
1055 ti->prev_group_obj_off = ti->si.M * stripe_unit;
1056 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
1057
1058 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
1059 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
16f75bb3
BH
1060}
1061
5bf696da 1062int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
9e9db456 1063 u64 size)
06886a5a 1064{
8ff660ab 1065 struct ore_io_state *ios;
5d952b83
BH
1066 struct exofs_trunc_attr {
1067 struct osd_attr attr;
1068 __be64 newsize;
1069 } *size_attrs;
16f75bb3 1070 struct _trunc_info ti;
06886a5a
BH
1071 int i, ret;
1072
5bf696da 1073 ret = ore_get_io_state(layout, oc, &ios);
5d952b83
BH
1074 if (unlikely(ret))
1075 return ret;
1076
16f75bb3
BH
1077 _calc_trunk_info(ios->layout, size, &ti);
1078
b916c5cd 1079 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
5d952b83
BH
1080 GFP_KERNEL);
1081 if (unlikely(!size_attrs)) {
1082 ret = -ENOMEM;
1083 goto out;
1084 }
06886a5a 1085
5bf696da 1086 ios->numdevs = ios->oc->numdevs;
06886a5a 1087
b916c5cd 1088 for (i = 0; i < ios->numdevs; ++i) {
5d952b83
BH
1089 struct exofs_trunc_attr *size_attr = &size_attrs[i];
1090 u64 obj_size;
06886a5a 1091
16f75bb3
BH
1092 if (i < ti.first_group_dev)
1093 obj_size = ti.prev_group_obj_off;
1094 else if (i >= ti.nex_group_dev)
1095 obj_size = ti.next_group_obj_off;
1096 else if (i < ti.si.dev) /* dev within this group */
1097 obj_size = ti.si.obj_offset +
1098 ios->layout->stripe_unit - ti.si.unit_off;
1099 else if (i == ti.si.dev)
1100 obj_size = ti.si.obj_offset;
1101 else /* i > ti.dev */
1102 obj_size = ti.si.obj_offset - ti.si.unit_off;
06886a5a 1103
5d952b83
BH
1104 size_attr->newsize = cpu_to_be64(obj_size);
1105 size_attr->attr = g_attr_logical_length;
1106 size_attr->attr.val_ptr = &size_attr->newsize;
1107
8ff660ab 1108 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
5bf696da 1109 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
5d952b83
BH
1110 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
1111 &size_attr->attr);
1112 if (unlikely(ret))
1113 goto out;
06886a5a 1114 }
8ff660ab 1115 ret = ore_io_execute(ios);
06886a5a
BH
1116
1117out:
5d952b83 1118 kfree(size_attrs);
8ff660ab 1119 ore_put_io_state(ios);
06886a5a
BH
1120 return ret;
1121}
cf283ade 1122EXPORT_SYMBOL(ore_truncate);
85e44df4
BH
1123
1124const struct osd_attr g_attr_logical_length = ATTR_DEF(
1125 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
cf283ade 1126EXPORT_SYMBOL(g_attr_logical_length);