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