blob: 0e2a8353f7cc97613965f5973c1e31537f03c903 [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) *
Boaz Harroshaad560b2013-11-21 17:58:08 +0200106 (layout->group_width - layout->parity);
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 Harroshaad560b2013-11-21 17:58:08 +0200289 ios->nr_pages = ((ios->offset & (PAGE_SIZE - 1)) +
290 ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200291 if (layout->parity)
292 _ore_post_alloc_raid_stuff(ios);
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300293 }
294
295 return 0;
296}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700297EXPORT_SYMBOL(ore_get_rw_state);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200298
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300299/* Allocate an io_state for all the devices in the comps array
300 *
301 * This version of io_state allocation is used mostly by create/remove
302 * and trunc where we currently need all the devices. The only wastful
303 * bit is the read/write_attributes with no IO. Those sites should
304 * be converted to use ore_get_rw_state() with length=0
305 */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300306int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300307 struct ore_io_state **pios)
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200308{
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200309 return _ore_get_io_state(layout, oc, oc->numdevs, 0, 0, pios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200310}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700311EXPORT_SYMBOL(ore_get_io_state);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200312
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700313void ore_put_io_state(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200314{
315 if (ios) {
316 unsigned i;
317
318 for (i = 0; i < ios->numdevs; i++) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700319 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200320
321 if (per_dev->or)
322 osd_end_request(per_dev->or);
323 if (per_dev->bio)
324 bio_put(per_dev->bio);
325 }
326
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200327 _ore_free_raid_stuff(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200328 kfree(ios);
329 }
330}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700331EXPORT_SYMBOL(ore_put_io_state);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200332
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700333static void _sync_done(struct ore_io_state *ios, void *p)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200334{
335 struct completion *waiting = p;
336
337 complete(waiting);
338}
339
340static void _last_io(struct kref *kref)
341{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700342 struct ore_io_state *ios = container_of(
343 kref, struct ore_io_state, kref);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200344
345 ios->done(ios, ios->private);
346}
347
348static void _done_io(struct osd_request *or, void *p)
349{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700350 struct ore_io_state *ios = p;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200351
352 kref_put(&ios->kref, _last_io);
353}
354
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200355int ore_io_execute(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200356{
357 DECLARE_COMPLETION_ONSTACK(wait);
358 bool sync = (ios->done == NULL);
359 int i, ret;
360
361 if (sync) {
362 ios->done = _sync_done;
363 ios->private = &wait;
364 }
365
366 for (i = 0; i < ios->numdevs; i++) {
367 struct osd_request *or = ios->per_dev[i].or;
368 if (unlikely(!or))
369 continue;
370
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700371 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200372 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700373 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200374 ret);
375 return ret;
376 }
377 }
378
379 kref_init(&ios->kref);
380
381 for (i = 0; i < ios->numdevs; i++) {
382 struct osd_request *or = ios->per_dev[i].or;
383 if (unlikely(!or))
384 continue;
385
386 kref_get(&ios->kref);
387 osd_execute_request_async(or, _done_io, ios);
388 }
389
390 kref_put(&ios->kref, _last_io);
391 ret = 0;
392
393 if (sync) {
394 wait_for_completion(&wait);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700395 ret = ore_check_io(ios, NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200396 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200397 return ret;
398}
399
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200400static void _clear_bio(struct bio *bio)
401{
402 struct bio_vec *bv;
403 unsigned i;
404
Kent Overstreetd74c6d52013-02-06 12:23:11 -0800405 bio_for_each_segment_all(bv, bio, i) {
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200406 unsigned this_count = bv->bv_len;
407
408 if (likely(PAGE_SIZE == this_count))
409 clear_highpage(bv->bv_page);
410 else
411 zero_user(bv->bv_page, bv->bv_offset, this_count);
412 }
413}
414
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300415int ore_check_io(struct ore_io_state *ios, ore_on_dev_error on_dev_error)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200416{
417 enum osd_err_priority acumulated_osd_err = 0;
418 int acumulated_lin_err = 0;
419 int i;
420
421 for (i = 0; i < ios->numdevs; i++) {
422 struct osd_sense_info osi;
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300423 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
424 struct osd_request *or = per_dev->or;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200425 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200426
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200427 if (unlikely(!or))
428 continue;
429
430 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200431 if (likely(!ret))
432 continue;
433
Boaz Harrosh2deb76d2014-01-13 19:10:40 +0200434 if ((OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) &&
435 per_dev->bio) {
436 /* start read offset passed endof file.
437 * Note: if we do not have bio it means read-attributes
438 * In this case we should return error to caller.
439 */
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300440 _clear_bio(per_dev->bio);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700441 ORE_DBGMSG("start read offset passed end of file "
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200442 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300443 _LLU(per_dev->offset),
444 _LLU(per_dev->length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200445
446 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200447 }
448
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300449 if (on_dev_error) {
450 u64 residual = ios->reading ?
451 or->in.residual : or->out.residual;
452 u64 offset = (ios->offset + ios->length) - residual;
Boaz Harroshffefb8ea2011-12-27 19:23:36 +0200453 unsigned dev = per_dev->dev - ios->oc->first_dev;
454 struct ore_dev *od = ios->oc->ods[dev];
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300455
Boaz Harroshffefb8ea2011-12-27 19:23:36 +0200456 on_dev_error(ios, od, dev, osi.osd_err_pri,
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300457 offset, residual);
458 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200459 if (osi.osd_err_pri >= acumulated_osd_err) {
460 acumulated_osd_err = osi.osd_err_pri;
461 acumulated_lin_err = ret;
462 }
463 }
464
Boaz Harrosh06886a52009-11-08 14:54:08 +0200465 return acumulated_lin_err;
466}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700467EXPORT_SYMBOL(ore_check_io);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200468
Boaz Harroshb367e782010-02-07 19:18:58 +0200469/*
470 * L - logical offset into the file
471 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200472 * D - number of Data devices
473 * D = group_width - parity
Boaz Harroshb367e782010-02-07 19:18:58 +0200474 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200475 * U - The number of bytes in a stripe within a group
476 * U = stripe_unit * D
Boaz Harroshb367e782010-02-07 19:18:58 +0200477 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200478 * T - The number of bytes striped within a group of component objects
479 * (before advancing to the next group)
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200480 * T = U * group_depth
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200481 *
482 * S - The number of bytes striped across all component objects
483 * before the pattern repeats
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200484 * S = T * group_count
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200485 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200486 * M - The "major" (i.e., across all components) cycle number
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200487 * M = L / S
488 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200489 * G - Counts the groups from the beginning of the major cycle
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200490 * G = (L - (M * S)) / T [or (L % S) / T]
491 *
492 * H - The byte offset within the group
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200493 * H = (L - (M * S)) % T [or (L % S) % T]
494 *
495 * N - The "minor" (i.e., across the group) stripe number
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200496 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200497 *
498 * C - The component index coresponding to L
499 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200500 * C = (H - (N * U)) / stripe_unit + G * D
501 * [or (L % U) / stripe_unit + G * D]
Boaz Harroshb367e782010-02-07 19:18:58 +0200502 *
503 * O - The component offset coresponding to L
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200504 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200505 *
506 * LCMdP – Parity cycle: Lowest Common Multiple of group_width, parity
507 * divide by parity
508 * LCMdP = lcm(group_width, parity) / parity
509 *
510 * R - The parity Rotation stripe
511 * (Note parity cycle always starts at a group's boundary)
512 * R = N % LCMdP
513 *
514 * I = the first parity device index
515 * I = (group_width + group_width - R*parity - parity) % group_width
516 *
517 * Craid - The component index Rotated
518 * Craid = (group_width + C - R*parity) % group_width
519 * (We add the group_width to avoid negative numbers modulo math)
Boaz Harroshb367e782010-02-07 19:18:58 +0200520 */
Boaz Harrosh611d7a52011-10-04 14:20:17 +0200521void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200522 u64 length, struct ore_striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200523{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700524 u32 stripe_unit = layout->stripe_unit;
525 u32 group_width = layout->group_width;
526 u64 group_depth = layout->group_depth;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200527 u32 parity = layout->parity;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200528
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200529 u32 D = group_width - parity;
530 u32 U = D * stripe_unit;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200531 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700532 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200533 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200534
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200535 /*
536 G = (L - (M * S)) / T
537 H = (L - (M * S)) % T
538 */
539 u64 LmodS = file_offset - M * S;
540 u32 G = div64_u64(LmodS, T);
541 u64 H = LmodS - G * T;
Boaz Harroshb367e782010-02-07 19:18:58 +0200542
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200543 u32 N = div_u64(H, U);
Boaz Harroshaad560b2013-11-21 17:58:08 +0200544 u32 Nlast;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200545
546 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200547 u32 C = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh455682c2014-04-09 23:14:38 +0300548 u32 first_dev = C - C % group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200549
550 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
551
552 si->obj_offset = si->unit_off + (N * stripe_unit) +
553 (M * group_depth * stripe_unit);
Boaz Harrosh455682c2014-04-09 23:14:38 +0300554 si->cur_comp = C - first_dev;
555 si->cur_pg = si->unit_off / PAGE_SIZE;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200556
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200557 if (parity) {
558 u32 LCMdP = lcm(group_width, parity) / parity;
559 /* R = N % LCMdP; */
560 u32 RxP = (N % LCMdP) * parity;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200561
562 si->par_dev = (group_width + group_width - parity - RxP) %
563 group_width + first_dev;
564 si->dev = (group_width + C - RxP) % group_width + first_dev;
565 si->bytes_in_stripe = U;
566 si->first_stripe_start = M * S + G * T + N * U;
567 } else {
568 /* Make the math correct see _prepare_one_group */
569 si->par_dev = group_width;
570 si->dev = C;
571 }
572
573 si->dev *= layout->mirrors_p1;
574 si->par_dev *= layout->mirrors_p1;
575 si->offset = file_offset;
576 si->length = T - H;
577 if (si->length > length)
578 si->length = length;
Boaz Harroshaad560b2013-11-21 17:58:08 +0200579
580 Nlast = div_u64(H + si->length + U - 1, U);
581 si->maxdevUnits = Nlast - N;
582
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700583 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200584}
Boaz Harrosh611d7a52011-10-04 14:20:17 +0200585EXPORT_SYMBOL(ore_calc_stripe_info);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200586
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200587int _ore_add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
588 unsigned pgbase, struct page **pages,
589 struct ore_per_dev_state *per_dev, int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200590{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200591 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200592 struct request_queue *q =
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700593 osd_request_queue(_ios_od(ios, per_dev->dev));
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700594 unsigned len = cur_len;
595 int ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200596
597 if (per_dev->bio == NULL) {
Boaz Harroshaad560b2013-11-21 17:58:08 +0200598 unsigned bio_size;
599
600 if (!ios->reading) {
601 bio_size = ios->si.maxdevUnits;
602 } else {
603 bio_size = (ios->si.maxdevUnits + 1) *
604 (ios->layout->group_width - ios->layout->parity) /
605 ios->layout->group_width;
606 }
607 bio_size *= (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200608
609 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
610 if (unlikely(!per_dev->bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700611 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200612 bio_size);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700613 ret = -ENOMEM;
614 goto out;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200615 }
616 }
617
618 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200619 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
620 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200621
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200622 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200623
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200624 added_len = bio_add_pc_page(q, per_dev->bio, pages[pg],
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200625 pglen, pgbase);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700626 if (unlikely(pglen != added_len)) {
Boaz Harroshaad560b2013-11-21 17:58:08 +0200627 /* If bi_vcnt == bi_max then this is a SW BUG */
628 ORE_DBGMSG("Failed bio_add_pc_page bi_vcnt=0x%x "
629 "bi_max=0x%x BIO_MAX=0x%x cur_len=0x%x\n",
630 per_dev->bio->bi_vcnt,
631 per_dev->bio->bi_max_vecs,
632 BIO_MAX_PAGES_KMALLOC, cur_len);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700633 ret = -ENOMEM;
634 goto out;
635 }
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200636 _add_stripe_page(ios->sp2d, &ios->si, pages[pg]);
637
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200638 pgbase = 0;
639 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200640 }
641 BUG_ON(cur_len);
642
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700643 per_dev->length += len;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200644 *cur_pg = pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700645 ret = 0;
646out: /* we fail the complete unit on an error eg don't advance
647 * per_dev->length and cur_pg. This means that we might have a bigger
648 * bio than the CDB requested length (per_dev->length). That's fine
649 * only the oposite is fatal.
650 */
651 return ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200652}
653
Boaz Harrosh98260752011-10-02 15:32:50 +0200654static int _prepare_for_striping(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200655{
Boaz Harrosh98260752011-10-02 15:32:50 +0200656 struct ore_striping_info *si = &ios->si;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200657 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200658 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200659 unsigned group_width = ios->layout->group_width;
660 unsigned devs_in_group = group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200661 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200662 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200663 unsigned dev_order;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200664 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh98260752011-10-02 15:32:50 +0200665 u64 length = ios->length;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200666 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200667
Boaz Harrosh98260752011-10-02 15:32:50 +0200668 if (!ios->pages) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200669 ios->numdevs = ios->layout->mirrors_p1;
670 return 0;
671 }
672
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200673 BUG_ON(length > si->length);
674
Boaz Harrosh455682c2014-04-09 23:14:38 +0300675 dev_order = si->cur_comp;
Boaz Harrosh98260752011-10-02 15:32:50 +0200676
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200677 while (length) {
Boaz Harrosh101a6422014-04-03 17:53:31 +0300678 struct ore_per_dev_state *per_dev =
679 &ios->per_dev[dev - first_dev];
Boaz Harroshb367e782010-02-07 19:18:58 +0200680 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200681
682 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200683 per_dev->dev = dev;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200684 if (dev == si->dev) {
685 WARN_ON(dev == si->par_dev);
Boaz Harroshb367e782010-02-07 19:18:58 +0200686 per_dev->offset = si->obj_offset;
687 cur_len = stripe_unit - si->unit_off;
688 page_off = si->unit_off & ~PAGE_MASK;
689 BUG_ON(page_off && (page_off != ios->pgbase));
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200690 } else {
691 if (si->cur_comp > dev_order)
692 per_dev->offset =
693 si->obj_offset - si->unit_off;
694 else /* si->cur_comp < dev_order */
695 per_dev->offset =
696 si->obj_offset + stripe_unit -
697 si->unit_off;
Boaz Harroshb367e782010-02-07 19:18:58 +0200698 cur_len = stripe_unit;
699 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200700 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200701 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200702 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200703 if (cur_len >= length)
704 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200705
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200706 ret = _ore_add_stripe_unit(ios, &cur_pg, page_off, ios->pages,
707 per_dev, cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200708 if (unlikely(ret))
709 goto out;
710
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200711 length -= cur_len;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200712
Boaz Harrosh101a6422014-04-03 17:53:31 +0300713 dev = ((dev + mirrors_p1) % devs_in_group) + first_dev;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200714 si->cur_comp = (si->cur_comp + 1) % group_width;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200715 if (unlikely((dev == si->par_dev) || (!length && ios->sp2d))) {
716 if (!length && ios->sp2d) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200717 /* If we are writing and this is the very last
718 * stripe. then operate on parity dev.
719 */
720 dev = si->par_dev;
Boaz Harrosh455682c2014-04-09 23:14:38 +0300721 /* If last stripe operate on parity comp */
722 si->cur_comp = group_width - ios->layout->parity;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200723 }
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200724 per_dev = &ios->per_dev[dev - first_dev];
725 if (!per_dev->length) {
726 /* Only/always the parity unit of the first
727 * stripe will be empty. So this is a chance to
728 * initialize the per_dev info.
729 */
730 per_dev->dev = dev;
731 per_dev->offset = si->obj_offset - si->unit_off;
732 }
733
Boaz Harrosh101a6422014-04-03 17:53:31 +0300734 /* In writes cur_len just means if it's the
735 * last one. See _ore_add_parity_unit.
736 */
737 ret = _ore_add_parity_unit(ios, si, per_dev,
738 ios->sp2d ? length : cur_len);
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200739 if (unlikely(ret))
740 goto out;
741
742 /* Rotate next par_dev backwards with wraping */
743 si->par_dev = (devs_in_group + si->par_dev -
744 ios->layout->parity * mirrors_p1) %
745 devs_in_group + first_dev;
746 /* Next stripe, start fresh */
747 si->cur_comp = 0;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200748 si->cur_pg = 0;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200749 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200750 }
751out:
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300752 ios->numdevs = devs_in_group;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200753 ios->pages_consumed = cur_pg;
Boaz Harrosh62b62ad2012-06-08 04:30:40 +0300754 return ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200755}
756
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700757int ore_create(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200758{
759 int i, ret;
760
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300761 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200762 struct osd_request *or;
763
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700764 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200765 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700766 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200767 ret = -ENOMEM;
768 goto out;
769 }
770 ios->per_dev[i].or = or;
771 ios->numdevs++;
772
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700773 osd_req_create_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200774 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700775 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200776
777out:
778 return ret;
779}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700780EXPORT_SYMBOL(ore_create);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200781
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700782int ore_remove(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200783{
784 int i, ret;
785
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300786 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200787 struct osd_request *or;
788
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700789 or = osd_start_request(_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200790 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700791 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200792 ret = -ENOMEM;
793 goto out;
794 }
795 ios->per_dev[i].or = or;
796 ios->numdevs++;
797
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700798 osd_req_remove_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200799 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700800 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200801
802out:
803 return ret;
804}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700805EXPORT_SYMBOL(ore_remove);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200806
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700807static int _write_mirror(struct ore_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200808{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700809 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200810 unsigned dev = ios->per_dev[cur_comp].dev;
811 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
812 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200813
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200814 if (ios->pages && !master_dev->length)
815 return 0; /* Just an empty slot */
816
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200817 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700818 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200819 struct osd_request *or;
820
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700821 or = osd_start_request(_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200822 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700823 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200824 ret = -ENOMEM;
825 goto out;
826 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200827 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200828
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200829 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200830 struct bio *bio;
831
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200832 if (per_dev != master_dev) {
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700833 bio = bio_clone_kmalloc(master_dev->bio,
834 GFP_KERNEL);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200835 if (unlikely(!bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700836 ORE_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200837 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200838 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200839 ret = -ENOMEM;
840 goto out;
841 }
842
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200843 bio->bi_bdev = NULL;
844 bio->bi_next = NULL;
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700845 per_dev->offset = master_dev->offset;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200846 per_dev->length = master_dev->length;
847 per_dev->bio = bio;
848 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200849 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200850 bio = master_dev->bio;
851 /* FIXME: bio_set_dir() */
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200852 bio->bi_rw |= REQ_WRITE;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200853 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200854
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300855 osd_req_write(or, _ios_obj(ios, cur_comp),
856 per_dev->offset, bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700857 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200858 "length=0x%llx dev=%d\n",
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300859 _LLU(_ios_obj(ios, cur_comp)->id),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700860 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200861 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200862 } else if (ios->kern_buff) {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700863 per_dev->offset = ios->si.obj_offset;
864 per_dev->dev = ios->si.dev + dev;
865
866 /* no cross device without page array */
867 BUG_ON((ios->layout->group_width > 1) &&
868 (ios->si.unit_off + ios->length >
869 ios->layout->stripe_unit));
870
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300871 ret = osd_req_write_kern(or, _ios_obj(ios, cur_comp),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700872 per_dev->offset,
873 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200874 if (unlikely(ret))
875 goto out;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700876 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e7c2009-12-15 19:34:17 +0200877 "length=0x%llx dev=%d\n",
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300878 _LLU(_ios_obj(ios, cur_comp)->id),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700879 _LLU(per_dev->offset),
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700880 _LLU(ios->length), per_dev->dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200881 } else {
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300882 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700883 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300884 _LLU(_ios_obj(ios, cur_comp)->id),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700885 ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200886 }
887
888 if (ios->out_attr)
889 osd_req_add_set_attr_list(or, ios->out_attr,
890 ios->out_attr_len);
891
892 if (ios->in_attr)
893 osd_req_add_get_attr_list(or, ios->in_attr,
894 ios->in_attr_len);
895 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200896
897out:
898 return ret;
899}
900
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700901int ore_write(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200902{
903 int i;
904 int ret;
905
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200906 if (unlikely(ios->sp2d && !ios->r4w)) {
907 /* A library is attempting a RAID-write without providing
908 * a pages lock interface.
909 */
910 WARN_ON_ONCE(1);
911 return -ENOTSUPP;
912 }
913
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200914 ret = _prepare_for_striping(ios);
915 if (unlikely(ret))
916 return ret;
917
918 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700919 ret = _write_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200920 if (unlikely(ret))
921 return ret;
922 }
923
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700924 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200925 return ret;
926}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700927EXPORT_SYMBOL(ore_write);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200928
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200929int _ore_read_mirror(struct ore_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200930{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200931 struct osd_request *or;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700932 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700933 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
934 unsigned first_dev = (unsigned)obj->id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200935
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200936 if (ios->pages && !per_dev->length)
937 return 0; /* Just an empty slot */
938
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200939 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700940 or = osd_start_request(_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200941 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700942 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200943 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200944 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200945 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200946
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200947 if (ios->pages) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200948 if (per_dev->cur_sg) {
949 /* finalize the last sg_entry */
950 _ore_add_sg_seg(per_dev, 0, false);
951 if (unlikely(!per_dev->cur_sg))
952 return 0; /* Skip parity only device */
953
954 osd_req_read_sg(or, obj, per_dev->bio,
955 per_dev->sglist, per_dev->cur_sg);
956 } else {
957 /* The no raid case */
958 osd_req_read(or, obj, per_dev->offset,
959 per_dev->bio, per_dev->length);
960 }
961
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700962 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200963 " dev=%d sg_len=%d\n", _LLU(obj->id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200964 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200965 first_dev, per_dev->cur_sg);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200966 } else {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700967 BUG_ON(ios->kern_buff);
968
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700969 osd_req_get_attributes(or, obj);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700970 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700971 _LLU(obj->id),
972 ios->in_attr_len, first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200973 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200974 if (ios->out_attr)
975 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
976
977 if (ios->in_attr)
978 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
979
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200980 return 0;
981}
982
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700983int ore_read(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200984{
985 int i;
986 int ret;
987
988 ret = _prepare_for_striping(ios);
989 if (unlikely(ret))
990 return ret;
991
992 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200993 ret = _ore_read_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200994 if (unlikely(ret))
995 return ret;
996 }
997
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700998 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200999 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001000}
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001001EXPORT_SYMBOL(ore_read);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001002
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001003int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +02001004{
1005 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
1006 void *iter = NULL;
1007 int nelem;
1008
1009 do {
1010 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001011 osd_req_decode_get_attr_list(ios->per_dev[0].or,
1012 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +02001013 if ((cur_attr.attr_page == attr->attr_page) &&
1014 (cur_attr.attr_id == attr->attr_id)) {
1015 attr->len = cur_attr.len;
1016 attr->val_ptr = cur_attr.val_ptr;
1017 return 0;
1018 }
1019 } while (iter);
1020
1021 return -EIO;
1022}
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001023EXPORT_SYMBOL(extract_attr_from_ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001024
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001025static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001026 struct osd_attr *attr)
1027{
1028 int last_comp = cur_comp + ios->layout->mirrors_p1;
1029
1030 for (; cur_comp < last_comp; ++cur_comp) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001031 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001032 struct osd_request *or;
1033
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001034 or = osd_start_request(_ios_od(ios, cur_comp), GFP_KERNEL);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001035 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001036 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001037 return -ENOMEM;
1038 }
1039 per_dev->or = or;
1040
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001041 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001042 osd_req_add_set_attr_list(or, attr, 1);
1043 }
1044
1045 return 0;
1046}
1047
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001048struct _trunc_info {
Boaz Harrosheb507bc2011-08-10 14:17:28 -07001049 struct ore_striping_info si;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001050 u64 prev_group_obj_off;
1051 u64 next_group_obj_off;
1052
1053 unsigned first_group_dev;
1054 unsigned nex_group_dev;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001055};
1056
H Hartley Sweeten1958c7c22011-09-23 13:42:43 -07001057static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
1058 struct _trunc_info *ti)
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001059{
1060 unsigned stripe_unit = layout->stripe_unit;
1061
Boaz Harrosha1fec1d2011-10-12 18:42:22 +02001062 ore_calc_stripe_info(layout, file_offset, 0, &ti->si);
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001063
1064 ti->prev_group_obj_off = ti->si.M * stripe_unit;
1065 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
1066
1067 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
1068 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001069}
1070
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001071int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001072 u64 size)
Boaz Harrosh06886a52009-11-08 14:54:08 +02001073{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001074 struct ore_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001075 struct exofs_trunc_attr {
1076 struct osd_attr attr;
1077 __be64 newsize;
1078 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001079 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001080 int i, ret;
1081
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001082 ret = ore_get_io_state(layout, oc, &ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001083 if (unlikely(ret))
1084 return ret;
1085
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001086 _calc_trunk_info(ios->layout, size, &ti);
1087
Boaz Harroshb916c5c2011-09-28 11:55:51 +03001088 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001089 GFP_KERNEL);
1090 if (unlikely(!size_attrs)) {
1091 ret = -ENOMEM;
1092 goto out;
1093 }
Boaz Harrosh06886a52009-11-08 14:54:08 +02001094
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001095 ios->numdevs = ios->oc->numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001096
Boaz Harroshb916c5c2011-09-28 11:55:51 +03001097 for (i = 0; i < ios->numdevs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001098 struct exofs_trunc_attr *size_attr = &size_attrs[i];
1099 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001100
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001101 if (i < ti.first_group_dev)
1102 obj_size = ti.prev_group_obj_off;
1103 else if (i >= ti.nex_group_dev)
1104 obj_size = ti.next_group_obj_off;
1105 else if (i < ti.si.dev) /* dev within this group */
1106 obj_size = ti.si.obj_offset +
1107 ios->layout->stripe_unit - ti.si.unit_off;
1108 else if (i == ti.si.dev)
1109 obj_size = ti.si.obj_offset;
1110 else /* i > ti.dev */
1111 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001112
1113 size_attr->newsize = cpu_to_be64(obj_size);
1114 size_attr->attr = g_attr_logical_length;
1115 size_attr->attr.val_ptr = &size_attr->newsize;
1116
Boaz Harroshaad560b2013-11-21 17:58:08 +02001117 ORE_DBGMSG2("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001118 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001119 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
1120 &size_attr->attr);
1121 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +02001122 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001123 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001124 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001125
1126out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001127 kfree(size_attrs);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001128 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001129 return ret;
1130}
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001131EXPORT_SYMBOL(ore_truncate);
Boaz Harrosh85e44df2011-05-16 15:26:47 +03001132
1133const struct osd_attr g_attr_logical_length = ATTR_DEF(
1134 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001135EXPORT_SYMBOL(g_attr_logical_length);