blob: d28febdf54ab44d92d3e0d0fbb77a6a4d7b24716 [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
25#include <scsi/scsi_device.h>
Boaz Harrosh5d952b82010-02-01 13:35:51 +020026#include <asm/div64.h>
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020027
28#include "exofs.h"
29
Boaz Harrosh34ce4e72009-12-15 19:34:17 +020030#define EXOFS_DBGMSG2(M...) do {} while (0)
31/* #define EXOFS_DBGMSG2 EXOFS_DBGMSG */
32
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020033void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj)
34{
35 osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
36}
37
Boaz Harrosh06886a52009-11-08 14:54:08 +020038int exofs_read_kern(struct osd_dev *od, u8 *cred, struct osd_obj_id *obj,
39 u64 offset, void *p, unsigned length)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020040{
Boaz Harrosh06886a52009-11-08 14:54:08 +020041 struct osd_request *or = osd_start_request(od, GFP_KERNEL);
42/* struct osd_sense_info osi = {.key = 0};*/
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020043 int ret;
44
Boaz Harrosh06886a52009-11-08 14:54:08 +020045 if (unlikely(!or)) {
46 EXOFS_DBGMSG("%s: osd_start_request failed.\n", __func__);
47 return -ENOMEM;
48 }
49 ret = osd_req_read_kern(or, obj, offset, p, length);
50 if (unlikely(ret)) {
51 EXOFS_DBGMSG("%s: osd_req_read_kern failed.\n", __func__);
52 goto out;
53 }
54
55 ret = osd_finalize_request(or, 0, cred, NULL);
56 if (unlikely(ret)) {
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020057 EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n", ret);
Boaz Harrosh06886a52009-11-08 14:54:08 +020058 goto out;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020059 }
60
61 ret = osd_execute_request(or);
Boaz Harrosh06886a52009-11-08 14:54:08 +020062 if (unlikely(ret))
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020063 EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
64 /* osd_req_decode_sense(or, ret); */
Boaz Harrosh06886a52009-11-08 14:54:08 +020065
66out:
67 osd_end_request(or);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020068 return ret;
69}
70
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020071int exofs_get_io_state(struct exofs_layout *layout,
72 struct exofs_io_state **pios)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020073{
Boaz Harrosh06886a52009-11-08 14:54:08 +020074 struct exofs_io_state *ios;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020075
Boaz Harrosh06886a52009-11-08 14:54:08 +020076 /*TODO: Maybe use kmem_cach per sbi of size
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020077 * exofs_io_state_size(layout->s_numdevs)
Boaz Harrosh06886a52009-11-08 14:54:08 +020078 */
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020079 ios = kzalloc(exofs_io_state_size(layout->s_numdevs), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +020080 if (unlikely(!ios)) {
Boaz Harrosh34ce4e72009-12-15 19:34:17 +020081 EXOFS_DBGMSG("Faild kzalloc bytes=%d\n",
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020082 exofs_io_state_size(layout->s_numdevs));
Boaz Harrosh06886a52009-11-08 14:54:08 +020083 *pios = NULL;
84 return -ENOMEM;
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020085 }
86
Boaz Harrosh45d3abc2010-01-28 11:46:16 +020087 ios->layout = layout;
88 ios->obj.partition = layout->s_pid;
Boaz Harrosh06886a52009-11-08 14:54:08 +020089 *pios = ios;
90 return 0;
91}
Boaz Harroshb14f8ab2008-10-27 18:27:55 +020092
Boaz Harrosh06886a52009-11-08 14:54:08 +020093void exofs_put_io_state(struct exofs_io_state *ios)
94{
95 if (ios) {
96 unsigned i;
97
98 for (i = 0; i < ios->numdevs; i++) {
99 struct exofs_per_dev_state *per_dev = &ios->per_dev[i];
100
101 if (per_dev->or)
102 osd_end_request(per_dev->or);
103 if (per_dev->bio)
104 bio_put(per_dev->bio);
105 }
106
107 kfree(ios);
108 }
109}
110
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200111unsigned exofs_layout_od_id(struct exofs_layout *layout,
112 osd_id obj_no, unsigned layout_index)
113{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200114/* switch (layout->lay_func) {
115 case LAYOUT_MOVING_WINDOW:
116 {*/
117 unsigned dev_mod = obj_no;
118
119 return (layout_index + dev_mod * layout->mirrors_p1) %
120 layout->s_numdevs;
121/* }
122 case LAYOUT_FUNC_IMPLICT:
123 return layout->devs[layout_index];
124 }*/
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200125}
126
127static inline struct osd_dev *exofs_ios_od(struct exofs_io_state *ios,
128 unsigned layout_index)
129{
130 return ios->layout->s_ods[
131 exofs_layout_od_id(ios->layout, ios->obj.id, layout_index)];
132}
133
Boaz Harrosh06886a52009-11-08 14:54:08 +0200134static void _sync_done(struct exofs_io_state *ios, void *p)
135{
136 struct completion *waiting = p;
137
138 complete(waiting);
139}
140
141static void _last_io(struct kref *kref)
142{
143 struct exofs_io_state *ios = container_of(
144 kref, struct exofs_io_state, kref);
145
146 ios->done(ios, ios->private);
147}
148
149static void _done_io(struct osd_request *or, void *p)
150{
151 struct exofs_io_state *ios = p;
152
153 kref_put(&ios->kref, _last_io);
154}
155
156static int exofs_io_execute(struct exofs_io_state *ios)
157{
158 DECLARE_COMPLETION_ONSTACK(wait);
159 bool sync = (ios->done == NULL);
160 int i, ret;
161
162 if (sync) {
163 ios->done = _sync_done;
164 ios->private = &wait;
165 }
166
167 for (i = 0; i < ios->numdevs; i++) {
168 struct osd_request *or = ios->per_dev[i].or;
169 if (unlikely(!or))
170 continue;
171
172 ret = osd_finalize_request(or, 0, ios->cred, NULL);
173 if (unlikely(ret)) {
174 EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n",
175 ret);
176 return ret;
177 }
178 }
179
180 kref_init(&ios->kref);
181
182 for (i = 0; i < ios->numdevs; i++) {
183 struct osd_request *or = ios->per_dev[i].or;
184 if (unlikely(!or))
185 continue;
186
187 kref_get(&ios->kref);
188 osd_execute_request_async(or, _done_io, ios);
189 }
190
191 kref_put(&ios->kref, _last_io);
192 ret = 0;
193
194 if (sync) {
195 wait_for_completion(&wait);
196 ret = exofs_check_io(ios, NULL);
197 }
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200198 return ret;
199}
200
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200201static void _clear_bio(struct bio *bio)
202{
203 struct bio_vec *bv;
204 unsigned i;
205
206 __bio_for_each_segment(bv, bio, i, 0) {
207 unsigned this_count = bv->bv_len;
208
209 if (likely(PAGE_SIZE == this_count))
210 clear_highpage(bv->bv_page);
211 else
212 zero_user(bv->bv_page, bv->bv_offset, this_count);
213 }
214}
215
Boaz Harrosh06886a52009-11-08 14:54:08 +0200216int exofs_check_io(struct exofs_io_state *ios, u64 *resid)
217{
218 enum osd_err_priority acumulated_osd_err = 0;
219 int acumulated_lin_err = 0;
220 int i;
221
222 for (i = 0; i < ios->numdevs; i++) {
223 struct osd_sense_info osi;
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200224 struct osd_request *or = ios->per_dev[i].or;
225 int ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200226
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200227 if (unlikely(!or))
228 continue;
229
230 ret = osd_req_decode_sense(or, &osi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200231 if (likely(!ret))
232 continue;
233
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200234 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
235 /* start read offset passed endof file */
236 _clear_bio(ios->per_dev[i].bio);
237 EXOFS_DBGMSG("start read offset passed end of file "
238 "offset=0x%llx, length=0x%llx\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200239 _LLU(ios->per_dev[i].offset),
240 _LLU(ios->per_dev[i].length));
Boaz Harrosh22ddc552010-01-19 19:24:45 +0200241
242 continue; /* we recovered */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200243 }
244
245 if (osi.osd_err_pri >= acumulated_osd_err) {
246 acumulated_osd_err = osi.osd_err_pri;
247 acumulated_lin_err = ret;
248 }
249 }
250
251 /* TODO: raid specific residual calculations */
252 if (resid) {
253 if (likely(!acumulated_lin_err))
254 *resid = 0;
255 else
256 *resid = ios->length;
257 }
258
259 return acumulated_lin_err;
260}
261
Boaz Harroshb367e782010-02-07 19:18:58 +0200262/*
263 * L - logical offset into the file
264 *
265 * U - The number of bytes in a full stripe
266 *
267 * U = stripe_unit * group_width
268 *
269 * N - The stripe number
270 *
271 * N = L / U
272 *
273 * C - The component index coresponding to L
274 *
275 * C = (L - (N*U)) / stripe_unit
276 *
277 * O - The component offset coresponding to L
278 *
279 * (N*stripe_unit)+(L%stripe_unit)
280 */
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200281
Boaz Harroshb367e782010-02-07 19:18:58 +0200282struct _striping_info {
283 u64 obj_offset;
284 unsigned dev;
285 unsigned unit_off;
286};
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200287
Boaz Harroshb367e782010-02-07 19:18:58 +0200288static void _calc_stripe_info(struct exofs_io_state *ios, u64 file_offset,
289 struct _striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200290{
Boaz Harroshb367e782010-02-07 19:18:58 +0200291 u32 stripe_unit = ios->layout->stripe_unit;
292 u32 group_width = ios->layout->group_width;
293 u32 U = stripe_unit * group_width;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200294
Boaz Harroshb367e782010-02-07 19:18:58 +0200295 u32 LmodU;
296 u64 N = div_u64_rem(file_offset, U, &LmodU);
297
298 si->unit_off = LmodU % stripe_unit;
299 si->obj_offset = N * stripe_unit + si->unit_off;
300 si->dev = LmodU / stripe_unit;
301 si->dev *= ios->layout->mirrors_p1;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200302}
303
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200304static int _add_stripe_unit(struct exofs_io_state *ios, unsigned *cur_pg,
305 unsigned pgbase, struct exofs_per_dev_state *per_dev,
306 int cur_len)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200307{
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200308 unsigned pg = *cur_pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200309 struct request_queue *q =
310 osd_request_queue(exofs_ios_od(ios, per_dev->dev));
311
312 per_dev->length += cur_len;
313
314 if (per_dev->bio == NULL) {
315 unsigned pages_in_stripe = ios->layout->group_width *
316 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200317 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200318 ios->layout->group_width;
319
320 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
321 if (unlikely(!per_dev->bio)) {
322 EXOFS_DBGMSG("Faild to allocate BIO size=%u\n",
323 bio_size);
324 return -ENOMEM;
325 }
326 }
327
328 while (cur_len > 0) {
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200329 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
330 unsigned added_len;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200331
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200332 BUG_ON(ios->nr_pages <= pg);
333 cur_len -= pglen;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200334
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200335 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
336 pglen, pgbase);
337 if (unlikely(pglen != added_len))
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200338 return -ENOMEM;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200339 pgbase = 0;
340 ++pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200341 }
342 BUG_ON(cur_len);
343
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200344 *cur_pg = pg;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200345 return 0;
346}
347
Boaz Harroshb367e782010-02-07 19:18:58 +0200348static int _prepare_pages(struct exofs_io_state *ios,
349 struct _striping_info *si)
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200350{
351 u64 length = ios->length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200352 unsigned stripe_unit = ios->layout->stripe_unit;
Boaz Harroshb367e782010-02-07 19:18:58 +0200353 unsigned mirrors_p1 = ios->layout->mirrors_p1;
354 unsigned dev = si->dev;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200355 unsigned comp = 0;
356 unsigned stripes = 0;
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200357 unsigned cur_pg = 0;
358 int ret = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200359
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200360 while (length) {
361 struct exofs_per_dev_state *per_dev = &ios->per_dev[comp];
Boaz Harroshb367e782010-02-07 19:18:58 +0200362 unsigned cur_len, page_off = 0;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200363
364 if (!per_dev->length) {
Boaz Harroshb367e782010-02-07 19:18:58 +0200365 per_dev->dev = dev;
366 if (dev < si->dev) {
367 per_dev->offset = si->obj_offset + stripe_unit -
368 si->unit_off;
369 cur_len = stripe_unit;
370 } else if (dev == si->dev) {
371 per_dev->offset = si->obj_offset;
372 cur_len = stripe_unit - si->unit_off;
373 page_off = si->unit_off & ~PAGE_MASK;
374 BUG_ON(page_off && (page_off != ios->pgbase));
375 } else { /* dev > si->dev */
376 per_dev->offset = si->obj_offset - si->unit_off;
377 cur_len = stripe_unit;
378 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200379
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200380 stripes++;
Boaz Harroshb367e782010-02-07 19:18:58 +0200381
382 dev += mirrors_p1;
383 dev %= ios->layout->s_numdevs;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200384 } else {
Boaz Harroshb367e782010-02-07 19:18:58 +0200385 cur_len = stripe_unit;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200386 }
Boaz Harroshb367e782010-02-07 19:18:58 +0200387 if (cur_len >= length)
388 cur_len = length;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200389
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200390 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
391 cur_len);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200392 if (unlikely(ret))
393 goto out;
394
Boaz Harroshb367e782010-02-07 19:18:58 +0200395 comp += mirrors_p1;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200396 comp %= ios->layout->s_numdevs;
397
398 length -= cur_len;
399 }
400out:
Boaz Harroshb367e782010-02-07 19:18:58 +0200401 ios->numdevs = stripes * mirrors_p1;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200402 return ret;
403}
404
Boaz Harroshb367e782010-02-07 19:18:58 +0200405static int _prepare_for_striping(struct exofs_io_state *ios)
406{
407 struct _striping_info si;
408
409 _calc_stripe_info(ios, ios->offset, &si);
410
411 if (!ios->pages) {
412 if (ios->kern_buff) {
413 struct exofs_per_dev_state *per_dev = &ios->per_dev[0];
414
415 per_dev->offset = si.obj_offset;
416 per_dev->dev = si.dev;
417
418 /* no cross device without page array */
419 BUG_ON((ios->layout->group_width > 1) &&
420 (si.unit_off + ios->length >
421 ios->layout->stripe_unit));
422 }
423 ios->numdevs = ios->layout->mirrors_p1;
424 return 0;
425 }
426
427 return _prepare_pages(ios, &si);
428}
429
Boaz Harrosh06886a52009-11-08 14:54:08 +0200430int exofs_sbi_create(struct exofs_io_state *ios)
431{
432 int i, ret;
433
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200434 for (i = 0; i < ios->layout->s_numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200435 struct osd_request *or;
436
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200437 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200438 if (unlikely(!or)) {
439 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
440 ret = -ENOMEM;
441 goto out;
442 }
443 ios->per_dev[i].or = or;
444 ios->numdevs++;
445
446 osd_req_create_object(or, &ios->obj);
447 }
448 ret = exofs_io_execute(ios);
449
450out:
451 return ret;
452}
453
454int exofs_sbi_remove(struct exofs_io_state *ios)
455{
456 int i, ret;
457
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200458 for (i = 0; i < ios->layout->s_numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200459 struct osd_request *or;
460
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200461 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200462 if (unlikely(!or)) {
463 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
464 ret = -ENOMEM;
465 goto out;
466 }
467 ios->per_dev[i].or = or;
468 ios->numdevs++;
469
470 osd_req_remove_object(or, &ios->obj);
471 }
472 ret = exofs_io_execute(ios);
473
474out:
475 return ret;
476}
477
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200478static int _sbi_write_mirror(struct exofs_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200479{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200480 struct exofs_per_dev_state *master_dev = &ios->per_dev[cur_comp];
481 unsigned dev = ios->per_dev[cur_comp].dev;
482 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
483 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200484
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200485 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
486 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200487 struct osd_request *or;
488
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200489 or = osd_start_request(exofs_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200490 if (unlikely(!or)) {
491 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
492 ret = -ENOMEM;
493 goto out;
494 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200495 per_dev->or = or;
496 per_dev->offset = master_dev->offset;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200497
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200498 if (ios->pages) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200499 struct bio *bio;
500
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200501 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200502 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200503 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200504 if (unlikely(!bio)) {
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200505 EXOFS_DBGMSG(
506 "Faild to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200507 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200508 ret = -ENOMEM;
509 goto out;
510 }
511
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200512 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200513 bio->bi_bdev = NULL;
514 bio->bi_next = NULL;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200515 per_dev->length = master_dev->length;
516 per_dev->bio = bio;
517 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200518 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200519 bio = master_dev->bio;
520 /* FIXME: bio_set_dir() */
521 bio->bi_rw |= (1 << BIO_RW);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200522 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200523
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200524 osd_req_write(or, &ios->obj, per_dev->offset, bio,
525 per_dev->length);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200526 EXOFS_DBGMSG("write(0x%llx) offset=0x%llx "
527 "length=0x%llx dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200528 _LLU(ios->obj.id), _LLU(per_dev->offset),
529 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200530 } else if (ios->kern_buff) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200531 ret = osd_req_write_kern(or, &ios->obj, per_dev->offset,
Boaz Harrosh06886a52009-11-08 14:54:08 +0200532 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200533 if (unlikely(ret))
534 goto out;
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200535 EXOFS_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
536 "length=0x%llx dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200537 _LLU(ios->obj.id), _LLU(per_dev->offset),
538 _LLU(ios->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200539 } else {
540 osd_req_set_attributes(or, &ios->obj);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200541 EXOFS_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200542 _LLU(ios->obj.id), ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200543 }
544
545 if (ios->out_attr)
546 osd_req_add_set_attr_list(or, ios->out_attr,
547 ios->out_attr_len);
548
549 if (ios->in_attr)
550 osd_req_add_get_attr_list(or, ios->in_attr,
551 ios->in_attr_len);
552 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200553
554out:
555 return ret;
556}
557
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200558int exofs_sbi_write(struct exofs_io_state *ios)
559{
560 int i;
561 int ret;
562
563 ret = _prepare_for_striping(ios);
564 if (unlikely(ret))
565 return ret;
566
567 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
568 ret = _sbi_write_mirror(ios, i);
569 if (unlikely(ret))
570 return ret;
571 }
572
573 ret = exofs_io_execute(ios);
574 return ret;
575}
576
577static int _sbi_read_mirror(struct exofs_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200578{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200579 struct osd_request *or;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200580 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200581 unsigned first_dev = (unsigned)ios->obj.id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200582
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200583 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200584 or = osd_start_request(exofs_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200585 if (unlikely(!or)) {
586 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
587 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200588 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200589 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200590
Boaz Harrosh86093aa2010-01-28 18:24:06 +0200591 if (ios->pages) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200592 osd_req_read(or, &ios->obj, per_dev->offset,
593 per_dev->bio, per_dev->length);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200594 EXOFS_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
595 " dev=%d\n", _LLU(ios->obj.id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200596 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200597 first_dev);
598 } else if (ios->kern_buff) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200599 int ret = osd_req_read_kern(or, &ios->obj, per_dev->offset,
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200600 ios->kern_buff, ios->length);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200601 EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
602 "length=0x%llx dev=%d ret=>%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200603 _LLU(ios->obj.id), _LLU(per_dev->offset),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200604 _LLU(ios->length), first_dev, ret);
605 if (unlikely(ret))
606 return ret;
607 } else {
608 osd_req_get_attributes(or, &ios->obj);
609 EXOFS_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
610 _LLU(ios->obj.id), ios->in_attr_len, first_dev);
611 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200612 if (ios->out_attr)
613 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
614
615 if (ios->in_attr)
616 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
617
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200618 return 0;
619}
620
621int exofs_sbi_read(struct exofs_io_state *ios)
622{
623 int i;
624 int ret;
625
626 ret = _prepare_for_striping(ios);
627 if (unlikely(ret))
628 return ret;
629
630 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
631 ret = _sbi_read_mirror(ios, i);
632 if (unlikely(ret))
633 return ret;
634 }
635
636 ret = exofs_io_execute(ios);
637 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200638}
639
640int extract_attr_from_ios(struct exofs_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200641{
642 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
643 void *iter = NULL;
644 int nelem;
645
646 do {
647 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200648 osd_req_decode_get_attr_list(ios->per_dev[0].or,
649 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200650 if ((cur_attr.attr_page == attr->attr_page) &&
651 (cur_attr.attr_id == attr->attr_id)) {
652 attr->len = cur_attr.len;
653 attr->val_ptr = cur_attr.val_ptr;
654 return 0;
655 }
656 } while (iter);
657
658 return -EIO;
659}
Boaz Harrosh06886a52009-11-08 14:54:08 +0200660
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200661static int _truncate_mirrors(struct exofs_io_state *ios, unsigned cur_comp,
662 struct osd_attr *attr)
663{
664 int last_comp = cur_comp + ios->layout->mirrors_p1;
665
666 for (; cur_comp < last_comp; ++cur_comp) {
667 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
668 struct osd_request *or;
669
670 or = osd_start_request(exofs_ios_od(ios, cur_comp), GFP_KERNEL);
671 if (unlikely(!or)) {
672 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
673 return -ENOMEM;
674 }
675 per_dev->or = or;
676
677 osd_req_set_attributes(or, &ios->obj);
678 osd_req_add_set_attr_list(or, attr, 1);
679 }
680
681 return 0;
682}
683
Boaz Harrosh06886a52009-11-08 14:54:08 +0200684int exofs_oi_truncate(struct exofs_i_info *oi, u64 size)
685{
686 struct exofs_sb_info *sbi = oi->vfs_inode.i_sb->s_fs_info;
687 struct exofs_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200688 struct exofs_trunc_attr {
689 struct osd_attr attr;
690 __be64 newsize;
691 } *size_attrs;
Boaz Harroshb367e782010-02-07 19:18:58 +0200692 struct _striping_info si;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200693 int i, ret;
694
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200695 ret = exofs_get_io_state(&sbi->layout, &ios);
696 if (unlikely(ret))
697 return ret;
698
699 size_attrs = kcalloc(ios->layout->group_width, sizeof(*size_attrs),
700 GFP_KERNEL);
701 if (unlikely(!size_attrs)) {
702 ret = -ENOMEM;
703 goto out;
704 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200705
706 ios->obj.id = exofs_oi_objno(oi);
707 ios->cred = oi->i_cred;
708
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200709 ios->numdevs = ios->layout->s_numdevs;
Boaz Harroshb367e782010-02-07 19:18:58 +0200710 _calc_stripe_info(ios, size, &si);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200711
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200712 for (i = 0; i < ios->layout->group_width; ++i) {
713 struct exofs_trunc_attr *size_attr = &size_attrs[i];
714 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200715
Boaz Harroshb367e782010-02-07 19:18:58 +0200716 if (i < si.dev)
717 obj_size = si.obj_offset +
718 ios->layout->stripe_unit - si.unit_off;
719 else if (i == si.dev)
720 obj_size = si.obj_offset;
721 else /* i > si.dev */
722 obj_size = si.obj_offset - si.unit_off;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200723
724 size_attr->newsize = cpu_to_be64(obj_size);
725 size_attr->attr = g_attr_logical_length;
726 size_attr->attr.val_ptr = &size_attr->newsize;
727
728 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
729 &size_attr->attr);
730 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200731 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200732 }
733 ret = exofs_io_execute(ios);
734
735out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200736 kfree(size_attrs);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200737 exofs_put_io_state(ios);
738 return ret;
739}