blob: f936cb50dc0d524250dae6d6ac5382db5c1f25b7 [file] [log] [blame]
Boaz Harroshb14f8ab2008-10-27 18:27:55 +02001/*
2 * Copyright (C) 2005, 2006
Boaz Harrosh27d2e142009-06-14 17:23:09 +03003 * Avishay Traeger (avishay@gmail.com)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +02004 * 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040026#include <linux/module.h>
Boaz Harrosh5d952b82010-02-01 13:35:51 +020027#include <asm/div64.h>
Boaz Harrosha1fec1d2011-10-12 18:42:22 +020028#include <linux/lcm.h>
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020029
Boaz Harrosha1fec1d2011-10-12 18:42:22 +020030#include "ore_raid.h"
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070031
Boaz Harroshcf283ad2011-08-06 19:22:06 -070032MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
33MODULE_DESCRIPTION("Objects Raid Engine ore.ko");
34MODULE_LICENSE("GPL");
35
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030036/* 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
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030046enum { 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
Boaz Harrosh44231e62010-11-21 20:03:24 +020053 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");
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030064 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;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200107 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 }
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +0300115 return 0;
116}
117EXPORT_SYMBOL(ore_verify_layout);
118
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700119static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700120{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300121 return ios->oc->comps[index & ios->oc->single_comp].cred;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700122}
123
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700124static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700125{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300126 return &ios->oc->comps[index & ios->oc->single_comp].obj;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700127}
128
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700129static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700130{
Boaz Harrosh3bd98562011-09-28 12:04:23 +0300131 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
Boaz Harroshd866d872011-09-28 14:43:09 +0300135 return ore_comp_dev(ios->oc, index);
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700136}
137
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200138int _ore_get_io_state(struct ore_layout *layout,
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200139 struct ore_components *oc, unsigned numdevs,
140 unsigned sgs_per_dev, unsigned num_par_pages,
141 struct ore_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200142{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700143 struct ore_io_state *ios;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200144 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;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200154
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200155 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 }
191
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;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200213 }
214
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200215 ios->layout = layout;
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300216 ios->oc = oc;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200217 *pios = ios;
218 return 0;
219}
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300220
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;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200243 unsigned sgs_per_dev = 0, max_par_pages = 0;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300244 int ret;
245
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200246 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;
Boaz Harrosh361aba52011-12-28 19:14:23 +0200269 sgs_per_dev = div_u64(num_raid_units, data_devs) + 2;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200270 } 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);
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300279 if (unlikely(ret))
280 return ret;
281
282 ios = *pios;
283 ios->reading = is_reading;
284 ios->offset = offset;
285
286 if (length) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200287 ore_calc_stripe_info(layout, offset, length, &ios->si);
288 ios->length = ios->si.length;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300289 ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200290 if (layout->parity)
291 _ore_post_alloc_raid_stuff(ios);
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300292 }
293
294 return 0;
295}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700296EXPORT_SYMBOL(ore_get_rw_state);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200297
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300298/* 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 */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300305int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300306 struct ore_io_state **pios)
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200307{
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200308 return _ore_get_io_state(layout, oc, oc->numdevs, 0, 0, pios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200309}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700310EXPORT_SYMBOL(ore_get_io_state);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200311
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700312void ore_put_io_state(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200313{
314 if (ios) {
315 unsigned i;
316
317 for (i = 0; i < ios->numdevs; i++) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700318 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200319
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
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200326 _ore_free_raid_stuff(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200327 kfree(ios);
328 }
329}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700330EXPORT_SYMBOL(ore_put_io_state);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200331
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700332static void _sync_done(struct ore_io_state *ios, void *p)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200333{
334 struct completion *waiting = p;
335
336 complete(waiting);
337}
338
339static void _last_io(struct kref *kref)
340{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700341 struct ore_io_state *ios = container_of(
342 kref, struct ore_io_state, kref);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200343
344 ios->done(ios, ios->private);
345}
346
347static void _done_io(struct osd_request *or, void *p)
348{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700349 struct ore_io_state *ios = p;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200350
351 kref_put(&ios->kref, _last_io);
352}
353
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200354int ore_io_execute(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200355{
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
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700370 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200371 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700372 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200373 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);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700394 ret = ore_check_io(ios, NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200395 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200396 return ret;
397}
398
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200399static 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
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300414int ore_check_io(struct ore_io_state *ios, ore_on_dev_error on_dev_error)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200415{
416 enum osd_err_priority acumulated_osd_err = 0;
417 int acumulated_lin_err = 0;
418 int i;
419
420 for (i = 0; i < ios->numdevs; i++) {
421 struct osd_sense_info osi;
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300422 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
423 struct osd_request *or = per_dev->or;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200424 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200425
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200426 if (unlikely(!or))
427 continue;
428
429 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200430 if (likely(!ret))
431 continue;
432
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200433 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
434 /* start read offset passed endof file */
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300435 _clear_bio(per_dev->bio);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700436 ORE_DBGMSG("start read offset passed end of file "
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200437 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300438 _LLU(per_dev->offset),
439 _LLU(per_dev->length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200440
441 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200442 }
443
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300444 if (on_dev_error) {
445 u64 residual = ios->reading ?
446 or->in.residual : or->out.residual;
447 u64 offset = (ios->offset + ios->length) - residual;
Boaz Harroshffefb8ea2011-12-27 19:23:36 +0200448 unsigned dev = per_dev->dev - ios->oc->first_dev;
449 struct ore_dev *od = ios->oc->ods[dev];
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300450
Boaz Harroshffefb8ea2011-12-27 19:23:36 +0200451 on_dev_error(ios, od, dev, osi.osd_err_pri,
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300452 offset, residual);
453 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200454 if (osi.osd_err_pri >= acumulated_osd_err) {
455 acumulated_osd_err = osi.osd_err_pri;
456 acumulated_lin_err = ret;
457 }
458 }
459
Boaz Harrosh06886a52009-11-08 14:54:08 +0200460 return acumulated_lin_err;
461}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700462EXPORT_SYMBOL(ore_check_io);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200463
Boaz Harroshb367e782010-02-07 19:18:58 +0200464/*
465 * L - logical offset into the file
466 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200467 * D - number of Data devices
468 * D = group_width - parity
Boaz Harroshb367e782010-02-07 19:18:58 +0200469 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200470 * U - The number of bytes in a stripe within a group
471 * U = stripe_unit * D
Boaz Harroshb367e782010-02-07 19:18:58 +0200472 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200473 * T - The number of bytes striped within a group of component objects
474 * (before advancing to the next group)
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200475 * T = U * group_depth
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200476 *
477 * S - The number of bytes striped across all component objects
478 * before the pattern repeats
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200479 * S = T * group_count
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200480 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200481 * M - The "major" (i.e., across all components) cycle number
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200482 * M = L / S
483 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200484 * G - Counts the groups from the beginning of the major cycle
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200485 * G = (L - (M * S)) / T [or (L % S) / T]
486 *
487 * H - The byte offset within the group
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200488 * H = (L - (M * S)) % T [or (L % S) % T]
489 *
490 * N - The "minor" (i.e., across the group) stripe number
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200491 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200492 *
493 * C - The component index coresponding to L
494 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200495 * C = (H - (N * U)) / stripe_unit + G * D
496 * [or (L % U) / stripe_unit + G * D]
Boaz Harroshb367e782010-02-07 19:18:58 +0200497 *
498 * O - The component offset coresponding to L
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200499 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200500 *
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)
Boaz Harroshb367e782010-02-07 19:18:58 +0200515 */
Boaz Harrosh611d7a52011-10-04 14:20:17 +0200516void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200517 u64 length, struct ore_striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200518{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700519 u32 stripe_unit = layout->stripe_unit;
520 u32 group_width = layout->group_width;
521 u64 group_depth = layout->group_depth;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200522 u32 parity = layout->parity;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200523
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200524 u32 D = group_width - parity;
525 u32 U = D * stripe_unit;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200526 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700527 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200528 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200529
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200530 /*
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;
Boaz Harroshb367e782010-02-07 19:18:58 +0200537
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200538 u32 N = div_u64(H, U);
539
540 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200541 u32 C = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200542
543 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
544
545 si->obj_offset = si->unit_off + (N * stripe_unit) +
546 (M * group_depth * stripe_unit);
547
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200548 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;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700571 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200572}
Boaz Harrosh611d7a52011-10-04 14:20:17 +0200573EXPORT_SYMBOL(ore_calc_stripe_info);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200574
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200575int _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)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200578{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200579 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200580 struct request_queue *q =
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700581 osd_request_queue(_ios_od(ios, per_dev->dev));
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700582 unsigned len = cur_len;
583 int ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200584
585 if (per_dev->bio == NULL) {
586 unsigned pages_in_stripe = ios->layout->group_width *
587 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200588 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;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200593
594 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
595 if (unlikely(!per_dev->bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700596 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200597 bio_size);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700598 ret = -ENOMEM;
599 goto out;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200600 }
601 }
602
603 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200604 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
605 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200606
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200607 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200608
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200609 added_len = bio_add_pc_page(q, per_dev->bio, pages[pg],
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200610 pglen, pgbase);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700611 if (unlikely(pglen != added_len)) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200612 ORE_DBGMSG("Failed bio_add_pc_page bi_vcnt=%u\n",
613 per_dev->bio->bi_vcnt);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700614 ret = -ENOMEM;
615 goto out;
616 }
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200617 _add_stripe_page(ios->sp2d, &ios->si, pages[pg]);
618
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200619 pgbase = 0;
620 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200621 }
622 BUG_ON(cur_len);
623
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700624 per_dev->length += len;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200625 *cur_pg = pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700626 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;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200633}
634
Boaz Harrosh98260752011-10-02 15:32:50 +0200635static int _prepare_for_striping(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200636{
Boaz Harrosh98260752011-10-02 15:32:50 +0200637 struct ore_striping_info *si = &ios->si;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200638 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200639 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200640 unsigned group_width = ios->layout->group_width;
641 unsigned devs_in_group = group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200642 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200643 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200644 unsigned dev_order;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200645 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh98260752011-10-02 15:32:50 +0200646 u64 length = ios->length;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200647 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200648
Boaz Harrosh98260752011-10-02 15:32:50 +0200649 if (!ios->pages) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200650 ios->numdevs = ios->layout->mirrors_p1;
651 return 0;
652 }
653
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200654 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;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200658 si->cur_pg = si->unit_off / PAGE_SIZE;
Boaz Harrosh98260752011-10-02 15:32:50 +0200659
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200660 while (length) {
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300661 unsigned comp = dev - first_dev;
662 struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
Boaz Harroshb367e782010-02-07 19:18:58 +0200663 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200664
665 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200666 per_dev->dev = dev;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200667 if (dev == si->dev) {
668 WARN_ON(dev == si->par_dev);
Boaz Harroshb367e782010-02-07 19:18:58 +0200669 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));
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200673 } 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;
Boaz Harroshb367e782010-02-07 19:18:58 +0200681 cur_len = stripe_unit;
682 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200683 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200684 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200685 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200686 if (cur_len >= length)
687 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200688
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200689 ret = _ore_add_stripe_unit(ios, &cur_pg, page_off, ios->pages,
690 per_dev, cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200691 if (unlikely(ret))
692 goto out;
693
Boaz Harrosh6e316092010-07-29 17:08:13 +0300694 dev += mirrors_p1;
695 dev = (dev % devs_in_group) + first_dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200696
697 length -= cur_len;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200698
699 si->cur_comp = (si->cur_comp + 1) % group_width;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200700 if (unlikely((dev == si->par_dev) || (!length && ios->sp2d))) {
701 if (!length && ios->sp2d) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200702 /* If we are writing and this is the very last
703 * stripe. then operate on parity dev.
704 */
705 dev = si->par_dev;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200706 }
707 if (ios->sp2d)
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200708 /* 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;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200732 si->cur_pg = 0;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200733 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200734 }
735out:
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300736 ios->numdevs = devs_in_group;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200737 ios->pages_consumed = cur_pg;
Boaz Harrosh62b62ad2012-06-08 04:30:40 +0300738 return ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200739}
740
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700741int ore_create(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200742{
743 int i, ret;
744
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300745 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200746 struct osd_request *or;
747
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700748 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200749 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700750 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200751 ret = -ENOMEM;
752 goto out;
753 }
754 ios->per_dev[i].or = or;
755 ios->numdevs++;
756
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700757 osd_req_create_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200758 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700759 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200760
761out:
762 return ret;
763}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700764EXPORT_SYMBOL(ore_create);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200765
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700766int ore_remove(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200767{
768 int i, ret;
769
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300770 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200771 struct osd_request *or;
772
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700773 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200774 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700775 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200776 ret = -ENOMEM;
777 goto out;
778 }
779 ios->per_dev[i].or = or;
780 ios->numdevs++;
781
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700782 osd_req_remove_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200783 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700784 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200785
786out:
787 return ret;
788}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700789EXPORT_SYMBOL(ore_remove);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200790
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700791static int _write_mirror(struct ore_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200792{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700793 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200794 unsigned dev = ios->per_dev[cur_comp].dev;
795 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
796 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200797
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200798 if (ios->pages && !master_dev->length)
799 return 0; /* Just an empty slot */
800
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200801 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700802 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200803 struct osd_request *or;
804
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700805 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200806 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700807 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200808 ret = -ENOMEM;
809 goto out;
810 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200811 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200812
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200813 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200814 struct bio *bio;
815
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200816 if (per_dev != master_dev) {
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700817 bio = bio_clone_kmalloc(master_dev->bio,
818 GFP_KERNEL);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200819 if (unlikely(!bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700820 ORE_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200821 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200822 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200823 ret = -ENOMEM;
824 goto out;
825 }
826
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200827 bio->bi_bdev = NULL;
828 bio->bi_next = NULL;
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700829 per_dev->offset = master_dev->offset;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200830 per_dev->length = master_dev->length;
831 per_dev->bio = bio;
832 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200833 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200834 bio = master_dev->bio;
835 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200836 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200837 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200838
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300839 osd_req_write(or, _ios_obj(ios, cur_comp),
840 per_dev->offset, bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700841 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200842 "length=0x%llx dev=%d\n",
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300843 _LLU(_ios_obj(ios, cur_comp)->id),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700844 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200845 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200846 } else if (ios->kern_buff) {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700847 per_dev->offset = ios->si.obj_offset;
848 per_dev->dev = ios->si.dev + dev;
849
850 /* no cross device without page array */
851 BUG_ON((ios->layout->group_width > 1) &&
852 (ios->si.unit_off + ios->length >
853 ios->layout->stripe_unit));
854
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300855 ret = osd_req_write_kern(or, _ios_obj(ios, cur_comp),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700856 per_dev->offset,
857 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200858 if (unlikely(ret))
859 goto out;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700860 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200861 "length=0x%llx dev=%d\n",
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300862 _LLU(_ios_obj(ios, cur_comp)->id),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700863 _LLU(per_dev->offset),
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700864 _LLU(ios->length), per_dev->dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200865 } else {
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300866 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700867 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300868 _LLU(_ios_obj(ios, cur_comp)->id),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700869 ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200870 }
871
872 if (ios->out_attr)
873 osd_req_add_set_attr_list(or, ios->out_attr,
874 ios->out_attr_len);
875
876 if (ios->in_attr)
877 osd_req_add_get_attr_list(or, ios->in_attr,
878 ios->in_attr_len);
879 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200880
881out:
882 return ret;
883}
884
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700885int ore_write(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200886{
887 int i;
888 int ret;
889
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200890 if (unlikely(ios->sp2d && !ios->r4w)) {
891 /* A library is attempting a RAID-write without providing
892 * a pages lock interface.
893 */
894 WARN_ON_ONCE(1);
895 return -ENOTSUPP;
896 }
897
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200898 ret = _prepare_for_striping(ios);
899 if (unlikely(ret))
900 return ret;
901
902 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700903 ret = _write_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200904 if (unlikely(ret))
905 return ret;
906 }
907
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700908 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200909 return ret;
910}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700911EXPORT_SYMBOL(ore_write);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200912
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200913int _ore_read_mirror(struct ore_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200914{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200915 struct osd_request *or;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700916 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700917 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
918 unsigned first_dev = (unsigned)obj->id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200919
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200920 if (ios->pages && !per_dev->length)
921 return 0; /* Just an empty slot */
922
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200923 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700924 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200925 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700926 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200927 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200928 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200929 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200930
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200931 if (ios->pages) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200932 if (per_dev->cur_sg) {
933 /* finalize the last sg_entry */
934 _ore_add_sg_seg(per_dev, 0, false);
935 if (unlikely(!per_dev->cur_sg))
936 return 0; /* Skip parity only device */
937
938 osd_req_read_sg(or, obj, per_dev->bio,
939 per_dev->sglist, per_dev->cur_sg);
940 } else {
941 /* The no raid case */
942 osd_req_read(or, obj, per_dev->offset,
943 per_dev->bio, per_dev->length);
944 }
945
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700946 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200947 " dev=%d sg_len=%d\n", _LLU(obj->id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200948 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200949 first_dev, per_dev->cur_sg);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200950 } else {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700951 BUG_ON(ios->kern_buff);
952
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700953 osd_req_get_attributes(or, obj);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700954 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700955 _LLU(obj->id),
956 ios->in_attr_len, first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200957 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200958 if (ios->out_attr)
959 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
960
961 if (ios->in_attr)
962 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
963
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200964 return 0;
965}
966
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700967int ore_read(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200968{
969 int i;
970 int ret;
971
972 ret = _prepare_for_striping(ios);
973 if (unlikely(ret))
974 return ret;
975
976 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200977 ret = _ore_read_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200978 if (unlikely(ret))
979 return ret;
980 }
981
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700982 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200983 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200984}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700985EXPORT_SYMBOL(ore_read);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200986
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700987int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200988{
989 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
990 void *iter = NULL;
991 int nelem;
992
993 do {
994 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200995 osd_req_decode_get_attr_list(ios->per_dev[0].or,
996 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200997 if ((cur_attr.attr_page == attr->attr_page) &&
998 (cur_attr.attr_id == attr->attr_id)) {
999 attr->len = cur_attr.len;
1000 attr->val_ptr = cur_attr.val_ptr;
1001 return 0;
1002 }
1003 } while (iter);
1004
1005 return -EIO;
1006}
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001007EXPORT_SYMBOL(extract_attr_from_ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001008
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001009static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001010 struct osd_attr *attr)
1011{
1012 int last_comp = cur_comp + ios->layout->mirrors_p1;
1013
1014 for (; cur_comp < last_comp; ++cur_comp) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001015 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001016 struct osd_request *or;
1017
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001018 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001019 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001020 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001021 return -ENOMEM;
1022 }
1023 per_dev->or = or;
1024
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001025 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001026 osd_req_add_set_attr_list(or, attr, 1);
1027 }
1028
1029 return 0;
1030}
1031
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001032struct _trunc_info {
Boaz Harrosheb507bc2011-08-10 14:17:28 -07001033 struct ore_striping_info si;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001034 u64 prev_group_obj_off;
1035 u64 next_group_obj_off;
1036
1037 unsigned first_group_dev;
1038 unsigned nex_group_dev;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001039};
1040
H Hartley Sweeten1958c7c22011-09-23 13:42:43 -07001041static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
1042 struct _trunc_info *ti)
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001043{
1044 unsigned stripe_unit = layout->stripe_unit;
1045
Boaz Harrosha1fec1d2011-10-12 18:42:22 +02001046 ore_calc_stripe_info(layout, file_offset, 0, &ti->si);
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001047
1048 ti->prev_group_obj_off = ti->si.M * stripe_unit;
1049 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
1050
1051 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
1052 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001053}
1054
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001055int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001056 u64 size)
Boaz Harrosh06886a52009-11-08 14:54:08 +02001057{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001058 struct ore_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001059 struct exofs_trunc_attr {
1060 struct osd_attr attr;
1061 __be64 newsize;
1062 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001063 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001064 int i, ret;
1065
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001066 ret = ore_get_io_state(layout, oc, &ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001067 if (unlikely(ret))
1068 return ret;
1069
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001070 _calc_trunk_info(ios->layout, size, &ti);
1071
Boaz Harroshb916c5c2011-09-28 11:55:51 +03001072 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001073 GFP_KERNEL);
1074 if (unlikely(!size_attrs)) {
1075 ret = -ENOMEM;
1076 goto out;
1077 }
Boaz Harrosh06886a52009-11-08 14:54:08 +02001078
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001079 ios->numdevs = ios->oc->numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001080
Boaz Harroshb916c5c2011-09-28 11:55:51 +03001081 for (i = 0; i < ios->numdevs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001082 struct exofs_trunc_attr *size_attr = &size_attrs[i];
1083 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001084
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001085 if (i < ti.first_group_dev)
1086 obj_size = ti.prev_group_obj_off;
1087 else if (i >= ti.nex_group_dev)
1088 obj_size = ti.next_group_obj_off;
1089 else if (i < ti.si.dev) /* dev within this group */
1090 obj_size = ti.si.obj_offset +
1091 ios->layout->stripe_unit - ti.si.unit_off;
1092 else if (i == ti.si.dev)
1093 obj_size = ti.si.obj_offset;
1094 else /* i > ti.dev */
1095 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001096
1097 size_attr->newsize = cpu_to_be64(obj_size);
1098 size_attr->attr = g_attr_logical_length;
1099 size_attr->attr.val_ptr = &size_attr->newsize;
1100
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001101 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001102 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001103 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
1104 &size_attr->attr);
1105 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +02001106 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001107 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001108 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001109
1110out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001111 kfree(size_attrs);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001112 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001113 return ret;
1114}
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001115EXPORT_SYMBOL(ore_truncate);
Boaz Harrosh85e44df2011-05-16 15:26:47 +03001116
1117const struct osd_attr g_attr_logical_length = ATTR_DEF(
1118 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001119EXPORT_SYMBOL(g_attr_logical_length);