blob: 1b8b44637e7065b9bc7ffdf17f0228fc84814860 [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
Boaz Harroshaa281ac2014-10-19 19:38:58 +03005 * Boaz Harrosh <ooo@electrozaur.com>
Boaz Harroshb14f8ab2008-10-27 18:27:55 +02006 *
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 Harroshaa281ac2014-10-19 19:38:58 +030032MODULE_AUTHOR("Boaz Harrosh <ooo@electrozaur.com>");
Boaz Harroshcf283ad2011-08-06 19:22:06 -070033MODULE_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:
Boaz Harroshce5d36a2014-05-22 14:48:15 +030061 layout->parity = 2;
62 break;
Boaz Harrosh44231e62010-11-21 20:03:24 +020063 case PNFS_OSD_RAID_4:
64 default:
Boaz Harroshce5d36a2014-05-22 14:48:15 +030065 ORE_ERR("Only RAID_0/5/6 for now received-enum=%d\n",
66 layout->raid_algorithm);
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +030067 return -EINVAL;
68 }
69 if (0 != (layout->stripe_unit & ~PAGE_MASK)) {
70 ORE_ERR("Stripe Unit(0x%llx)"
71 " must be Multples of PAGE_SIZE(0x%lx)\n",
72 _LLU(layout->stripe_unit), PAGE_SIZE);
73 return -EINVAL;
74 }
75 if (layout->group_width) {
76 if (!layout->group_depth) {
77 ORE_ERR("group_depth == 0 && group_width != 0\n");
78 return -EINVAL;
79 }
80 if (total_comps < (layout->group_width * layout->mirrors_p1)) {
81 ORE_ERR("Data Map wrong, "
82 "numdevs=%d < group_width=%d * mirrors=%d\n",
83 total_comps, layout->group_width,
84 layout->mirrors_p1);
85 return -EINVAL;
86 }
87 layout->group_count = total_comps / layout->mirrors_p1 /
88 layout->group_width;
89 } else {
90 if (layout->group_depth) {
91 printk(KERN_NOTICE "Warning: group_depth ignored "
92 "group_width == 0 && group_depth == %lld\n",
93 _LLU(layout->group_depth));
94 }
95 layout->group_width = total_comps / layout->mirrors_p1;
96 layout->group_depth = -1;
97 layout->group_count = 1;
98 }
99
100 stripe_length = (u64)layout->group_width * layout->stripe_unit;
101 if (stripe_length >= (1ULL << 32)) {
102 ORE_ERR("Stripe_length(0x%llx) >= 32bit is not supported\n",
103 _LLU(stripe_length));
104 return -EINVAL;
105 }
106
107 layout->max_io_length =
108 (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE - layout->stripe_unit) *
Boaz Harroshaad560b2013-11-21 17:58:08 +0200109 (layout->group_width - layout->parity);
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200110 if (layout->parity) {
111 unsigned stripe_length =
112 (layout->group_width - layout->parity) *
113 layout->stripe_unit;
114
115 layout->max_io_length /= stripe_length;
116 layout->max_io_length *= stripe_length;
117 }
Boaz Harroshce5d36a2014-05-22 14:48:15 +0300118 ORE_DBGMSG("max_io_length=0x%lx\n", layout->max_io_length);
119
Boaz Harrosh5a51c0c2011-09-28 13:18:45 +0300120 return 0;
121}
122EXPORT_SYMBOL(ore_verify_layout);
123
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700124static u8 *_ios_cred(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].cred;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700127}
128
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700129static struct osd_obj_id *_ios_obj(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700130{
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300131 return &ios->oc->comps[index & ios->oc->single_comp].obj;
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700132}
133
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700134static struct osd_dev *_ios_od(struct ore_io_state *ios, unsigned index)
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700135{
Boaz Harrosh3bd98562011-09-28 12:04:23 +0300136 ORE_DBGMSG2("oc->first_dev=%d oc->numdevs=%d i=%d oc->ods=%p\n",
137 ios->oc->first_dev, ios->oc->numdevs, index,
138 ios->oc->ods);
139
Boaz Harroshd866d872011-09-28 14:43:09 +0300140 return ore_comp_dev(ios->oc, index);
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700141}
142
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200143int _ore_get_io_state(struct ore_layout *layout,
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200144 struct ore_components *oc, unsigned numdevs,
145 unsigned sgs_per_dev, unsigned num_par_pages,
146 struct ore_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200147{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700148 struct ore_io_state *ios;
Kees Cook20fe9352018-06-14 15:27:27 -0700149 size_t size_ios, size_extra, size_total;
150 void *ios_extra;
151
152 /*
153 * The desired layout looks like this, with the extra_allocation
154 * items pointed at from fields within ios or per_dev:
155
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200156 struct __alloc_all_io_state {
157 struct ore_io_state ios;
158 struct ore_per_dev_state per_dev[numdevs];
159 union {
160 struct osd_sg_entry sglist[sgs_per_dev * numdevs];
161 struct page *pages[num_par_pages];
Kees Cook20fe9352018-06-14 15:27:27 -0700162 } extra_allocation;
163 } whole_allocation;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200164
Kees Cook20fe9352018-06-14 15:27:27 -0700165 */
166
167 /* This should never happen, so abort early if it ever does. */
168 if (sgs_per_dev && num_par_pages) {
169 ORE_DBGMSG("Tried to use both pages and sglist\n");
170 *pios = NULL;
171 return -EINVAL;
172 }
173
174 if (numdevs > (INT_MAX - sizeof(*ios)) /
175 sizeof(struct ore_per_dev_state))
176 return -ENOMEM;
177 size_ios = sizeof(*ios) + sizeof(struct ore_per_dev_state) * numdevs;
178
179 if (sgs_per_dev * numdevs > INT_MAX / sizeof(struct osd_sg_entry))
180 return -ENOMEM;
181 if (num_par_pages > INT_MAX / sizeof(struct page *))
182 return -ENOMEM;
183 size_extra = max(sizeof(struct osd_sg_entry) * (sgs_per_dev * numdevs),
184 sizeof(struct page *) * num_par_pages);
185
186 size_total = size_ios + size_extra;
187
188 if (likely(size_total <= PAGE_SIZE)) {
189 ios = kzalloc(size_total, GFP_KERNEL);
190 if (unlikely(!ios)) {
191 ORE_DBGMSG("Failed kzalloc bytes=%zd\n", size_total);
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200192 *pios = NULL;
193 return -ENOMEM;
194 }
Kees Cook20fe9352018-06-14 15:27:27 -0700195 ios_extra = (char *)ios + size_ios;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200196 } else {
Kees Cook20fe9352018-06-14 15:27:27 -0700197 ios = kzalloc(size_ios, GFP_KERNEL);
198 if (unlikely(!ios)) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200199 ORE_DBGMSG("Failed alloc first part bytes=%zd\n",
Kees Cook20fe9352018-06-14 15:27:27 -0700200 size_ios);
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200201 *pios = NULL;
202 return -ENOMEM;
203 }
Kees Cook20fe9352018-06-14 15:27:27 -0700204 ios_extra = kzalloc(size_extra, GFP_KERNEL);
205 if (unlikely(!ios_extra)) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200206 ORE_DBGMSG("Failed alloc second part bytes=%zd\n",
Kees Cook20fe9352018-06-14 15:27:27 -0700207 size_extra);
208 kfree(ios);
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200209 *pios = NULL;
210 return -ENOMEM;
211 }
212
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200213 /* In this case the per_dev[0].sgilist holds the pointer to
214 * be freed
215 */
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200216 ios->extra_part_alloc = true;
217 }
218
Kees Cook20fe9352018-06-14 15:27:27 -0700219 if (num_par_pages) {
220 ios->parity_pages = ios_extra;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200221 ios->max_par_pages = num_par_pages;
222 }
Kees Cook20fe9352018-06-14 15:27:27 -0700223 if (sgs_per_dev) {
224 struct osd_sg_entry *sgilist = ios_extra;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200225 unsigned d;
226
227 for (d = 0; d < numdevs; ++d) {
228 ios->per_dev[d].sglist = sgilist;
229 sgilist += sgs_per_dev;
230 }
231 ios->sgs_per_dev = sgs_per_dev;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200232 }
233
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200234 ios->layout = layout;
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300235 ios->oc = oc;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200236 *pios = ios;
237 return 0;
238}
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300239
240/* Allocate an io_state for only a single group of devices
241 *
242 * If a user needs to call ore_read/write() this version must be used becase it
243 * allocates extra stuff for striping and raid.
244 * The ore might decide to only IO less then @length bytes do to alignmets
245 * and constrains as follows:
246 * - The IO cannot cross group boundary.
247 * - In raid5/6 The end of the IO must align at end of a stripe eg.
248 * (@offset + @length) % strip_size == 0. Or the complete range is within a
249 * single stripe.
250 * - Memory condition only permitted a shorter IO. (A user can use @length=~0
251 * And check the returned ios->length for max_io_size.)
252 *
253 * The caller must check returned ios->length (and/or ios->nr_pages) and
254 * re-issue these pages that fall outside of ios->length
255 */
256int ore_get_rw_state(struct ore_layout *layout, struct ore_components *oc,
257 bool is_reading, u64 offset, u64 length,
258 struct ore_io_state **pios)
259{
260 struct ore_io_state *ios;
261 unsigned numdevs = layout->group_width * layout->mirrors_p1;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200262 unsigned sgs_per_dev = 0, max_par_pages = 0;
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300263 int ret;
264
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200265 if (layout->parity && length) {
266 unsigned data_devs = layout->group_width - layout->parity;
267 unsigned stripe_size = layout->stripe_unit * data_devs;
268 unsigned pages_in_unit = layout->stripe_unit / PAGE_SIZE;
269 u32 remainder;
270 u64 num_stripes;
271 u64 num_raid_units;
272
273 num_stripes = div_u64_rem(length, stripe_size, &remainder);
274 if (remainder)
275 ++num_stripes;
276
277 num_raid_units = num_stripes * layout->parity;
278
279 if (is_reading) {
280 /* For reads add per_dev sglist array */
281 /* TODO: Raid 6 we need twice more. Actually:
282 * num_stripes / LCMdP(W,P);
283 * if (W%P != 0) num_stripes *= parity;
284 */
285
286 /* first/last seg is split */
287 num_raid_units += layout->group_width;
Boaz Harrosh361aba52011-12-28 19:14:23 +0200288 sgs_per_dev = div_u64(num_raid_units, data_devs) + 2;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200289 } else {
290 /* For Writes add parity pages array. */
291 max_par_pages = num_raid_units * pages_in_unit *
292 sizeof(struct page *);
293 }
294 }
295
296 ret = _ore_get_io_state(layout, oc, numdevs, sgs_per_dev, max_par_pages,
297 pios);
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300298 if (unlikely(ret))
299 return ret;
300
301 ios = *pios;
302 ios->reading = is_reading;
303 ios->offset = offset;
304
305 if (length) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200306 ore_calc_stripe_info(layout, offset, length, &ios->si);
307 ios->length = ios->si.length;
Boaz Harroshaad560b2013-11-21 17:58:08 +0200308 ios->nr_pages = ((ios->offset & (PAGE_SIZE - 1)) +
309 ios->length + PAGE_SIZE - 1) / PAGE_SIZE;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200310 if (layout->parity)
311 _ore_post_alloc_raid_stuff(ios);
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300312 }
313
314 return 0;
315}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700316EXPORT_SYMBOL(ore_get_rw_state);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200317
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300318/* Allocate an io_state for all the devices in the comps array
319 *
320 * This version of io_state allocation is used mostly by create/remove
321 * and trunc where we currently need all the devices. The only wastful
322 * bit is the read/write_attributes with no IO. Those sites should
323 * be converted to use ore_get_rw_state() with length=0
324 */
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300325int ore_get_io_state(struct ore_layout *layout, struct ore_components *oc,
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300326 struct ore_io_state **pios)
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200327{
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200328 return _ore_get_io_state(layout, oc, oc->numdevs, 0, 0, pios);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200329}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700330EXPORT_SYMBOL(ore_get_io_state);
Boaz Harroshe1042ba2010-11-16 20:09:58 +0200331
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700332void ore_put_io_state(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200333{
334 if (ios) {
335 unsigned i;
336
337 for (i = 0; i < ios->numdevs; i++) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700338 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200339
340 if (per_dev->or)
341 osd_end_request(per_dev->or);
342 if (per_dev->bio)
343 bio_put(per_dev->bio);
344 }
345
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200346 _ore_free_raid_stuff(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200347 kfree(ios);
348 }
349}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700350EXPORT_SYMBOL(ore_put_io_state);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200351
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700352static void _sync_done(struct ore_io_state *ios, void *p)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200353{
354 struct completion *waiting = p;
355
356 complete(waiting);
357}
358
359static void _last_io(struct kref *kref)
360{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700361 struct ore_io_state *ios = container_of(
362 kref, struct ore_io_state, kref);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200363
364 ios->done(ios, ios->private);
365}
366
367static void _done_io(struct osd_request *or, void *p)
368{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700369 struct ore_io_state *ios = p;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200370
371 kref_put(&ios->kref, _last_io);
372}
373
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200374int ore_io_execute(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200375{
376 DECLARE_COMPLETION_ONSTACK(wait);
377 bool sync = (ios->done == NULL);
378 int i, ret;
379
380 if (sync) {
381 ios->done = _sync_done;
382 ios->private = &wait;
383 }
384
385 for (i = 0; i < ios->numdevs; i++) {
386 struct osd_request *or = ios->per_dev[i].or;
387 if (unlikely(!or))
388 continue;
389
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700390 ret = osd_finalize_request(or, 0, _ios_cred(ios, i), NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200391 if (unlikely(ret)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700392 ORE_DBGMSG("Failed to osd_finalize_request() => %d\n",
Boaz Harrosh06886a52009-11-08 14:54:08 +0200393 ret);
394 return ret;
395 }
396 }
397
398 kref_init(&ios->kref);
399
400 for (i = 0; i < ios->numdevs; i++) {
401 struct osd_request *or = ios->per_dev[i].or;
402 if (unlikely(!or))
403 continue;
404
405 kref_get(&ios->kref);
406 osd_execute_request_async(or, _done_io, ios);
407 }
408
409 kref_put(&ios->kref, _last_io);
410 ret = 0;
411
412 if (sync) {
413 wait_for_completion(&wait);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700414 ret = ore_check_io(ios, NULL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200415 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200416 return ret;
417}
418
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200419static void _clear_bio(struct bio *bio)
420{
421 struct bio_vec *bv;
422 unsigned i;
423
Kent Overstreetd74c6d52013-02-06 12:23:11 -0800424 bio_for_each_segment_all(bv, bio, i) {
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200425 unsigned this_count = bv->bv_len;
426
427 if (likely(PAGE_SIZE == this_count))
428 clear_highpage(bv->bv_page);
429 else
430 zero_user(bv->bv_page, bv->bv_offset, this_count);
431 }
432}
433
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300434int ore_check_io(struct ore_io_state *ios, ore_on_dev_error on_dev_error)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200435{
436 enum osd_err_priority acumulated_osd_err = 0;
437 int acumulated_lin_err = 0;
438 int i;
439
440 for (i = 0; i < ios->numdevs; i++) {
441 struct osd_sense_info osi;
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300442 struct ore_per_dev_state *per_dev = &ios->per_dev[i];
443 struct osd_request *or = per_dev->or;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200444 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200445
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200446 if (unlikely(!or))
447 continue;
448
449 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200450 if (likely(!ret))
451 continue;
452
Boaz Harrosh2deb76d2014-01-13 19:10:40 +0200453 if ((OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) &&
454 per_dev->bio) {
455 /* start read offset passed endof file.
456 * Note: if we do not have bio it means read-attributes
457 * In this case we should return error to caller.
458 */
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300459 _clear_bio(per_dev->bio);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700460 ORE_DBGMSG("start read offset passed end of file "
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200461 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300462 _LLU(per_dev->offset),
463 _LLU(per_dev->length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200464
465 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200466 }
467
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300468 if (on_dev_error) {
469 u64 residual = ios->reading ?
470 or->in.residual : or->out.residual;
471 u64 offset = (ios->offset + ios->length) - residual;
Boaz Harroshffefb8ea2011-12-27 19:23:36 +0200472 unsigned dev = per_dev->dev - ios->oc->first_dev;
473 struct ore_dev *od = ios->oc->ods[dev];
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300474
Boaz Harroshffefb8ea2011-12-27 19:23:36 +0200475 on_dev_error(ios, od, dev, osi.osd_err_pri,
Boaz Harrosh4b46c9f2011-09-28 13:25:50 +0300476 offset, residual);
477 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200478 if (osi.osd_err_pri >= acumulated_osd_err) {
479 acumulated_osd_err = osi.osd_err_pri;
480 acumulated_lin_err = ret;
481 }
482 }
483
Boaz Harrosh06886a52009-11-08 14:54:08 +0200484 return acumulated_lin_err;
485}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700486EXPORT_SYMBOL(ore_check_io);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200487
Boaz Harroshb367e782010-02-07 19:18:58 +0200488/*
489 * L - logical offset into the file
490 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200491 * D - number of Data devices
492 * D = group_width - parity
Boaz Harroshb367e782010-02-07 19:18:58 +0200493 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200494 * U - The number of bytes in a stripe within a group
495 * U = stripe_unit * D
Boaz Harroshb367e782010-02-07 19:18:58 +0200496 *
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200497 * T - The number of bytes striped within a group of component objects
498 * (before advancing to the next group)
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200499 * T = U * group_depth
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200500 *
501 * S - The number of bytes striped across all component objects
502 * before the pattern repeats
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200503 * S = T * group_count
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200504 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200505 * M - The "major" (i.e., across all components) cycle number
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200506 * M = L / S
507 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200508 * G - Counts the groups from the beginning of the major cycle
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200509 * G = (L - (M * S)) / T [or (L % S) / T]
510 *
511 * H - The byte offset within the group
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200512 * H = (L - (M * S)) % T [or (L % S) % T]
513 *
514 * N - The "minor" (i.e., across the group) stripe number
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200515 * N = H / U
Boaz Harroshb367e782010-02-07 19:18:58 +0200516 *
517 * C - The component index coresponding to L
518 *
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200519 * C = (H - (N * U)) / stripe_unit + G * D
520 * [or (L % U) / stripe_unit + G * D]
Boaz Harroshb367e782010-02-07 19:18:58 +0200521 *
522 * O - The component offset coresponding to L
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200523 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200524 *
525 * LCMdP – Parity cycle: Lowest Common Multiple of group_width, parity
526 * divide by parity
527 * LCMdP = lcm(group_width, parity) / parity
528 *
529 * R - The parity Rotation stripe
530 * (Note parity cycle always starts at a group's boundary)
531 * R = N % LCMdP
532 *
533 * I = the first parity device index
534 * I = (group_width + group_width - R*parity - parity) % group_width
535 *
536 * Craid - The component index Rotated
537 * Craid = (group_width + C - R*parity) % group_width
538 * (We add the group_width to avoid negative numbers modulo math)
Boaz Harroshb367e782010-02-07 19:18:58 +0200539 */
Boaz Harrosh611d7a52011-10-04 14:20:17 +0200540void ore_calc_stripe_info(struct ore_layout *layout, u64 file_offset,
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200541 u64 length, struct ore_striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200542{
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700543 u32 stripe_unit = layout->stripe_unit;
544 u32 group_width = layout->group_width;
545 u64 group_depth = layout->group_depth;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200546 u32 parity = layout->parity;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200547
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200548 u32 D = group_width - parity;
549 u32 U = D * stripe_unit;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200550 u64 T = U * group_depth;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700551 u64 S = T * layout->group_count;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200552 u64 M = div64_u64(file_offset, S);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200553
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200554 /*
555 G = (L - (M * S)) / T
556 H = (L - (M * S)) % T
557 */
558 u64 LmodS = file_offset - M * S;
559 u32 G = div64_u64(LmodS, T);
560 u64 H = LmodS - G * T;
Boaz Harroshb367e782010-02-07 19:18:58 +0200561
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200562 u32 N = div_u64(H, U);
Boaz Harroshaad560b2013-11-21 17:58:08 +0200563 u32 Nlast;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200564
565 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200566 u32 C = (u32)(H - (N * U)) / stripe_unit + G * group_width;
Boaz Harrosh455682c2014-04-09 23:14:38 +0300567 u32 first_dev = C - C % group_width;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200568
569 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
570
571 si->obj_offset = si->unit_off + (N * stripe_unit) +
572 (M * group_depth * stripe_unit);
Boaz Harrosh455682c2014-04-09 23:14:38 +0300573 si->cur_comp = C - first_dev;
574 si->cur_pg = si->unit_off / PAGE_SIZE;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200575
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200576 if (parity) {
577 u32 LCMdP = lcm(group_width, parity) / parity;
578 /* R = N % LCMdP; */
579 u32 RxP = (N % LCMdP) * parity;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200580
581 si->par_dev = (group_width + group_width - parity - RxP) %
582 group_width + first_dev;
Boaz Harroshce5d36a2014-05-22 14:48:15 +0300583 si->dev = (group_width + group_width + C - RxP) %
584 group_width + first_dev;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200585 si->bytes_in_stripe = U;
586 si->first_stripe_start = M * S + G * T + N * U;
587 } else {
588 /* Make the math correct see _prepare_one_group */
589 si->par_dev = group_width;
590 si->dev = C;
591 }
592
593 si->dev *= layout->mirrors_p1;
594 si->par_dev *= layout->mirrors_p1;
595 si->offset = file_offset;
596 si->length = T - H;
597 if (si->length > length)
598 si->length = length;
Boaz Harroshaad560b2013-11-21 17:58:08 +0200599
600 Nlast = div_u64(H + si->length + U - 1, U);
601 si->maxdevUnits = Nlast - N;
602
Boaz Harrosh16f75bb2011-08-03 20:44:16 -0700603 si->M = M;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200604}
Boaz Harrosh611d7a52011-10-04 14:20:17 +0200605EXPORT_SYMBOL(ore_calc_stripe_info);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200606
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200607int _ore_add_stripe_unit(struct ore_io_state *ios, unsigned *cur_pg,
608 unsigned pgbase, struct page **pages,
609 struct ore_per_dev_state *per_dev, int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200610{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200611 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200612 struct request_queue *q =
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700613 osd_request_queue(_ios_od(ios, per_dev->dev));
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700614 unsigned len = cur_len;
615 int ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200616
617 if (per_dev->bio == NULL) {
Boaz Harroshaad560b2013-11-21 17:58:08 +0200618 unsigned bio_size;
619
620 if (!ios->reading) {
621 bio_size = ios->si.maxdevUnits;
622 } else {
623 bio_size = (ios->si.maxdevUnits + 1) *
624 (ios->layout->group_width - ios->layout->parity) /
625 ios->layout->group_width;
626 }
627 bio_size *= (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200628
629 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
630 if (unlikely(!per_dev->bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700631 ORE_DBGMSG("Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200632 bio_size);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700633 ret = -ENOMEM;
634 goto out;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200635 }
636 }
637
638 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200639 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
640 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200641
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200642 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200643
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200644 added_len = bio_add_pc_page(q, per_dev->bio, pages[pg],
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200645 pglen, pgbase);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700646 if (unlikely(pglen != added_len)) {
Boaz Harroshaad560b2013-11-21 17:58:08 +0200647 /* If bi_vcnt == bi_max then this is a SW BUG */
648 ORE_DBGMSG("Failed bio_add_pc_page bi_vcnt=0x%x "
649 "bi_max=0x%x BIO_MAX=0x%x cur_len=0x%x\n",
650 per_dev->bio->bi_vcnt,
651 per_dev->bio->bi_max_vecs,
652 BIO_MAX_PAGES_KMALLOC, cur_len);
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700653 ret = -ENOMEM;
654 goto out;
655 }
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200656 _add_stripe_page(ios->sp2d, &ios->si, pages[pg]);
657
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200658 pgbase = 0;
659 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200660 }
661 BUG_ON(cur_len);
662
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700663 per_dev->length += len;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200664 *cur_pg = pg;
Boaz Harroshbbf9a312011-08-26 21:04:52 -0700665 ret = 0;
666out: /* we fail the complete unit on an error eg don't advance
667 * per_dev->length and cur_pg. This means that we might have a bigger
668 * bio than the CDB requested length (per_dev->length). That's fine
669 * only the oposite is fatal.
670 */
671 return ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200672}
673
Boaz Harroshce5d36a2014-05-22 14:48:15 +0300674static int _add_parity_units(struct ore_io_state *ios,
675 struct ore_striping_info *si,
676 unsigned dev, unsigned first_dev,
677 unsigned mirrors_p1, unsigned devs_in_group,
678 unsigned cur_len)
679{
680 unsigned do_parity;
681 int ret = 0;
682
683 for (do_parity = ios->layout->parity; do_parity; --do_parity) {
684 struct ore_per_dev_state *per_dev;
685
686 per_dev = &ios->per_dev[dev - first_dev];
687 if (!per_dev->length && !per_dev->offset) {
688 /* Only/always the parity unit of the first
689 * stripe will be empty. So this is a chance to
690 * initialize the per_dev info.
691 */
692 per_dev->dev = dev;
693 per_dev->offset = si->obj_offset - si->unit_off;
694 }
695
696 ret = _ore_add_parity_unit(ios, si, per_dev, cur_len,
697 do_parity == 1);
698 if (unlikely(ret))
699 break;
700
701 if (do_parity != 1) {
702 dev = ((dev + mirrors_p1) % devs_in_group) + first_dev;
703 si->cur_comp = (si->cur_comp + 1) %
704 ios->layout->group_width;
705 }
706 }
707
708 return ret;
709}
710
Boaz Harrosh98260752011-10-02 15:32:50 +0200711static int _prepare_for_striping(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200712{
Boaz Harrosh98260752011-10-02 15:32:50 +0200713 struct ore_striping_info *si = &ios->si;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200714 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200715 unsigned mirrors_p1 = ios->layout->mirrors_p1;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200716 unsigned group_width = ios->layout->group_width;
717 unsigned devs_in_group = group_width * mirrors_p1;
Boaz Harroshb367e782010-02-07 19:18:58 +0200718 unsigned dev = si->dev;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200719 unsigned first_dev = dev - (dev % devs_in_group);
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200720 unsigned cur_pg = ios->pages_consumed;
Boaz Harrosh98260752011-10-02 15:32:50 +0200721 u64 length = ios->length;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200722 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200723
Boaz Harrosh98260752011-10-02 15:32:50 +0200724 if (!ios->pages) {
Boaz Harrosh98260752011-10-02 15:32:50 +0200725 ios->numdevs = ios->layout->mirrors_p1;
726 return 0;
727 }
728
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200729 BUG_ON(length > si->length);
730
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200731 while (length) {
Boaz Harrosh101a6422014-04-03 17:53:31 +0300732 struct ore_per_dev_state *per_dev =
733 &ios->per_dev[dev - first_dev];
Boaz Harroshb367e782010-02-07 19:18:58 +0200734 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200735
Boaz Harroshce5d36a2014-05-22 14:48:15 +0300736 if (!per_dev->length && !per_dev->offset) {
737 /* First time initialize the per_dev info. */
Boaz Harroshb367e782010-02-07 19:18:58 +0200738 per_dev->dev = dev;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200739 if (dev == si->dev) {
740 WARN_ON(dev == si->par_dev);
Boaz Harroshb367e782010-02-07 19:18:58 +0200741 per_dev->offset = si->obj_offset;
742 cur_len = stripe_unit - si->unit_off;
743 page_off = si->unit_off & ~PAGE_MASK;
744 BUG_ON(page_off && (page_off != ios->pgbase));
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200745 } else {
Boaz Harroshce5d36a2014-05-22 14:48:15 +0300746 per_dev->offset = si->obj_offset - si->unit_off;
Boaz Harroshb367e782010-02-07 19:18:58 +0200747 cur_len = stripe_unit;
748 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200749 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200750 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200751 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200752 if (cur_len >= length)
753 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200754
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200755 ret = _ore_add_stripe_unit(ios, &cur_pg, page_off, ios->pages,
756 per_dev, cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200757 if (unlikely(ret))
758 goto out;
759
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200760 length -= cur_len;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200761
Boaz Harrosh101a6422014-04-03 17:53:31 +0300762 dev = ((dev + mirrors_p1) % devs_in_group) + first_dev;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200763 si->cur_comp = (si->cur_comp + 1) % group_width;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200764 if (unlikely((dev == si->par_dev) || (!length && ios->sp2d))) {
765 if (!length && ios->sp2d) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200766 /* If we are writing and this is the very last
767 * stripe. then operate on parity dev.
768 */
769 dev = si->par_dev;
Boaz Harrosh455682c2014-04-09 23:14:38 +0300770 /* If last stripe operate on parity comp */
771 si->cur_comp = group_width - ios->layout->parity;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200772 }
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200773
Boaz Harrosh101a6422014-04-03 17:53:31 +0300774 /* In writes cur_len just means if it's the
775 * last one. See _ore_add_parity_unit.
776 */
Boaz Harroshce5d36a2014-05-22 14:48:15 +0300777 ret = _add_parity_units(ios, si, dev, first_dev,
778 mirrors_p1, devs_in_group,
Boaz Harrosh101a6422014-04-03 17:53:31 +0300779 ios->sp2d ? length : cur_len);
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200780 if (unlikely(ret))
781 goto out;
782
783 /* Rotate next par_dev backwards with wraping */
784 si->par_dev = (devs_in_group + si->par_dev -
785 ios->layout->parity * mirrors_p1) %
786 devs_in_group + first_dev;
787 /* Next stripe, start fresh */
788 si->cur_comp = 0;
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200789 si->cur_pg = 0;
Boaz Harroshce5d36a2014-05-22 14:48:15 +0300790 si->obj_offset += cur_len;
791 si->unit_off = 0;
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200792 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200793 }
794out:
Boaz Harroshb916c5c2011-09-28 11:55:51 +0300795 ios->numdevs = devs_in_group;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200796 ios->pages_consumed = cur_pg;
Boaz Harrosh62b62ad2012-06-08 04:30:40 +0300797 return ret;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200798}
799
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700800int ore_create(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200801{
802 int i, ret;
803
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300804 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200805 struct osd_request *or;
806
Christoph Hellwigac613e42018-05-09 09:54:03 +0200807 or = osd_start_request(_ios_od(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200808 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700809 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200810 ret = -ENOMEM;
811 goto out;
812 }
813 ios->per_dev[i].or = or;
814 ios->numdevs++;
815
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700816 osd_req_create_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200817 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700818 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200819
820out:
821 return ret;
822}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700823EXPORT_SYMBOL(ore_create);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200824
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700825int ore_remove(struct ore_io_state *ios)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200826{
827 int i, ret;
828
Boaz Harrosh5bf696d2011-09-28 11:39:59 +0300829 for (i = 0; i < ios->oc->numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200830 struct osd_request *or;
831
Christoph Hellwigac613e42018-05-09 09:54:03 +0200832 or = osd_start_request(_ios_od(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200833 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700834 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200835 ret = -ENOMEM;
836 goto out;
837 }
838 ios->per_dev[i].or = or;
839 ios->numdevs++;
840
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700841 osd_req_remove_object(or, _ios_obj(ios, i));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200842 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700843 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200844
845out:
846 return ret;
847}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700848EXPORT_SYMBOL(ore_remove);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200849
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700850static int _write_mirror(struct ore_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200851{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700852 struct ore_per_dev_state *master_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200853 unsigned dev = ios->per_dev[cur_comp].dev;
854 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
855 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200856
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200857 if (ios->pages && !master_dev->length)
858 return 0; /* Just an empty slot */
859
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200860 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700861 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200862 struct osd_request *or;
863
Christoph Hellwigac613e42018-05-09 09:54:03 +0200864 or = osd_start_request(_ios_od(ios, dev));
Boaz Harrosh06886a52009-11-08 14:54:08 +0200865 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700866 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200867 ret = -ENOMEM;
868 goto out;
869 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200870 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200871
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200872 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200873 struct bio *bio;
874
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200875 if (per_dev != master_dev) {
Kent Overstreetbf800ef2012-09-06 15:35:02 -0700876 bio = bio_clone_kmalloc(master_dev->bio,
877 GFP_KERNEL);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200878 if (unlikely(!bio)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700879 ORE_DBGMSG(
Paul Bolle426d3102010-08-07 12:30:03 +0200880 "Failed to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200881 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200882 ret = -ENOMEM;
883 goto out;
884 }
885
Christoph Hellwig74d46992017-08-23 19:10:32 +0200886 bio->bi_disk = NULL;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200887 bio->bi_next = NULL;
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700888 per_dev->offset = master_dev->offset;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200889 per_dev->length = master_dev->length;
890 per_dev->bio = bio;
891 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200892 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200893 bio = master_dev->bio;
894 /* FIXME: bio_set_dir() */
Mike Christie95fe6c12016-06-05 14:31:48 -0500895 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200896 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200897
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300898 osd_req_write(or, _ios_obj(ios, cur_comp),
899 per_dev->offset, bio, per_dev->length);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700900 ORE_DBGMSG("write(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200901 "length=0x%llx dev=%d\n",
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300902 _LLU(_ios_obj(ios, cur_comp)->id),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700903 _LLU(per_dev->offset),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200904 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200905 } else if (ios->kern_buff) {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700906 per_dev->offset = ios->si.obj_offset;
907 per_dev->dev = ios->si.dev + dev;
908
909 /* no cross device without page array */
910 BUG_ON((ios->layout->group_width > 1) &&
911 (ios->si.unit_off + ios->length >
912 ios->layout->stripe_unit));
913
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300914 ret = osd_req_write_kern(or, _ios_obj(ios, cur_comp),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700915 per_dev->offset,
916 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200917 if (unlikely(ret))
918 goto out;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700919 ORE_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200920 "length=0x%llx dev=%d\n",
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300921 _LLU(_ios_obj(ios, cur_comp)->id),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700922 _LLU(per_dev->offset),
Boaz Harrosh6851a5e2011-08-24 18:10:49 -0700923 _LLU(ios->length), per_dev->dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200924 } else {
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300925 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700926 ORE_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh9e62bb42012-08-01 17:48:36 +0300927 _LLU(_ios_obj(ios, cur_comp)->id),
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700928 ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200929 }
930
931 if (ios->out_attr)
932 osd_req_add_set_attr_list(or, ios->out_attr,
933 ios->out_attr_len);
934
935 if (ios->in_attr)
936 osd_req_add_get_attr_list(or, ios->in_attr,
937 ios->in_attr_len);
938 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200939
940out:
941 return ret;
942}
943
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700944int ore_write(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200945{
946 int i;
947 int ret;
948
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200949 if (unlikely(ios->sp2d && !ios->r4w)) {
950 /* A library is attempting a RAID-write without providing
951 * a pages lock interface.
952 */
953 WARN_ON_ONCE(1);
954 return -ENOTSUPP;
955 }
956
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200957 ret = _prepare_for_striping(ios);
958 if (unlikely(ret))
959 return ret;
960
961 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700962 ret = _write_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200963 if (unlikely(ret))
964 return ret;
965 }
966
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700967 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200968 return ret;
969}
Boaz Harroshcf283ad2011-08-06 19:22:06 -0700970EXPORT_SYMBOL(ore_write);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200971
Boaz Harrosh769ba8d2011-10-14 15:33:51 +0200972int _ore_read_mirror(struct ore_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200973{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200974 struct osd_request *or;
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700975 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh9e9db452011-08-05 15:06:04 -0700976 struct osd_obj_id *obj = _ios_obj(ios, cur_comp);
977 unsigned first_dev = (unsigned)obj->id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200978
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200979 if (ios->pages && !per_dev->length)
980 return 0; /* Just an empty slot */
981
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200982 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Christoph Hellwigac613e42018-05-09 09:54:03 +0200983 or = osd_start_request(_ios_od(ios, first_dev));
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200984 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -0700985 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200986 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200987 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200988 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200989
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200990 if (ios->pages) {
Boaz Harrosha1fec1d2011-10-12 18:42:22 +0200991 if (per_dev->cur_sg) {
992 /* finalize the last sg_entry */
993 _ore_add_sg_seg(per_dev, 0, false);
994 if (unlikely(!per_dev->cur_sg))
995 return 0; /* Skip parity only device */
996
997 osd_req_read_sg(or, obj, per_dev->bio,
998 per_dev->sglist, per_dev->cur_sg);
999 } else {
1000 /* The no raid case */
1001 osd_req_read(or, obj, per_dev->offset,
1002 per_dev->bio, per_dev->length);
1003 }
1004
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001005 ORE_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
Boaz Harrosha1fec1d2011-10-12 18:42:22 +02001006 " dev=%d sg_len=%d\n", _LLU(obj->id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001007 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosha1fec1d2011-10-12 18:42:22 +02001008 first_dev, per_dev->cur_sg);
Boaz Harrosh46f4d972010-02-01 11:37:30 +02001009 } else {
Boaz Harrosh6851a5e2011-08-24 18:10:49 -07001010 BUG_ON(ios->kern_buff);
1011
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001012 osd_req_get_attributes(or, obj);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001013 ORE_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001014 _LLU(obj->id),
1015 ios->in_attr_len, first_dev);
Boaz Harrosh46f4d972010-02-01 11:37:30 +02001016 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +02001017 if (ios->out_attr)
1018 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
1019
1020 if (ios->in_attr)
1021 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
1022
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001023 return 0;
1024}
1025
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001026int ore_read(struct ore_io_state *ios)
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001027{
1028 int i;
1029 int ret;
1030
1031 ret = _prepare_for_striping(ios);
1032 if (unlikely(ret))
1033 return ret;
1034
1035 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
Boaz Harrosh769ba8d2011-10-14 15:33:51 +02001036 ret = _ore_read_mirror(ios, i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001037 if (unlikely(ret))
1038 return ret;
1039 }
1040
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001041 ret = ore_io_execute(ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001042 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001043}
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001044EXPORT_SYMBOL(ore_read);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001045
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001046int extract_attr_from_ios(struct ore_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +02001047{
1048 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
1049 void *iter = NULL;
1050 int nelem;
1051
1052 do {
1053 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001054 osd_req_decode_get_attr_list(ios->per_dev[0].or,
1055 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +02001056 if ((cur_attr.attr_page == attr->attr_page) &&
1057 (cur_attr.attr_id == attr->attr_id)) {
1058 attr->len = cur_attr.len;
1059 attr->val_ptr = cur_attr.val_ptr;
1060 return 0;
1061 }
1062 } while (iter);
1063
1064 return -EIO;
1065}
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001066EXPORT_SYMBOL(extract_attr_from_ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001067
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001068static int _truncate_mirrors(struct ore_io_state *ios, unsigned cur_comp,
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001069 struct osd_attr *attr)
1070{
1071 int last_comp = cur_comp + ios->layout->mirrors_p1;
1072
1073 for (; cur_comp < last_comp; ++cur_comp) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001074 struct ore_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001075 struct osd_request *or;
1076
Christoph Hellwigac613e42018-05-09 09:54:03 +02001077 or = osd_start_request(_ios_od(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001078 if (unlikely(!or)) {
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001079 ORE_ERR("%s: osd_start_request failed\n", __func__);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001080 return -ENOMEM;
1081 }
1082 per_dev->or = or;
1083
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001084 osd_req_set_attributes(or, _ios_obj(ios, cur_comp));
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001085 osd_req_add_set_attr_list(or, attr, 1);
1086 }
1087
1088 return 0;
1089}
1090
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001091struct _trunc_info {
Boaz Harrosheb507bc2011-08-10 14:17:28 -07001092 struct ore_striping_info si;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001093 u64 prev_group_obj_off;
1094 u64 next_group_obj_off;
1095
1096 unsigned first_group_dev;
1097 unsigned nex_group_dev;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001098};
1099
H Hartley Sweeten1958c7c22011-09-23 13:42:43 -07001100static void _calc_trunk_info(struct ore_layout *layout, u64 file_offset,
1101 struct _trunc_info *ti)
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001102{
1103 unsigned stripe_unit = layout->stripe_unit;
1104
Boaz Harrosha1fec1d2011-10-12 18:42:22 +02001105 ore_calc_stripe_info(layout, file_offset, 0, &ti->si);
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001106
1107 ti->prev_group_obj_off = ti->si.M * stripe_unit;
1108 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
1109
1110 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
1111 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001112}
1113
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001114int ore_truncate(struct ore_layout *layout, struct ore_components *oc,
Boaz Harrosh9e9db452011-08-05 15:06:04 -07001115 u64 size)
Boaz Harrosh06886a52009-11-08 14:54:08 +02001116{
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001117 struct ore_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001118 struct exofs_trunc_attr {
1119 struct osd_attr attr;
1120 __be64 newsize;
1121 } *size_attrs;
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001122 struct _trunc_info ti;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001123 int i, ret;
1124
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001125 ret = ore_get_io_state(layout, oc, &ios);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001126 if (unlikely(ret))
1127 return ret;
1128
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001129 _calc_trunk_info(ios->layout, size, &ti);
1130
Boaz Harroshb916c5c2011-09-28 11:55:51 +03001131 size_attrs = kcalloc(ios->oc->numdevs, sizeof(*size_attrs),
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001132 GFP_KERNEL);
1133 if (unlikely(!size_attrs)) {
1134 ret = -ENOMEM;
1135 goto out;
1136 }
Boaz Harrosh06886a52009-11-08 14:54:08 +02001137
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001138 ios->numdevs = ios->oc->numdevs;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001139
Boaz Harroshb916c5c2011-09-28 11:55:51 +03001140 for (i = 0; i < ios->numdevs; ++i) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001141 struct exofs_trunc_attr *size_attr = &size_attrs[i];
1142 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001143
Boaz Harrosh16f75bb2011-08-03 20:44:16 -07001144 if (i < ti.first_group_dev)
1145 obj_size = ti.prev_group_obj_off;
1146 else if (i >= ti.nex_group_dev)
1147 obj_size = ti.next_group_obj_off;
1148 else if (i < ti.si.dev) /* dev within this group */
1149 obj_size = ti.si.obj_offset +
1150 ios->layout->stripe_unit - ti.si.unit_off;
1151 else if (i == ti.si.dev)
1152 obj_size = ti.si.obj_offset;
1153 else /* i > ti.dev */
1154 obj_size = ti.si.obj_offset - ti.si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001155
1156 size_attr->newsize = cpu_to_be64(obj_size);
1157 size_attr->attr = g_attr_logical_length;
1158 size_attr->attr.val_ptr = &size_attr->newsize;
1159
Boaz Harroshaad560b2013-11-21 17:58:08 +02001160 ORE_DBGMSG2("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
Boaz Harrosh5bf696d2011-09-28 11:39:59 +03001161 _LLU(oc->comps->obj.id), _LLU(obj_size), i);
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001162 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
1163 &size_attr->attr);
1164 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +02001165 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +02001166 }
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001167 ret = ore_io_execute(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001168
1169out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +02001170 kfree(size_attrs);
Boaz Harrosh8ff660a2011-08-06 19:26:31 -07001171 ore_put_io_state(ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +02001172 return ret;
1173}
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001174EXPORT_SYMBOL(ore_truncate);
Boaz Harrosh85e44df2011-05-16 15:26:47 +03001175
1176const struct osd_attr g_attr_logical_length = ATTR_DEF(
1177 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
Boaz Harroshcf283ad2011-08-06 19:22:06 -07001178EXPORT_SYMBOL(g_attr_logical_length);