2 * Copyright (C) 2005, 2006
3 * Avishay Traeger (avishay@gmail.com)
4 * Copyright (C) 2008, 2009
5 * Boaz Harrosh <bharrosh@panasas.com>
7 * This file is part of exofs.
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.
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.
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
25 #include <linux/slab.h>
26 #include <asm/div64.h>
27 #include <linux/lcm.h>
31 MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
32 MODULE_DESCRIPTION("Objects Raid Engine ore.ko");
33 MODULE_LICENSE("GPL");
35 /* ore_verify_layout does a couple of things:
36 * 1. Given a minimum number of needed parameters fixes up the rest of the
37 * members to be operatonals for the ore. The needed parameters are those
38 * that are defined by the pnfs-objects layout STD.
39 * 2. Check to see if the current ore code actually supports these parameters
40 * for example stripe_unit must be a multple of the system PAGE_SIZE,
42 * 3. Cache some havily used calculations that will be needed by users.
45 enum { BIO_MAX_PAGES_KMALLOC
=
46 (PAGE_SIZE
- sizeof(struct bio
)) / sizeof(struct bio_vec
),};
48 int ore_verify_layout(unsigned total_comps
, struct ore_layout
*layout
)
52 switch (layout
->raid_algorithm
) {
59 case PNFS_OSD_RAID_PQ
:
62 ORE_ERR("Only RAID_0/5 for now\n");
65 if (0 != (layout
->stripe_unit
& ~PAGE_MASK
)) {
66 ORE_ERR("Stripe Unit(0x%llx)"
67 " must be Multples of PAGE_SIZE(0x%lx)\n",
68 _LLU(layout
->stripe_unit
), PAGE_SIZE
);
71 if (layout
->group_width
) {
72 if (!layout
->group_depth
) {
73 ORE_ERR("group_depth == 0 && group_width != 0\n");
76 if (total_comps
< (layout
->group_width
* layout
->mirrors_p1
)) {
77 ORE_ERR("Data Map wrong, "
78 "numdevs=%d < group_width=%d * mirrors=%d\n",
79 total_comps
, layout
->group_width
,
83 layout
->group_count
= total_comps
/ layout
->mirrors_p1
/
86 if (layout
->group_depth
) {
87 printk(KERN_NOTICE
"Warning: group_depth ignored "
88 "group_width == 0 && group_depth == %lld\n",
89 _LLU(layout
->group_depth
));
91 layout
->group_width
= total_comps
/ layout
->mirrors_p1
;
92 layout
->group_depth
= -1;
93 layout
->group_count
= 1;
96 stripe_length
= (u64
)layout
->group_width
* layout
->stripe_unit
;
97 if (stripe_length
>= (1ULL << 32)) {
98 ORE_ERR("Stripe_length(0x%llx) >= 32bit is not supported\n",
103 layout
->max_io_length
=
104 (BIO_MAX_PAGES_KMALLOC
* PAGE_SIZE
- layout
->stripe_unit
) *
106 if (layout
->parity
) {
107 unsigned stripe_length
=
108 (layout
->group_width
- layout
->parity
) *
111 layout
->max_io_length
/= stripe_length
;
112 layout
->max_io_length
*= stripe_length
;
116 EXPORT_SYMBOL(ore_verify_layout
);
118 static u8
*_ios_cred(struct ore_io_state
*ios
, unsigned index
)
120 return ios
->oc
->comps
[index
& ios
->oc
->single_comp
].cred
;
123 static struct osd_obj_id
*_ios_obj(struct ore_io_state
*ios
, unsigned index
)
125 return &ios
->oc
->comps
[index
& ios
->oc
->single_comp
].obj
;
128 static struct osd_dev
*_ios_od(struct ore_io_state
*ios
, unsigned index
)
130 ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n",
131 ios
->oc
->first_dev
, ios
->oc
->numdevs
, index
,
134 return ore_comp_dev(ios
->oc
, index
);
137 int _ore_get_io_state(struct ore_layout
*layout
,
138 struct ore_components
*oc
, unsigned numdevs
,
139 unsigned sgs_per_dev
, unsigned num_par_pages
,
140 struct ore_io_state
**pios
)
142 struct ore_io_state
*ios
;
144 struct osd_sg_entry
*sgilist
;
145 struct __alloc_all_io_state
{
146 struct ore_io_state ios
;
147 struct ore_per_dev_state per_dev
[numdevs
];
149 struct osd_sg_entry sglist
[sgs_per_dev
* numdevs
];
150 struct page
*pages
[num_par_pages
];
154 if (likely(sizeof(*_aios
) <= PAGE_SIZE
)) {
155 _aios
= kzalloc(sizeof(*_aios
), GFP_KERNEL
);
156 if (unlikely(!_aios
)) {
157 ORE_DBGMSG("Failed kzalloc bytes=%zd\n",
162 pages
= num_par_pages
? _aios
->pages
: NULL
;
163 sgilist
= sgs_per_dev
? _aios
->sglist
: NULL
;
166 struct __alloc_small_io_state
{
167 struct ore_io_state ios
;
168 struct ore_per_dev_state per_dev
[numdevs
];
171 struct osd_sg_entry sglist
[sgs_per_dev
* numdevs
];
172 struct page
*pages
[num_par_pages
];
175 _aio_small
= kzalloc(sizeof(*_aio_small
), GFP_KERNEL
);
176 if (unlikely(!_aio_small
)) {
177 ORE_DBGMSG("Failed alloc first part bytes=%zd\n",
178 sizeof(*_aio_small
));
182 extra_part
= kzalloc(sizeof(*extra_part
), GFP_KERNEL
);
183 if (unlikely(!extra_part
)) {
184 ORE_DBGMSG("Failed alloc second part bytes=%zd\n",
185 sizeof(*extra_part
));
191 pages
= num_par_pages
? extra_part
->pages
: NULL
;
192 sgilist
= sgs_per_dev
? extra_part
->sglist
: NULL
;
193 /* In this case the per_dev[0].sgilist holds the pointer to
196 ios
= &_aio_small
->ios
;
197 ios
->extra_part_alloc
= true;
201 ios
->parity_pages
= pages
;
202 ios
->max_par_pages
= num_par_pages
;
207 for (d
= 0; d
< numdevs
; ++d
) {
208 ios
->per_dev
[d
].sglist
= sgilist
;
209 sgilist
+= sgs_per_dev
;
211 ios
->sgs_per_dev
= sgs_per_dev
;
214 ios
->layout
= layout
;
220 /* Allocate an io_state for only a single group of devices
222 * If a user needs to call ore_read/write() this version must be used becase it
223 * allocates extra stuff for striping and raid.
224 * The ore might decide to only IO less then @length bytes do to alignmets
225 * and constrains as follows:
226 * - The IO cannot cross group boundary.
227 * - In raid5/6 The end of the IO must align at end of a stripe eg.
228 * (@offset + @length) % strip_size == 0. Or the complete range is within a
230 * - Memory condition only permitted a shorter IO. (A user can use @length=~0
231 * And check the returned ios->length for max_io_size.)
233 * The caller must check returned ios->length (and/or ios->nr_pages) and
234 * re-issue these pages that fall outside of ios->length
236 int ore_get_rw_state(struct ore_layout
*layout
, struct ore_components
*oc
,
237 bool is_reading
, u64 offset
, u64 length
,
238 struct ore_io_state
**pios
)
240 struct ore_io_state
*ios
;
241 unsigned numdevs
= layout
->group_width
* layout
->mirrors_p1
;
242 unsigned sgs_per_dev
= 0, max_par_pages
= 0;
245 if (layout
->parity
&& length
) {
246 unsigned data_devs
= layout
->group_width
- layout
->parity
;
247 unsigned stripe_size
= layout
->stripe_unit
* data_devs
;
248 unsigned pages_in_unit
= layout
->stripe_unit
/ PAGE_SIZE
;
253 num_stripes
= div_u64_rem(length
, stripe_size
, &remainder
);
257 num_raid_units
= num_stripes
* layout
->parity
;
260 /* For reads add per_dev sglist array */
261 /* TODO: Raid 6 we need twice more. Actually:
262 * num_stripes / LCMdP(W,P);
263 * if (W%P != 0) num_stripes *= parity;
266 /* first/last seg is split */
267 num_raid_units
+= layout
->group_width
;
268 sgs_per_dev
= div_u64(num_raid_units
, data_devs
);
270 /* For Writes add parity pages array. */
271 max_par_pages
= num_raid_units
* pages_in_unit
*
272 sizeof(struct page
*);
276 ret
= _ore_get_io_state(layout
, oc
, numdevs
, sgs_per_dev
, max_par_pages
,
282 ios
->reading
= is_reading
;
283 ios
->offset
= offset
;
286 ore_calc_stripe_info(layout
, offset
, length
, &ios
->si
);
287 ios
->length
= ios
->si
.length
;
288 ios
->nr_pages
= (ios
->length
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
290 _ore_post_alloc_raid_stuff(ios
);
295 EXPORT_SYMBOL(ore_get_rw_state
);
297 /* Allocate an io_state for all the devices in the comps array
299 * This version of io_state allocation is used mostly by create/remove
300 * and trunc where we currently need all the devices. The only wastful
301 * bit is the read/write_attributes with no IO. Those sites should
302 * be converted to use ore_get_rw_state() with length=0
304 int ore_get_io_state(struct ore_layout
*layout
, struct ore_components
*oc
,
305 struct ore_io_state
**pios
)
307 return _ore_get_io_state(layout
, oc
, oc
->numdevs
, 0, 0, pios
);
309 EXPORT_SYMBOL(ore_get_io_state
);
311 void ore_put_io_state(struct ore_io_state
*ios
)
316 for (i
= 0; i
< ios
->numdevs
; i
++) {
317 struct ore_per_dev_state
*per_dev
= &ios
->per_dev
[i
];
320 osd_end_request(per_dev
->or);
322 bio_put(per_dev
->bio
);
325 _ore_free_raid_stuff(ios
);
329 EXPORT_SYMBOL(ore_put_io_state
);
331 static void _sync_done(struct ore_io_state
*ios
, void *p
)
333 struct completion
*waiting
= p
;
338 static void _last_io(struct kref
*kref
)
340 struct ore_io_state
*ios
= container_of(
341 kref
, struct ore_io_state
, kref
);
343 ios
->done(ios
, ios
->private);
346 static void _done_io(struct osd_request
*or, void *p
)
348 struct ore_io_state
*ios
= p
;
350 kref_put(&ios
->kref
, _last_io
);
353 int ore_io_execute(struct ore_io_state
*ios
)
355 DECLARE_COMPLETION_ONSTACK(wait
);
356 bool sync
= (ios
->done
== NULL
);
360 ios
->done
= _sync_done
;
361 ios
->private = &wait
;
364 for (i
= 0; i
< ios
->numdevs
; i
++) {
365 struct osd_request
*or = ios
->per_dev
[i
].or;
369 ret
= osd_finalize_request(or, 0, _ios_cred(ios
, i
), NULL
);
371 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
377 kref_init(&ios
->kref
);
379 for (i
= 0; i
< ios
->numdevs
; i
++) {
380 struct osd_request
*or = ios
->per_dev
[i
].or;
384 kref_get(&ios
->kref
);
385 osd_execute_request_async(or, _done_io
, ios
);
388 kref_put(&ios
->kref
, _last_io
);
392 wait_for_completion(&wait
);
393 ret
= ore_check_io(ios
, NULL
);
398 static void _clear_bio(struct bio
*bio
)
403 __bio_for_each_segment(bv
, bio
, i
, 0) {
404 unsigned this_count
= bv
->bv_len
;
406 if (likely(PAGE_SIZE
== this_count
))
407 clear_highpage(bv
->bv_page
);
409 zero_user(bv
->bv_page
, bv
->bv_offset
, this_count
);
413 int ore_check_io(struct ore_io_state
*ios
, ore_on_dev_error on_dev_error
)
415 enum osd_err_priority acumulated_osd_err
= 0;
416 int acumulated_lin_err
= 0;
419 for (i
= 0; i
< ios
->numdevs
; i
++) {
420 struct osd_sense_info osi
;
421 struct ore_per_dev_state
*per_dev
= &ios
->per_dev
[i
];
422 struct osd_request
*or = per_dev
->or;
428 ret
= osd_req_decode_sense(or, &osi
);
432 if (OSD_ERR_PRI_CLEAR_PAGES
== osi
.osd_err_pri
) {
433 /* start read offset passed endof file */
434 _clear_bio(per_dev
->bio
);
435 ORE_DBGMSG("start read offset passed end of file "
436 "offset=0x%llx, length=0x%llx\n",
437 _LLU(per_dev
->offset
),
438 _LLU(per_dev
->length
));
440 continue; /* we recovered */
444 u64 residual
= ios
->reading
?
445 or->in
.residual
: or->out
.residual
;
446 u64 offset
= (ios
->offset
+ ios
->length
) - residual
;
447 struct ore_dev
*od
= ios
->oc
->ods
[
448 per_dev
->dev
- ios
->oc
->first_dev
];
450 on_dev_error(ios
, od
, per_dev
->dev
, osi
.osd_err_pri
,
453 if (osi
.osd_err_pri
>= acumulated_osd_err
) {
454 acumulated_osd_err
= osi
.osd_err_pri
;
455 acumulated_lin_err
= ret
;
459 return acumulated_lin_err
;
461 EXPORT_SYMBOL(ore_check_io
);
464 * L - logical offset into the file
466 * D - number of Data devices
467 * D = group_width - parity
469 * U - The number of bytes in a stripe within a group
470 * U = stripe_unit * D
472 * T - The number of bytes striped within a group of component objects
473 * (before advancing to the next group)
474 * T = U * group_depth
476 * S - The number of bytes striped across all component objects
477 * before the pattern repeats
478 * S = T * group_count
480 * M - The "major" (i.e., across all components) cycle number
483 * G - Counts the groups from the beginning of the major cycle
484 * G = (L - (M * S)) / T [or (L % S) / T]
486 * H - The byte offset within the group
487 * H = (L - (M * S)) % T [or (L % S) % T]
489 * N - The "minor" (i.e., across the group) stripe number
492 * C - The component index coresponding to L
494 * C = (H - (N * U)) / stripe_unit + G * D
495 * [or (L % U) / stripe_unit + G * D]
497 * O - The component offset coresponding to L
498 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
500 * LCMdP – Parity cycle: Lowest Common Multiple of group_width, parity
502 * LCMdP = lcm(group_width, parity) / parity
504 * R - The parity Rotation stripe
505 * (Note parity cycle always starts at a group's boundary)
508 * I = the first parity device index
509 * I = (group_width + group_width - R*parity - parity) % group_width
511 * Craid - The component index Rotated
512 * Craid = (group_width + C - R*parity) % group_width
513 * (We add the group_width to avoid negative numbers modulo math)
515 void ore_calc_stripe_info(struct ore_layout
*layout
, u64 file_offset
,
516 u64 length
, struct ore_striping_info
*si
)
518 u32 stripe_unit
= layout
->stripe_unit
;
519 u32 group_width
= layout
->group_width
;
520 u64 group_depth
= layout
->group_depth
;
521 u32 parity
= layout
->parity
;
523 u32 D
= group_width
- parity
;
524 u32 U
= D
* stripe_unit
;
525 u64 T
= U
* group_depth
;
526 u64 S
= T
* layout
->group_count
;
527 u64 M
= div64_u64(file_offset
, S
);
530 G = (L - (M * S)) / T
531 H = (L - (M * S)) % T
533 u64 LmodS
= file_offset
- M
* S
;
534 u32 G
= div64_u64(LmodS
, T
);
535 u64 H
= LmodS
- G
* T
;
537 u32 N
= div_u64(H
, U
);
539 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
540 u32 C
= (u32
)(H
- (N
* U
)) / stripe_unit
+ G
* group_width
;
542 div_u64_rem(file_offset
, stripe_unit
, &si
->unit_off
);
544 si
->obj_offset
= si
->unit_off
+ (N
* stripe_unit
) +
545 (M
* group_depth
* stripe_unit
);
548 u32 LCMdP
= lcm(group_width
, parity
) / parity
;
550 u32 RxP
= (N
% LCMdP
) * parity
;
551 u32 first_dev
= C
- C
% group_width
;
553 si
->par_dev
= (group_width
+ group_width
- parity
- RxP
) %
554 group_width
+ first_dev
;
555 si
->dev
= (group_width
+ C
- RxP
) % group_width
+ first_dev
;
556 si
->bytes_in_stripe
= U
;
557 si
->first_stripe_start
= M
* S
+ G
* T
+ N
* U
;
559 /* Make the math correct see _prepare_one_group */
560 si
->par_dev
= group_width
;
564 si
->dev
*= layout
->mirrors_p1
;
565 si
->par_dev
*= layout
->mirrors_p1
;
566 si
->offset
= file_offset
;
568 if (si
->length
> length
)
572 EXPORT_SYMBOL(ore_calc_stripe_info
);
574 int _ore_add_stripe_unit(struct ore_io_state
*ios
, unsigned *cur_pg
,
575 unsigned pgbase
, struct page
**pages
,
576 struct ore_per_dev_state
*per_dev
, int cur_len
)
578 unsigned pg
= *cur_pg
;
579 struct request_queue
*q
=
580 osd_request_queue(_ios_od(ios
, per_dev
->dev
));
581 unsigned len
= cur_len
;
584 if (per_dev
->bio
== NULL
) {
585 unsigned pages_in_stripe
= ios
->layout
->group_width
*
586 (ios
->layout
->stripe_unit
/ PAGE_SIZE
);
587 unsigned nr_pages
= ios
->nr_pages
* ios
->layout
->group_width
/
588 (ios
->layout
->group_width
-
589 ios
->layout
->parity
);
590 unsigned bio_size
= (nr_pages
+ pages_in_stripe
) /
591 ios
->layout
->group_width
;
593 per_dev
->bio
= bio_kmalloc(GFP_KERNEL
, bio_size
);
594 if (unlikely(!per_dev
->bio
)) {
595 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
602 while (cur_len
> 0) {
603 unsigned pglen
= min_t(unsigned, PAGE_SIZE
- pgbase
, cur_len
);
608 added_len
= bio_add_pc_page(q
, per_dev
->bio
, pages
[pg
],
610 if (unlikely(pglen
!= added_len
)) {
611 ORE_DBGMSG("Failed bio_add_pc_page bi_vcnt=%u\n",
612 per_dev
->bio
->bi_vcnt
);
616 _add_stripe_page(ios
->sp2d
, &ios
->si
, pages
[pg
]);
623 per_dev
->length
+= len
;
626 out
: /* we fail the complete unit on an error eg don't advance
627 * per_dev->length and cur_pg. This means that we might have a bigger
628 * bio than the CDB requested length (per_dev->length). That's fine
629 * only the oposite is fatal.
634 static int _prepare_for_striping(struct ore_io_state
*ios
)
636 struct ore_striping_info
*si
= &ios
->si
;
637 unsigned stripe_unit
= ios
->layout
->stripe_unit
;
638 unsigned mirrors_p1
= ios
->layout
->mirrors_p1
;
639 unsigned group_width
= ios
->layout
->group_width
;
640 unsigned devs_in_group
= group_width
* mirrors_p1
;
641 unsigned dev
= si
->dev
;
642 unsigned first_dev
= dev
- (dev
% devs_in_group
);
644 unsigned cur_pg
= ios
->pages_consumed
;
645 u64 length
= ios
->length
;
649 ios
->numdevs
= ios
->layout
->mirrors_p1
;
653 BUG_ON(length
> si
->length
);
655 dev_order
= _dev_order(devs_in_group
, mirrors_p1
, si
->par_dev
, dev
);
656 si
->cur_comp
= dev_order
;
657 si
->cur_pg
= si
->unit_off
/ PAGE_SIZE
;
660 unsigned comp
= dev
- first_dev
;
661 struct ore_per_dev_state
*per_dev
= &ios
->per_dev
[comp
];
662 unsigned cur_len
, page_off
= 0;
664 if (!per_dev
->length
) {
666 if (dev
== si
->dev
) {
667 WARN_ON(dev
== si
->par_dev
);
668 per_dev
->offset
= si
->obj_offset
;
669 cur_len
= stripe_unit
- si
->unit_off
;
670 page_off
= si
->unit_off
& ~PAGE_MASK
;
671 BUG_ON(page_off
&& (page_off
!= ios
->pgbase
));
673 if (si
->cur_comp
> dev_order
)
675 si
->obj_offset
- si
->unit_off
;
676 else /* si->cur_comp < dev_order */
678 si
->obj_offset
+ stripe_unit
-
680 cur_len
= stripe_unit
;
683 cur_len
= stripe_unit
;
685 if (cur_len
>= length
)
688 ret
= _ore_add_stripe_unit(ios
, &cur_pg
, page_off
, ios
->pages
,
694 dev
= (dev
% devs_in_group
) + first_dev
;
698 si
->cur_comp
= (si
->cur_comp
+ 1) % group_width
;
699 if (unlikely((dev
== si
->par_dev
) || (!length
&& ios
->sp2d
))) {
700 if (!length
&& ios
->sp2d
) {
701 /* If we are writing and this is the very last
702 * stripe. then operate on parity dev.
707 /* In writes cur_len just means if it's the
708 * last one. See _ore_add_parity_unit.
711 per_dev
= &ios
->per_dev
[dev
- first_dev
];
712 if (!per_dev
->length
) {
713 /* Only/always the parity unit of the first
714 * stripe will be empty. So this is a chance to
715 * initialize the per_dev info.
718 per_dev
->offset
= si
->obj_offset
- si
->unit_off
;
721 ret
= _ore_add_parity_unit(ios
, si
, per_dev
, cur_len
);
725 /* Rotate next par_dev backwards with wraping */
726 si
->par_dev
= (devs_in_group
+ si
->par_dev
-
727 ios
->layout
->parity
* mirrors_p1
) %
728 devs_in_group
+ first_dev
;
729 /* Next stripe, start fresh */
735 ios
->numdevs
= devs_in_group
;
736 ios
->pages_consumed
= cur_pg
;
738 if (length
== ios
->length
)
741 ios
->length
-= length
;
746 int ore_create(struct ore_io_state
*ios
)
750 for (i
= 0; i
< ios
->oc
->numdevs
; i
++) {
751 struct osd_request
*or;
753 or = osd_start_request(_ios_od(ios
, i
), GFP_KERNEL
);
755 ORE_ERR("%s: osd_start_request failed\n", __func__
);
759 ios
->per_dev
[i
].or = or;
762 osd_req_create_object(or, _ios_obj(ios
, i
));
764 ret
= ore_io_execute(ios
);
769 EXPORT_SYMBOL(ore_create
);
771 int ore_remove(struct ore_io_state
*ios
)
775 for (i
= 0; i
< ios
->oc
->numdevs
; i
++) {
776 struct osd_request
*or;
778 or = osd_start_request(_ios_od(ios
, i
), GFP_KERNEL
);
780 ORE_ERR("%s: osd_start_request failed\n", __func__
);
784 ios
->per_dev
[i
].or = or;
787 osd_req_remove_object(or, _ios_obj(ios
, i
));
789 ret
= ore_io_execute(ios
);
794 EXPORT_SYMBOL(ore_remove
);
796 static int _write_mirror(struct ore_io_state
*ios
, int cur_comp
)
798 struct ore_per_dev_state
*master_dev
= &ios
->per_dev
[cur_comp
];
799 unsigned dev
= ios
->per_dev
[cur_comp
].dev
;
800 unsigned last_comp
= cur_comp
+ ios
->layout
->mirrors_p1
;
803 if (ios
->pages
&& !master_dev
->length
)
804 return 0; /* Just an empty slot */
806 for (; cur_comp
< last_comp
; ++cur_comp
, ++dev
) {
807 struct ore_per_dev_state
*per_dev
= &ios
->per_dev
[cur_comp
];
808 struct osd_request
*or;
810 or = osd_start_request(_ios_od(ios
, dev
), GFP_KERNEL
);
812 ORE_ERR("%s: osd_start_request failed\n", __func__
);
821 if (per_dev
!= master_dev
) {
822 bio
= bio_kmalloc(GFP_KERNEL
,
823 master_dev
->bio
->bi_max_vecs
);
824 if (unlikely(!bio
)) {
826 "Failed to allocate BIO size=%u\n",
827 master_dev
->bio
->bi_max_vecs
);
832 __bio_clone(bio
, master_dev
->bio
);
835 per_dev
->offset
= master_dev
->offset
;
836 per_dev
->length
= master_dev
->length
;
840 bio
= master_dev
->bio
;
841 /* FIXME: bio_set_dir() */
842 bio
->bi_rw
|= REQ_WRITE
;
845 osd_req_write(or, _ios_obj(ios
, dev
), per_dev
->offset
,
846 bio
, per_dev
->length
);
847 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
848 "length=0x%llx dev=%d\n",
849 _LLU(_ios_obj(ios
, dev
)->id
),
850 _LLU(per_dev
->offset
),
851 _LLU(per_dev
->length
), dev
);
852 } else if (ios
->kern_buff
) {
853 per_dev
->offset
= ios
->si
.obj_offset
;
854 per_dev
->dev
= ios
->si
.dev
+ dev
;
856 /* no cross device without page array */
857 BUG_ON((ios
->layout
->group_width
> 1) &&
858 (ios
->si
.unit_off
+ ios
->length
>
859 ios
->layout
->stripe_unit
));
861 ret
= osd_req_write_kern(or, _ios_obj(ios
, per_dev
->dev
),
863 ios
->kern_buff
, ios
->length
);
866 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
867 "length=0x%llx dev=%d\n",
868 _LLU(_ios_obj(ios
, dev
)->id
),
869 _LLU(per_dev
->offset
),
870 _LLU(ios
->length
), per_dev
->dev
);
872 osd_req_set_attributes(or, _ios_obj(ios
, dev
));
873 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
874 _LLU(_ios_obj(ios
, dev
)->id
),
875 ios
->out_attr_len
, dev
);
879 osd_req_add_set_attr_list(or, ios
->out_attr
,
883 osd_req_add_get_attr_list(or, ios
->in_attr
,
891 int ore_write(struct ore_io_state
*ios
)
896 if (unlikely(ios
->sp2d
&& !ios
->r4w
)) {
897 /* A library is attempting a RAID-write without providing
898 * a pages lock interface.
904 ret
= _prepare_for_striping(ios
);
908 for (i
= 0; i
< ios
->numdevs
; i
+= ios
->layout
->mirrors_p1
) {
909 ret
= _write_mirror(ios
, i
);
914 ret
= ore_io_execute(ios
);
917 EXPORT_SYMBOL(ore_write
);
919 int _ore_read_mirror(struct ore_io_state
*ios
, unsigned cur_comp
)
921 struct osd_request
*or;
922 struct ore_per_dev_state
*per_dev
= &ios
->per_dev
[cur_comp
];
923 struct osd_obj_id
*obj
= _ios_obj(ios
, cur_comp
);
924 unsigned first_dev
= (unsigned)obj
->id
;
926 if (ios
->pages
&& !per_dev
->length
)
927 return 0; /* Just an empty slot */
929 first_dev
= per_dev
->dev
+ first_dev
% ios
->layout
->mirrors_p1
;
930 or = osd_start_request(_ios_od(ios
, first_dev
), GFP_KERNEL
);
932 ORE_ERR("%s: osd_start_request failed\n", __func__
);
938 if (per_dev
->cur_sg
) {
939 /* finalize the last sg_entry */
940 _ore_add_sg_seg(per_dev
, 0, false);
941 if (unlikely(!per_dev
->cur_sg
))
942 return 0; /* Skip parity only device */
944 osd_req_read_sg(or, obj
, per_dev
->bio
,
945 per_dev
->sglist
, per_dev
->cur_sg
);
947 /* The no raid case */
948 osd_req_read(or, obj
, per_dev
->offset
,
949 per_dev
->bio
, per_dev
->length
);
952 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
953 " dev=%d sg_len=%d\n", _LLU(obj
->id
),
954 _LLU(per_dev
->offset
), _LLU(per_dev
->length
),
955 first_dev
, per_dev
->cur_sg
);
957 BUG_ON(ios
->kern_buff
);
959 osd_req_get_attributes(or, obj
);
960 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
962 ios
->in_attr_len
, first_dev
);
965 osd_req_add_set_attr_list(or, ios
->out_attr
, ios
->out_attr_len
);
968 osd_req_add_get_attr_list(or, ios
->in_attr
, ios
->in_attr_len
);
973 int ore_read(struct ore_io_state
*ios
)
978 ret
= _prepare_for_striping(ios
);
982 for (i
= 0; i
< ios
->numdevs
; i
+= ios
->layout
->mirrors_p1
) {
983 ret
= _ore_read_mirror(ios
, i
);
988 ret
= ore_io_execute(ios
);
991 EXPORT_SYMBOL(ore_read
);
993 int extract_attr_from_ios(struct ore_io_state
*ios
, struct osd_attr
*attr
)
995 struct osd_attr cur_attr
= {.attr_page
= 0}; /* start with zeros */
1001 osd_req_decode_get_attr_list(ios
->per_dev
[0].or,
1002 &cur_attr
, &nelem
, &iter
);
1003 if ((cur_attr
.attr_page
== attr
->attr_page
) &&
1004 (cur_attr
.attr_id
== attr
->attr_id
)) {
1005 attr
->len
= cur_attr
.len
;
1006 attr
->val_ptr
= cur_attr
.val_ptr
;
1013 EXPORT_SYMBOL(extract_attr_from_ios
);
1015 static int _truncate_mirrors(struct ore_io_state
*ios
, unsigned cur_comp
,
1016 struct osd_attr
*attr
)
1018 int last_comp
= cur_comp
+ ios
->layout
->mirrors_p1
;
1020 for (; cur_comp
< last_comp
; ++cur_comp
) {
1021 struct ore_per_dev_state
*per_dev
= &ios
->per_dev
[cur_comp
];
1022 struct osd_request
*or;
1024 or = osd_start_request(_ios_od(ios
, cur_comp
), GFP_KERNEL
);
1025 if (unlikely(!or)) {
1026 ORE_ERR("%s: osd_start_request failed\n", __func__
);
1031 osd_req_set_attributes(or, _ios_obj(ios
, cur_comp
));
1032 osd_req_add_set_attr_list(or, attr
, 1);
1038 struct _trunc_info
{
1039 struct ore_striping_info si
;
1040 u64 prev_group_obj_off
;
1041 u64 next_group_obj_off
;
1043 unsigned first_group_dev
;
1044 unsigned nex_group_dev
;
1047 static void _calc_trunk_info(struct ore_layout
*layout
, u64 file_offset
,
1048 struct _trunc_info
*ti
)
1050 unsigned stripe_unit
= layout
->stripe_unit
;
1052 ore_calc_stripe_info(layout
, file_offset
, 0, &ti
->si
);
1054 ti
->prev_group_obj_off
= ti
->si
.M
* stripe_unit
;
1055 ti
->next_group_obj_off
= ti
->si
.M
? (ti
->si
.M
- 1) * stripe_unit
: 0;
1057 ti
->first_group_dev
= ti
->si
.dev
- (ti
->si
.dev
% layout
->group_width
);
1058 ti
->nex_group_dev
= ti
->first_group_dev
+ layout
->group_width
;
1061 int ore_truncate(struct ore_layout
*layout
, struct ore_components
*oc
,
1064 struct ore_io_state
*ios
;
1065 struct exofs_trunc_attr
{
1066 struct osd_attr attr
;
1069 struct _trunc_info ti
;
1072 ret
= ore_get_io_state(layout
, oc
, &ios
);
1076 _calc_trunk_info(ios
->layout
, size
, &ti
);
1078 size_attrs
= kcalloc(ios
->oc
->numdevs
, sizeof(*size_attrs
),
1080 if (unlikely(!size_attrs
)) {
1085 ios
->numdevs
= ios
->oc
->numdevs
;
1087 for (i
= 0; i
< ios
->numdevs
; ++i
) {
1088 struct exofs_trunc_attr
*size_attr
= &size_attrs
[i
];
1091 if (i
< ti
.first_group_dev
)
1092 obj_size
= ti
.prev_group_obj_off
;
1093 else if (i
>= ti
.nex_group_dev
)
1094 obj_size
= ti
.next_group_obj_off
;
1095 else if (i
< ti
.si
.dev
) /* dev within this group */
1096 obj_size
= ti
.si
.obj_offset
+
1097 ios
->layout
->stripe_unit
- ti
.si
.unit_off
;
1098 else if (i
== ti
.si
.dev
)
1099 obj_size
= ti
.si
.obj_offset
;
1100 else /* i > ti.dev */
1101 obj_size
= ti
.si
.obj_offset
- ti
.si
.unit_off
;
1103 size_attr
->newsize
= cpu_to_be64(obj_size
);
1104 size_attr
->attr
= g_attr_logical_length
;
1105 size_attr
->attr
.val_ptr
= &size_attr
->newsize
;
1107 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
1108 _LLU(oc
->comps
->obj
.id
), _LLU(obj_size
), i
);
1109 ret
= _truncate_mirrors(ios
, i
* ios
->layout
->mirrors_p1
,
1114 ret
= ore_io_execute(ios
);
1118 ore_put_io_state(ios
);
1121 EXPORT_SYMBOL(ore_truncate
);
1123 const struct osd_attr g_attr_logical_length
= ATTR_DEF(
1124 OSD_APAGE_OBJECT_INFORMATION
, OSD_ATTR_OI_LOGICAL_LENGTH
, 8);
1125 EXPORT_SYMBOL(g_attr_logical_length
);