blob: fcfa86ae6faf0227a37ab4e6b6305b62c90ef2fc [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>
Boaz Harrosh5d952b82010-02-01 13:35:51 +020026#include <asm/div64.h>
Boaz Harrosha1fec1d2011-10-12 18:42:22 +020027#include <linux/lcm.h>
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020028
Boaz Harrosha1fec1d2011-10-12 18:42:22 +020029#include "ore_raid.h"
Boaz Harrosh8ff660a2011-08-06 19:26:31 -070030
Boaz Harroshcf283ad2011-08-06 19:22:06 -070031MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
32MODULE_DESCRIPTION("Objects Raid Engine ore.ko");
33MODULE_LICENSE("GPL");
34
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030035/* ore_verify_layout does a couple of things:
36 * 1. Given a minimum number of needed parameters fixes up the rest of the
37 * members to be operatonals for the ore. The needed parameters are those
38 * that are defined by the pnfs-objects layout STD.
39 * 2. Check to see if the current ore code actually supports these parameters
40 * for example stripe_unit must be a multple of the system PAGE_SIZE,
41 * and etc...
42 * 3. Cache some havily used calculations that will be needed by users.
43 */
44
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030045enum { BIO_MAX_PAGES_KMALLOC =
46 (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),};
47
48int ore_verify_layout(unsigned total_comps, struct ore_layout *layout)
49{
50 u64 stripe_length;
51
Boaz Harrosh44231e62010-11-21 20:03:24 +020052 switch (layout->raid_algorithm) {
53 case PNFS_OSD_RAID_0:
54 layout->parity = 0;
55 break;
56 case PNFS_OSD_RAID_5:
57 layout->parity = 1;
58 break;
59 case PNFS_OSD_RAID_PQ:
60 case PNFS_OSD_RAID_4:
61 default:
62 ORE_ERR("Only RAID_0/5 for now\n");
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030063 return -EINVAL;
64 }
65 if (0 != (layout->stripe_unit & ~PAGE_MASK)) {
66 ORE_ERR("Stripe Unit(0x%llx)"
67 " must be Multples of PAGE_SIZE(0x%lx)\n",
68 _LLU(layout->stripe_unit), PAGE_SIZE);
69 return -EINVAL;
70 }
71 if (layout->group_width) {
72 if (!layout->group_depth) {
73 ORE_ERR("group_depth == 0 && group_width != 0\n");
74 return -EINVAL;
75 }
76 if (total_comps < (layout->group_width * layout->mirrors_p1)) {
77 ORE_ERR("Data Map wrong, "
78 "numdevs=%d < group_width=%d * mirrors=%d\n",
79 total_comps, layout->group_width,
80 layout->mirrors_p1);
81 return -EINVAL;
82 }
83 layout->group_count = total_comps / layout->mirrors_p1 /
84 layout->group_width;
85 } else {
86 if (layout->group_depth) {
87 printk(KERN_NOTICE "Warning: group_depth ignored "
88 "group_width == 0 && group_depth == %lld\n",
89 _LLU(layout->group_depth));
90 }
91 layout->group_width = total_comps / layout->mirrors_p1;
92 layout->group_depth = -1;
93 layout->group_count = 1;
94 }
95
96 stripe_length = (u64)layout->group_width * layout->stripe_unit;
97 if (stripe_length >= (1ULL << 32)) {
98 ORE_ERR("Stripe_length(0x%llx) >= 32bit is not supported\n",
99 _LLU(stripe_length));
100 return -EINVAL;
101 }
102
103 layout->max_io_length =
104 (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE - layout->stripe_unit) *
105 layout->group_width;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200106 if (layout->parity) {
107 unsigned stripe_length =
108 (layout->group_width - layout->parity) *
109 layout->stripe_unit;
110
111 layout->max_io_length /= stripe_length;
112 layout->max_io_length *= stripe_length;
113 }
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +0300114 return 0;
115}
116EXPORT_SYMBOL(ore_verify_layout);
117
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700118static u8 *_ios_cred(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700119{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300120 return ios->oc->comps[index & ios->oc->single_comp].cred;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700121}
122
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700123static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700124{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300125 return &ios->oc->comps[index & ios->oc->single_comp].obj;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700126}
127
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700128static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700129{
Boaz Harrosh3bd98562011-09-28 12:04:23 +0300130 ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n",
131 ios->oc->first_dev, ios->oc->numdevs, index,
132 ios->oc->ods);
133
Boaz Harroshd866d872011-09-28 14:43:09 +0300134 return ore_comp_dev(ios->oc, index);
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700135}
136
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200137int _ore_get_io_state(struct ore_layout *layout,
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200138 struct ore_components *oc, unsigned numdevs,
139 unsigned sgs_per_dev, unsigned num_par_pages,
140 struct ore_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200141{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700142 struct ore_io_state *ios;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200143 struct page **pages;
144 struct osd_sg_entry *sgilist;
145 struct __alloc_all_io_state {
146 struct ore_io_state ios;
147 struct ore_per_dev_state per_dev[numdevs];
148 union {
149 struct osd_sg_entry sglist[sgs_per_dev * numdevs];
150 struct page *pages[num_par_pages];
151 };
152 } *_aios;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200153
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200154 if (likely(sizeof(*_aios) <= PAGE_SIZE)) {
155 _aios = kzalloc(sizeof(*_aios), GFP_KERNEL);
156 if (unlikely(!_aios)) {
157 ORE_DBGMSG("Failed kzalloc bytes=%zd\n",
158 sizeof(*_aios));
159 *pios = NULL;
160 return -ENOMEM;
161 }
162 pages = num_par_pages ? _aios->pages : NULL;
163 sgilist = sgs_per_dev ? _aios->sglist : NULL;
164 ios = &_aios->ios;
165 } else {
166 struct __alloc_small_io_state {
167 struct ore_io_state ios;
168 struct ore_per_dev_state per_dev[numdevs];
169 } *_aio_small;
170 union __extra_part {
171 struct osd_sg_entry sglist[sgs_per_dev * numdevs];
172 struct page *pages[num_par_pages];
173 } *extra_part;
174
175 _aio_small = kzalloc(sizeof(*_aio_small), GFP_KERNEL);
176 if (unlikely(!_aio_small)) {
177 ORE_DBGMSG("Failed alloc first part bytes=%zd\n",
178 sizeof(*_aio_small));
179 *pios = NULL;
180 return -ENOMEM;
181 }
182 extra_part = kzalloc(sizeof(*extra_part), GFP_KERNEL);
183 if (unlikely(!extra_part)) {
184 ORE_DBGMSG("Failed alloc second part bytes=%zd\n",
185 sizeof(*extra_part));
186 kfree(_aio_small);
187 *pios = NULL;
188 return -ENOMEM;
189 }
190
191 pages = num_par_pages ? extra_part->pages : NULL;
192 sgilist = sgs_per_dev ? extra_part->sglist : NULL;
193 /* In this case the per_dev[0].sgilist holds the pointer to
194 * be freed
195 */
196 ios = &_aio_small->ios;
197 ios->extra_part_alloc = true;
198 }
199
200 if (pages) {
201 ios->parity_pages = pages;
202 ios->max_par_pages = num_par_pages;
203 }
204 if (sgilist) {
205 unsigned d;
206
207 for (d = 0; d < numdevs; ++d) {
208 ios->per_dev[d].sglist = sgilist;
209 sgilist += sgs_per_dev;
210 }
211 ios->sgs_per_dev = sgs_per_dev;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200212 }
213
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200214 ios->layout = layout;
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300215 ios->oc = oc;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200216 *pios = ios;
217 return 0;
218}
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300219
220/* Allocate an io_state for only a single group of devices
221 *
222 * If a user needs to call ore_read/write() this version must be used becase it
223 * allocates extra stuff for striping and raid.
224 * The ore might decide to only IO less then @length bytes do to alignmets
225 * and constrains as follows:
226 * - The IO cannot cross group boundary.
227 * - In raid5/6 The end of the IO must align at end of a stripe eg.
228 * (@offset + @length) % strip_size == 0. Or the complete range is within a
229 * single stripe.
230 * - Memory condition only permitted a shorter IO. (A user can use @length=~0
231 * And check the returned ios->length for max_io_size.)
232 *
233 * The caller must check returned ios->length (and/or ios->nr_pages) and
234 * re-issue these pages that fall outside of ios->length
235 */
236int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
237 bool is_reading, u64 offset, u64 length,
238 struct ore_io_state **pios)
239{
240 struct ore_io_state *ios;
241 unsigned numdevs = layout->group_width * layout->mirrors_p1;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200242 unsigned sgs_per_dev = 0, max_par_pages = 0;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300243 int ret;
244
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200245 if (layout->parity && length) {
246 unsigned data_devs = layout->group_width - layout->parity;
247 unsigned stripe_size = layout->stripe_unit * data_devs;
248 unsigned pages_in_unit = layout->stripe_unit / PAGE_SIZE;
249 u32 remainder;
250 u64 num_stripes;
251 u64 num_raid_units;
252
253 num_stripes = div_u64_rem(length, stripe_size, &remainder);
254 if (remainder)
255 ++num_stripes;
256
257 num_raid_units = num_stripes * layout->parity;
258
259 if (is_reading) {
260 /* For reads add per_dev sglist array */
261 /* TODO: Raid 6 we need twice more. Actually:
262 * num_stripes / LCMdP(W,P);
263 * if (W%P != 0) num_stripes *= parity;
264 */
265
266 /* first/last seg is split */
267 num_raid_units += layout->group_width;
268 sgs_per_dev = div_u64(num_raid_units, data_devs);
269 } else {
270 /* For Writes add parity pages array. */
271 max_par_pages = num_raid_units * pages_in_unit *
272 sizeof(struct page *);
273 }
274 }
275
276 ret = _ore_get_io_state(layout, oc, numdevs, sgs_per_dev, max_par_pages,
277 pios);
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300278 if (unlikely(ret))
279 return ret;
280
281 ios = *pios;
282 ios->reading = is_reading;
283 ios->offset = offset;
284
285 if (length) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200286 ore_calc_stripe_info(layout, offset, length, &ios->si);
287 ios->length = ios->si.length;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300288 ios->nr_pages = (ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200289 if (layout->parity)
290 _ore_post_alloc_raid_stuff(ios);
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300291 }
292
293 return 0;
294}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700295EXPORT_SYMBOL(ore_get_rw_state);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200296
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300297/* Allocate an io_state for all the devices in the comps array
298 *
299 * This version of io_state allocation is used mostly by create/remove
300 * and trunc where we currently need all the devices. The only wastful
301 * bit is the read/write_attributes with no IO. Those sites should
302 * be converted to use ore_get_rw_state() with length=0
303 */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300304int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300305 struct ore_io_state **pios)
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200306{
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200307 return _ore_get_io_state(layout, oc, oc->numdevs, 0, 0, pios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200308}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700309EXPORT_SYMBOL(ore_get_io_state);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200310
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700311void ore_put_io_state(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200312{
313 if (ios) {
314 unsigned i;
315
316 for (i = 0; i < ios->numdevs; i++) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700317 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200318
319 if (per_dev->or)
320 osd_end_request(per_dev->or);
321 if (per_dev->bio)
322 bio_put(per_dev->bio);
323 }
324
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200325 _ore_free_raid_stuff(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200326 kfree(ios);
327 }
328}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700329EXPORT_SYMBOL(ore_put_io_state);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200330
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700331static void _sync_done(struct ore_io_state *ios, void *p)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200332{
333 struct completion *waiting = p;
334
335 complete(waiting);
336}
337
338static void _last_io(struct kref *kref)
339{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700340 struct ore_io_state *ios = container_of(
341 kref, struct ore_io_state, kref);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200342
343 ios->done(ios, ios->private);
344}
345
346static void _done_io(struct osd_request *or, void *p)
347{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700348 struct ore_io_state *ios = p;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200349
350 kref_put(&ios->kref, _last_io);
351}
352
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200353int ore_io_execute(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200354{
355 DECLARE_COMPLETION_ONSTACK(wait);
356 bool sync = (ios->done == NULL);
357 int i, ret;
358
359 if (sync) {
360 ios->done = _sync_done;
361 ios->private = &wait;
362 }
363
364 for (i = 0; i < ios->numdevs; i++) {
365 struct osd_request *or = ios->per_dev[i].or;
366 if (unlikely(!or))
367 continue;
368
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700369 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200370 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700371 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200372 ret);
373 return ret;
374 }
375 }
376
377 kref_init(&ios->kref);
378
379 for (i = 0; i < ios->numdevs; i++) {
380 struct osd_request *or = ios->per_dev[i].or;
381 if (unlikely(!or))
382 continue;
383
384 kref_get(&ios->kref);
385 osd_execute_request_async(or, _done_io, ios);
386 }
387
388 kref_put(&ios->kref, _last_io);
389 ret = 0;
390
391 if (sync) {
392 wait_for_completion(&wait);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700393 ret = ore_check_io(ios, NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200394 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200395 return ret;
396}
397
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200398static void _clear_bio(struct bio *bio)
399{
400 struct bio_vec *bv;
401 unsigned i;
402
403 __bio_for_each_segment(bv, bio, i, 0) {
404 unsigned this_count = bv->bv_len;
405
406 if (likely(PAGE_SIZE == this_count))
407 clear_highpage(bv->bv_page);
408 else
409 zero_user(bv->bv_page, bv->bv_offset, this_count);
410 }
411}
412
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300413int ore_check_io(struct ore_io_state *ios, ore_on_dev_error on_dev_error)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200414{
415 enum osd_err_priority acumulated_osd_err = 0;
416 int acumulated_lin_err = 0;
417 int i;
418
419 for (i = 0; i < ios->numdevs; i++) {
420 struct osd_sense_info osi;
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300421 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
422 struct osd_request *or = per_dev->or;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200423 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200424
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200425 if (unlikely(!or))
426 continue;
427
428 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200429 if (likely(!ret))
430 continue;
431
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200432 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
433 /* start read offset passed endof file */
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300434 _clear_bio(per_dev->bio);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700435 ORE_DBGMSG("start read offset passed end of file "
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200436 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300437 _LLU(per_dev->offset),
438 _LLU(per_dev->length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200439
440 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200441 }
442
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300443 if (on_dev_error) {
444 u64 residual = ios->reading ?
445 or->in.residual : or->out.residual;
446 u64 offset = (ios->offset + ios->length) - residual;
447 struct ore_dev *od = ios->oc->ods[
448 per_dev->dev - ios->oc->first_dev];
449
450 on_dev_error(ios, od, per_dev->dev, osi.osd_err_pri,
451 offset, residual);
452 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200453 if (osi.osd_err_pri >= acumulated_osd_err) {
454 acumulated_osd_err = osi.osd_err_pri;
455 acumulated_lin_err = ret;
456 }
457 }
458
Boaz Harrosh06886a52009-11-08 14:54:08 +0200459 return acumulated_lin_err;
460}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700461EXPORT_SYMBOL(ore_check_io);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200462
Boaz Harroshb367e782010-02-07 19:18:58 +0200463/*
464 * L - logical offset into the file
465 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200466 * D - number of Data devices
467 * D = group_width - parity
Boaz Harroshb367e782010-02-07 19:18:58 +0200468 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200469 * U - The number of bytes in a stripe within a group
470 * U = stripe_unit * D
Boaz Harroshb367e782010-02-07 19:18:58 +0200471 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200472 * T - The number of bytes striped within a group of component objects
473 * (before advancing to the next group)
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200474 * T = U * group_depth
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200475 *
476 * S - The number of bytes striped across all component objects
477 * before the pattern repeats
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200478 * S = T * group_count
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200479 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200480 * M - The "major" (i.e., across all components) cycle number
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200481 * M = L / S
482 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200483 * G - Counts the groups from the beginning of the major cycle
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200484 * G = (L - (M * S)) / T [or (L % S) / T]
485 *
486 * H - The byte offset within the group
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200487 * H = (L - (M * S)) % T [or (L % S) % T]
488 *
489 * N - The "minor" (i.e., across the group) stripe number
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200490 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200491 *
492 * C - The component index coresponding to L
493 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200494 * C = (H - (N * U)) / stripe_unit + G * D
495 * [or (L % U) / stripe_unit + G * D]
Boaz Harroshb367e782010-02-07 19:18:58 +0200496 *
497 * O - The component offset coresponding to L
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200498 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200499 *
500 * LCMdP – Parity cycle: Lowest Common Multiple of group_width, parity
501 * divide by parity
502 * LCMdP = lcm(group_width, parity) / parity
503 *
504 * R - The parity Rotation stripe
505 * (Note parity cycle always starts at a group's boundary)
506 * R = N % LCMdP
507 *
508 * I = the first parity device index
509 * I = (group_width + group_width - R*parity - parity) % group_width
510 *
511 * Craid - The component index Rotated
512 * Craid = (group_width + C - R*parity) % group_width
513 * (We add the group_width to avoid negative numbers modulo math)
Boaz Harroshb367e782010-02-07 19:18:58 +0200514 */
Boaz Harrosh611d7a52011-10-04 14:20:17 +0200515void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200516 u64 length, struct ore_striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200517{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700518 u32 stripe_unit = layout->stripe_unit;
519 u32 group_width = layout->group_width;
520 u64 group_depth = layout->group_depth;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200521 u32 parity = layout->parity;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200522
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200523 u32 D = group_width - parity;
524 u32 U = D * stripe_unit;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200525 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700526 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200527 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200528
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200529 /*
530 G = (L - (M * S)) / T
531 H = (L - (M * S)) % T
532 */
533 u64 LmodS = file_offset - M * S;
534 u32 G = div64_u64(LmodS, T);
535 u64 H = LmodS - G * T;
Boaz Harroshb367e782010-02-07 19:18:58 +0200536
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200537 u32 N = div_u64(H, U);
538
539 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200540 u32 C = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200541
542 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
543
544 si->obj_offset = si->unit_off + (N * stripe_unit) +
545 (M * group_depth * stripe_unit);
546
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200547 if (parity) {
548 u32 LCMdP = lcm(group_width, parity) / parity;
549 /* R = N % LCMdP; */
550 u32 RxP = (N % LCMdP) * parity;
551 u32 first_dev = C - C % group_width;
552
553 si->par_dev = (group_width + group_width - parity - RxP) %
554 group_width + first_dev;
555 si->dev = (group_width + C - RxP) % group_width + first_dev;
556 si->bytes_in_stripe = U;
557 si->first_stripe_start = M * S + G * T + N * U;
558 } else {
559 /* Make the math correct see _prepare_one_group */
560 si->par_dev = group_width;
561 si->dev = C;
562 }
563
564 si->dev *= layout->mirrors_p1;
565 si->par_dev *= layout->mirrors_p1;
566 si->offset = file_offset;
567 si->length = T - H;
568 if (si->length > length)
569 si->length = length;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700570 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200571}
Boaz Harrosh611d7a52011-10-04 14:20:17 +0200572EXPORT_SYMBOL(ore_calc_stripe_info);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200573
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200574int _ore_add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
575 unsigned pgbase, struct page **pages,
576 struct ore_per_dev_state *per_dev, int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200577{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200578 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200579 struct request_queue *q =
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700580 osd_request_queue(_ios_od(ios, per_dev->dev));
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700581 unsigned len = cur_len;
582 int ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200583
584 if (per_dev->bio == NULL) {
585 unsigned pages_in_stripe = ios->layout->group_width *
586 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200587 unsigned nr_pages = ios->nr_pages * ios->layout->group_width /
588 (ios->layout->group_width -
589 ios->layout->parity);
590 unsigned bio_size = (nr_pages + pages_in_stripe) /
591 ios->layout->group_width;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200592
593 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
594 if (unlikely(!per_dev->bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700595 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200596 bio_size);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700597 ret = -ENOMEM;
598 goto out;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200599 }
600 }
601
602 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200603 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
604 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200605
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200606 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200607
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200608 added_len = bio_add_pc_page(q, per_dev->bio, pages[pg],
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200609 pglen, pgbase);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700610 if (unlikely(pglen != added_len)) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200611 ORE_DBGMSG("Failed bio_add_pc_page bi_vcnt=%u\n",
612 per_dev->bio->bi_vcnt);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700613 ret = -ENOMEM;
614 goto out;
615 }
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200616 _add_stripe_page(ios->sp2d, &ios->si, pages[pg]);
617
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200618 pgbase = 0;
619 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200620 }
621 BUG_ON(cur_len);
622
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700623 per_dev->length += len;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200624 *cur_pg = pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700625 ret = 0;
626out: /* we fail the complete unit on an error eg don't advance
627 * per_dev->length and cur_pg. This means that we might have a bigger
628 * bio than the CDB requested length (per_dev->length). That's fine
629 * only the oposite is fatal.
630 */
631 return ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200632}
633
Boaz Harrosh98260752011-10-02 15:32:50 +0200634static int _prepare_for_striping(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200635{
Boaz Harrosh98260752011-10-02 15:32:50 +0200636 struct ore_striping_info *si = &ios->si;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200637 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200638 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200639 unsigned group_width = ios->layout->group_width;
640 unsigned devs_in_group = group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200641 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200642 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200643 unsigned dev_order;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200644 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh98260752011-10-02 15:32:50 +0200645 u64 length = ios->length;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200646 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200647
Boaz Harrosh98260752011-10-02 15:32:50 +0200648 if (!ios->pages) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200649 ios->numdevs = ios->layout->mirrors_p1;
650 return 0;
651 }
652
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200653 BUG_ON(length > si->length);
654
655 dev_order = _dev_order(devs_in_group, mirrors_p1, si->par_dev, dev);
656 si->cur_comp = dev_order;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200657 si->cur_pg = si->unit_off / PAGE_SIZE;
Boaz Harrosh98260752011-10-02 15:32:50 +0200658
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200659 while (length) {
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300660 unsigned comp = dev - first_dev;
661 struct ore_per_dev_state *per_dev = &ios->per_dev[comp];
Boaz Harroshb367e782010-02-07 19:18:58 +0200662 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200663
664 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200665 per_dev->dev = dev;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200666 if (dev == si->dev) {
667 WARN_ON(dev == si->par_dev);
Boaz Harroshb367e782010-02-07 19:18:58 +0200668 per_dev->offset = si->obj_offset;
669 cur_len = stripe_unit - si->unit_off;
670 page_off = si->unit_off & ~PAGE_MASK;
671 BUG_ON(page_off && (page_off != ios->pgbase));
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200672 } else {
673 if (si->cur_comp > dev_order)
674 per_dev->offset =
675 si->obj_offset - si->unit_off;
676 else /* si->cur_comp < dev_order */
677 per_dev->offset =
678 si->obj_offset + stripe_unit -
679 si->unit_off;
Boaz Harroshb367e782010-02-07 19:18:58 +0200680 cur_len = stripe_unit;
681 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200682 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200683 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200684 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200685 if (cur_len >= length)
686 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200687
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200688 ret = _ore_add_stripe_unit(ios, &cur_pg, page_off, ios->pages,
689 per_dev, cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200690 if (unlikely(ret))
691 goto out;
692
Boaz Harrosh6e316092010-07-29 17:08:13 +0300693 dev += mirrors_p1;
694 dev = (dev % devs_in_group) + first_dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200695
696 length -= cur_len;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200697
698 si->cur_comp = (si->cur_comp + 1) % group_width;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200699 if (unlikely((dev == si->par_dev) || (!length && ios->sp2d))) {
700 if (!length && ios->sp2d) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200701 /* If we are writing and this is the very last
702 * stripe. then operate on parity dev.
703 */
704 dev = si->par_dev;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200705 }
706 if (ios->sp2d)
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200707 /* In writes cur_len just means if it's the
708 * last one. See _ore_add_parity_unit.
709 */
710 cur_len = length;
711 per_dev = &ios->per_dev[dev - first_dev];
712 if (!per_dev->length) {
713 /* Only/always the parity unit of the first
714 * stripe will be empty. So this is a chance to
715 * initialize the per_dev info.
716 */
717 per_dev->dev = dev;
718 per_dev->offset = si->obj_offset - si->unit_off;
719 }
720
721 ret = _ore_add_parity_unit(ios, si, per_dev, cur_len);
722 if (unlikely(ret))
723 goto out;
724
725 /* Rotate next par_dev backwards with wraping */
726 si->par_dev = (devs_in_group + si->par_dev -
727 ios->layout->parity * mirrors_p1) %
728 devs_in_group + first_dev;
729 /* Next stripe, start fresh */
730 si->cur_comp = 0;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200731 si->cur_pg = 0;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200732 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200733 }
734out:
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300735 ios->numdevs = devs_in_group;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200736 ios->pages_consumed = cur_pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700737 if (unlikely(ret)) {
738 if (length == ios->length)
739 return ret;
740 else
741 ios->length -= length;
742 }
743 return 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200744}
745
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700746int ore_create(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200747{
748 int i, ret;
749
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300750 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200751 struct osd_request *or;
752
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700753 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200754 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700755 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200756 ret = -ENOMEM;
757 goto out;
758 }
759 ios->per_dev[i].or = or;
760 ios->numdevs++;
761
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700762 osd_req_create_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200763 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700764 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200765
766out:
767 return ret;
768}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700769EXPORT_SYMBOL(ore_create);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200770
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700771int ore_remove(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200772{
773 int i, ret;
774
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300775 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200776 struct osd_request *or;
777
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700778 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200779 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700780 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200781 ret = -ENOMEM;
782 goto out;
783 }
784 ios->per_dev[i].or = or;
785 ios->numdevs++;
786
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700787 osd_req_remove_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200788 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700789 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200790
791out:
792 return ret;
793}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700794EXPORT_SYMBOL(ore_remove);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200795
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700796static int _write_mirror(struct ore_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200797{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700798 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200799 unsigned dev = ios->per_dev[cur_comp].dev;
800 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
801 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200802
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200803 if (ios->pages && !master_dev->length)
804 return 0; /* Just an empty slot */
805
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200806 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700807 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200808 struct osd_request *or;
809
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700810 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200811 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700812 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200813 ret = -ENOMEM;
814 goto out;
815 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200816 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200817
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200818 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200819 struct bio *bio;
820
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200821 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200822 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200823 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200824 if (unlikely(!bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700825 ORE_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200826 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200827 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200828 ret = -ENOMEM;
829 goto out;
830 }
831
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200832 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200833 bio->bi_bdev = NULL;
834 bio->bi_next = NULL;
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700835 per_dev->offset = master_dev->offset;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200836 per_dev->length = master_dev->length;
837 per_dev->bio = bio;
838 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200839 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200840 bio = master_dev->bio;
841 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200842 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200843 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200844
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700845 osd_req_write(or, _ios_obj(ios, dev), per_dev->offset,
846 bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700847 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200848 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700849 _LLU(_ios_obj(ios, dev)->id),
850 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200851 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200852 } else if (ios->kern_buff) {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700853 per_dev->offset = ios->si.obj_offset;
854 per_dev->dev = ios->si.dev + dev;
855
856 /* no cross device without page array */
857 BUG_ON((ios->layout->group_width > 1) &&
858 (ios->si.unit_off + ios->length >
859 ios->layout->stripe_unit));
860
861 ret = osd_req_write_kern(or, _ios_obj(ios, per_dev->dev),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700862 per_dev->offset,
863 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200864 if (unlikely(ret))
865 goto out;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700866 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200867 "length=0x%llx dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700868 _LLU(_ios_obj(ios, dev)->id),
869 _LLU(per_dev->offset),
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700870 _LLU(ios->length), per_dev->dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200871 } else {
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700872 osd_req_set_attributes(or, _ios_obj(ios, dev));
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700873 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700874 _LLU(_ios_obj(ios, dev)->id),
875 ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200876 }
877
878 if (ios->out_attr)
879 osd_req_add_set_attr_list(or, ios->out_attr,
880 ios->out_attr_len);
881
882 if (ios->in_attr)
883 osd_req_add_get_attr_list(or, ios->in_attr,
884 ios->in_attr_len);
885 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200886
887out:
888 return ret;
889}
890
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700891int ore_write(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200892{
893 int i;
894 int ret;
895
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200896 if (unlikely(ios->sp2d && !ios->r4w)) {
897 /* A library is attempting a RAID-write without providing
898 * a pages lock interface.
899 */
900 WARN_ON_ONCE(1);
901 return -ENOTSUPP;
902 }
903
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200904 ret = _prepare_for_striping(ios);
905 if (unlikely(ret))
906 return ret;
907
908 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700909 ret = _write_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200910 if (unlikely(ret))
911 return ret;
912 }
913
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700914 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200915 return ret;
916}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700917EXPORT_SYMBOL(ore_write);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200918
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200919int _ore_read_mirror(struct ore_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200920{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200921 struct osd_request *or;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700922 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700923 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
924 unsigned first_dev = (unsigned)obj->id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200925
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200926 if (ios->pages && !per_dev->length)
927 return 0; /* Just an empty slot */
928
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200929 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700930 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200931 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700932 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200933 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200934 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200935 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200936
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200937 if (ios->pages) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200938 if (per_dev->cur_sg) {
939 /* finalize the last sg_entry */
940 _ore_add_sg_seg(per_dev, 0, false);
941 if (unlikely(!per_dev->cur_sg))
942 return 0; /* Skip parity only device */
943
944 osd_req_read_sg(or, obj, per_dev->bio,
945 per_dev->sglist, per_dev->cur_sg);
946 } else {
947 /* The no raid case */
948 osd_req_read(or, obj, per_dev->offset,
949 per_dev->bio, per_dev->length);
950 }
951
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700952 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200953 " dev=%d sg_len=%d\n", _LLU(obj->id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200954 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200955 first_dev, per_dev->cur_sg);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200956 } else {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700957 BUG_ON(ios->kern_buff);
958
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700959 osd_req_get_attributes(or, obj);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700960 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700961 _LLU(obj->id),
962 ios->in_attr_len, first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200963 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200964 if (ios->out_attr)
965 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
966
967 if (ios->in_attr)
968 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
969
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200970 return 0;
971}
972
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700973int ore_read(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200974{
975 int i;
976 int ret;
977
978 ret = _prepare_for_striping(ios);
979 if (unlikely(ret))
980 return ret;
981
982 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200983 ret = _ore_read_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200984 if (unlikely(ret))
985 return ret;
986 }
987
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700988 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200989 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200990}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700991EXPORT_SYMBOL(ore_read);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200992
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700993int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200994{
995 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
996 void *iter = NULL;
997 int nelem;
998
999 do {
1000 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001001 osd_req_decode_get_attr_list(ios->per_dev[0].or,
1002 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +02001003 if ((cur_attr.attr_page == attr->attr_page) &&
1004 (cur_attr.attr_id == attr->attr_id)) {
1005 attr->len = cur_attr.len;
1006 attr->val_ptr = cur_attr.val_ptr;
1007 return 0;
1008 }
1009 } while (iter);
1010
1011 return -EIO;
1012}
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001013EXPORT_SYMBOL(extract_attr_from_ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001014
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001015static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001016 struct osd_attr *attr)
1017{
1018 int last_comp = cur_comp + ios->layout->mirrors_p1;
1019
1020 for (; cur_comp < last_comp; ++cur_comp) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001021 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001022 struct osd_request *or;
1023
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001024 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001025 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001026 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001027 return -ENOMEM;
1028 }
1029 per_dev->or = or;
1030
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001031 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001032 osd_req_add_set_attr_list(or, attr, 1);
1033 }
1034
1035 return 0;
1036}
1037
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001038struct _trunc_info {
Boaz Harrosheb507bc2011-08-10 14:17:28 -07001039 struct ore_striping_info si;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001040 u64 prev_group_obj_off;
1041 u64 next_group_obj_off;
1042
1043 unsigned first_group_dev;
1044 unsigned nex_group_dev;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001045};
1046
H Hartley Sweeten1958c7c22011-09-23 13:42:43 -07001047static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
1048 struct _trunc_info *ti)
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001049{
1050 unsigned stripe_unit = layout->stripe_unit;
1051
Boaz Harrosha1fec1d2011-10-12 18:42:22 +02001052 ore_calc_stripe_info(layout, file_offset, 0, &ti->si);
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001053
1054 ti->prev_group_obj_off = ti->si.M * stripe_unit;
1055 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
1056
1057 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
1058 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001059}
1060
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001061int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001062 u64 size)
Boaz Harrosh06886a52009-11-08 14:54:08 +02001063{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001064 struct ore_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001065 struct exofs_trunc_attr {
1066 struct osd_attr attr;
1067 __be64 newsize;
1068 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001069 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001070 int i, ret;
1071
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001072 ret = ore_get_io_state(layout, oc, &ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001073 if (unlikely(ret))
1074 return ret;
1075
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001076 _calc_trunk_info(ios->layout, size, &ti);
1077
Boaz Harroshb916c5c2011-09-28 11:55:51 +03001078 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001079 GFP_KERNEL);
1080 if (unlikely(!size_attrs)) {
1081 ret = -ENOMEM;
1082 goto out;
1083 }
Boaz Harrosh06886a52009-11-08 14:54:08 +02001084
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001085 ios->numdevs = ios->oc->numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001086
Boaz Harroshb916c5c2011-09-28 11:55:51 +03001087 for (i = 0; i < ios->numdevs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001088 struct exofs_trunc_attr *size_attr = &size_attrs[i];
1089 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001090
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001091 if (i < ti.first_group_dev)
1092 obj_size = ti.prev_group_obj_off;
1093 else if (i >= ti.nex_group_dev)
1094 obj_size = ti.next_group_obj_off;
1095 else if (i < ti.si.dev) /* dev within this group */
1096 obj_size = ti.si.obj_offset +
1097 ios->layout->stripe_unit - ti.si.unit_off;
1098 else if (i == ti.si.dev)
1099 obj_size = ti.si.obj_offset;
1100 else /* i > ti.dev */
1101 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001102
1103 size_attr->newsize = cpu_to_be64(obj_size);
1104 size_attr->attr = g_attr_logical_length;
1105 size_attr->attr.val_ptr = &size_attr->newsize;
1106
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001107 ORE_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001108 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001109 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
1110 &size_attr->attr);
1111 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +02001112 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001113 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001114 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001115
1116out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001117 kfree(size_attrs);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001118 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001119 return ret;
1120}
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001121EXPORT_SYMBOL(ore_truncate);
Boaz Harrosh85e44df2011-05-16 15:26:47 +03001122
1123const struct osd_attr g_attr_logical_length = ATTR_DEF(
1124 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001125EXPORT_SYMBOL(g_attr_logical_length);