blob: 6e446b2670b911794bbbc1e031fe443ed87c0808 [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 Harrosh5d952b82010-02-01 13:35:51 +0200262/* REMOVEME: After review
263 Some quoteing from the standard
264
265 L = logical offset into the file
266 W = number of data components in a stripe
267 S = W * stripe_unit (S is Stripe length)
268 N = L / S (N is the stripe Number)
269 C = (L-(N*S)) / stripe_unit (C is the component)
270 O = (N*stripe_unit)+(L%stripe_unit) (O is the object's offset)
271*/
272
273static void _offset_dev_unit_off(struct exofs_io_state *ios, u64 file_offset,
274 u64 *obj_offset, unsigned *dev, unsigned *unit_off)
275{
276 unsigned stripe_unit = ios->layout->stripe_unit;
277 unsigned stripe_length = stripe_unit * ios->layout->group_width;
278 u64 stripe_no = file_offset;
279 unsigned stripe_mod = do_div(stripe_no, stripe_length);
280
281 *unit_off = stripe_mod % stripe_unit;
282 *obj_offset = stripe_no * stripe_unit + *unit_off;
283 *dev = stripe_mod / stripe_unit * ios->layout->mirrors_p1;
284}
285
286static int _add_stripe_unit(struct exofs_io_state *ios, unsigned *cur_bvec,
287 struct exofs_per_dev_state *per_dev, int cur_len)
288{
289 unsigned bv = *cur_bvec;
290 struct request_queue *q =
291 osd_request_queue(exofs_ios_od(ios, per_dev->dev));
292
293 per_dev->length += cur_len;
294
295 if (per_dev->bio == NULL) {
296 unsigned pages_in_stripe = ios->layout->group_width *
297 (ios->layout->stripe_unit / PAGE_SIZE);
298 unsigned bio_size = (ios->bio->bi_vcnt + pages_in_stripe) /
299 ios->layout->group_width;
300
301 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
302 if (unlikely(!per_dev->bio)) {
303 EXOFS_DBGMSG("Faild to allocate BIO size=%u\n",
304 bio_size);
305 return -ENOMEM;
306 }
307 }
308
309 while (cur_len > 0) {
310 int added_len;
311 struct bio_vec *bvec = &ios->bio->bi_io_vec[bv];
312
313 BUG_ON(ios->bio->bi_vcnt <= bv);
314 cur_len -= bvec->bv_len;
315
316 added_len = bio_add_pc_page(q, per_dev->bio, bvec->bv_page,
317 bvec->bv_len, bvec->bv_offset);
318 if (unlikely(bvec->bv_len != added_len))
319 return -ENOMEM;
320 ++bv;
321 }
322 BUG_ON(cur_len);
323
324 *cur_bvec = bv;
325 return 0;
326}
327
328static int _prepare_for_striping(struct exofs_io_state *ios)
329{
330 u64 length = ios->length;
331 u64 offset = ios->offset;
332 unsigned stripe_unit = ios->layout->stripe_unit;
333 unsigned comp = 0;
334 unsigned stripes = 0;
335 unsigned cur_bvec = 0;
336 int ret;
337
338 if (!ios->bio) {
339 if (ios->kern_buff) {
340 struct exofs_per_dev_state *per_dev = &ios->per_dev[0];
341 unsigned unit_off;
342
343 _offset_dev_unit_off(ios, offset, &per_dev->offset,
344 &per_dev->dev, &unit_off);
345 /* no cross device without page array */
346 BUG_ON((ios->layout->group_width > 1) &&
347 (unit_off + length > stripe_unit));
348 }
349 ios->numdevs = ios->layout->mirrors_p1;
350 return 0;
351 }
352
353 while (length) {
354 struct exofs_per_dev_state *per_dev = &ios->per_dev[comp];
355 unsigned cur_len;
356
357 if (!per_dev->length) {
358 unsigned unit_off;
359
360 _offset_dev_unit_off(ios, offset, &per_dev->offset,
361 &per_dev->dev, &unit_off);
362 stripes++;
363 cur_len = min_t(u64, stripe_unit - unit_off, length);
364 offset += cur_len;
365 } else {
366 cur_len = min_t(u64, stripe_unit, length);
367 }
368
369 ret = _add_stripe_unit(ios, &cur_bvec, per_dev, cur_len);
370 if (unlikely(ret))
371 goto out;
372
373 comp += ios->layout->mirrors_p1;
374 comp %= ios->layout->s_numdevs;
375
376 length -= cur_len;
377 }
378out:
379 ios->numdevs = stripes * ios->layout->mirrors_p1;
380 return ret;
381}
382
Boaz Harrosh06886a52009-11-08 14:54:08 +0200383int exofs_sbi_create(struct exofs_io_state *ios)
384{
385 int i, ret;
386
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200387 for (i = 0; i < ios->layout->s_numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200388 struct osd_request *or;
389
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200390 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200391 if (unlikely(!or)) {
392 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
393 ret = -ENOMEM;
394 goto out;
395 }
396 ios->per_dev[i].or = or;
397 ios->numdevs++;
398
399 osd_req_create_object(or, &ios->obj);
400 }
401 ret = exofs_io_execute(ios);
402
403out:
404 return ret;
405}
406
407int exofs_sbi_remove(struct exofs_io_state *ios)
408{
409 int i, ret;
410
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200411 for (i = 0; i < ios->layout->s_numdevs; i++) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200412 struct osd_request *or;
413
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200414 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200415 if (unlikely(!or)) {
416 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
417 ret = -ENOMEM;
418 goto out;
419 }
420 ios->per_dev[i].or = or;
421 ios->numdevs++;
422
423 osd_req_remove_object(or, &ios->obj);
424 }
425 ret = exofs_io_execute(ios);
426
427out:
428 return ret;
429}
430
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200431static int _sbi_write_mirror(struct exofs_io_state *ios, int cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200432{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200433 struct exofs_per_dev_state *master_dev = &ios->per_dev[cur_comp];
434 unsigned dev = ios->per_dev[cur_comp].dev;
435 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
436 int ret = 0;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200437
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200438 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
439 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh06886a52009-11-08 14:54:08 +0200440 struct osd_request *or;
441
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200442 or = osd_start_request(exofs_ios_od(ios, dev), GFP_KERNEL);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200443 if (unlikely(!or)) {
444 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
445 ret = -ENOMEM;
446 goto out;
447 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200448 per_dev->or = or;
449 per_dev->offset = master_dev->offset;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200450
451 if (ios->bio) {
452 struct bio *bio;
453
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200454 if (per_dev != master_dev) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200455 bio = bio_kmalloc(GFP_KERNEL,
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200456 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200457 if (unlikely(!bio)) {
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200458 EXOFS_DBGMSG(
459 "Faild to allocate BIO size=%u\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200460 master_dev->bio->bi_max_vecs);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200461 ret = -ENOMEM;
462 goto out;
463 }
464
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200465 __bio_clone(bio, master_dev->bio);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200466 bio->bi_bdev = NULL;
467 bio->bi_next = NULL;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200468 per_dev->length = master_dev->length;
469 per_dev->bio = bio;
470 per_dev->dev = dev;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200471 } else {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200472 bio = master_dev->bio;
473 /* FIXME: bio_set_dir() */
474 bio->bi_rw |= (1 << BIO_RW);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200475 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200476
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200477 osd_req_write(or, &ios->obj, per_dev->offset, bio,
478 per_dev->length);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200479 EXOFS_DBGMSG("write(0x%llx) offset=0x%llx "
480 "length=0x%llx dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200481 _LLU(ios->obj.id), _LLU(per_dev->offset),
482 _LLU(per_dev->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200483 } else if (ios->kern_buff) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200484 ret = osd_req_write_kern(or, &ios->obj, per_dev->offset,
Boaz Harrosh06886a52009-11-08 14:54:08 +0200485 ios->kern_buff, ios->length);
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200486 if (unlikely(ret))
487 goto out;
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200488 EXOFS_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
489 "length=0x%llx dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200490 _LLU(ios->obj.id), _LLU(per_dev->offset),
491 _LLU(ios->length), dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200492 } else {
493 osd_req_set_attributes(or, &ios->obj);
Boaz Harrosh34ce4e72009-12-15 19:34:17 +0200494 EXOFS_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200495 _LLU(ios->obj.id), ios->out_attr_len, dev);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200496 }
497
498 if (ios->out_attr)
499 osd_req_add_set_attr_list(or, ios->out_attr,
500 ios->out_attr_len);
501
502 if (ios->in_attr)
503 osd_req_add_get_attr_list(or, ios->in_attr,
504 ios->in_attr_len);
505 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200506
507out:
508 return ret;
509}
510
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200511int exofs_sbi_write(struct exofs_io_state *ios)
512{
513 int i;
514 int ret;
515
516 ret = _prepare_for_striping(ios);
517 if (unlikely(ret))
518 return ret;
519
520 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
521 ret = _sbi_write_mirror(ios, i);
522 if (unlikely(ret))
523 return ret;
524 }
525
526 ret = exofs_io_execute(ios);
527 return ret;
528}
529
530static int _sbi_read_mirror(struct exofs_io_state *ios, unsigned cur_comp)
Boaz Harrosh06886a52009-11-08 14:54:08 +0200531{
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200532 struct osd_request *or;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200533 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200534 unsigned first_dev = (unsigned)ios->obj.id;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200535
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200536 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
Boaz Harroshd9c740d2010-01-28 11:58:08 +0200537 or = osd_start_request(exofs_ios_od(ios, first_dev), GFP_KERNEL);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200538 if (unlikely(!or)) {
539 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
540 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200541 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200542 per_dev->or = or;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200543
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200544 if (ios->bio) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200545 osd_req_read(or, &ios->obj, per_dev->offset,
546 per_dev->bio, per_dev->length);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200547 EXOFS_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
548 " dev=%d\n", _LLU(ios->obj.id),
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200549 _LLU(per_dev->offset), _LLU(per_dev->length),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200550 first_dev);
551 } else if (ios->kern_buff) {
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200552 int ret = osd_req_read_kern(or, &ios->obj, per_dev->offset,
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200553 ios->kern_buff, ios->length);
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200554 EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
555 "length=0x%llx dev=%d ret=>%d\n",
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200556 _LLU(ios->obj.id), _LLU(per_dev->offset),
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200557 _LLU(ios->length), first_dev, ret);
558 if (unlikely(ret))
559 return ret;
560 } else {
561 osd_req_get_attributes(or, &ios->obj);
562 EXOFS_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
563 _LLU(ios->obj.id), ios->in_attr_len, first_dev);
564 }
Boaz Harrosh46f4d972010-02-01 11:37:30 +0200565 if (ios->out_attr)
566 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
567
568 if (ios->in_attr)
569 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
570
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200571 return 0;
572}
573
574int exofs_sbi_read(struct exofs_io_state *ios)
575{
576 int i;
577 int ret;
578
579 ret = _prepare_for_striping(ios);
580 if (unlikely(ret))
581 return ret;
582
583 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
584 ret = _sbi_read_mirror(ios, i);
585 if (unlikely(ret))
586 return ret;
587 }
588
589 ret = exofs_io_execute(ios);
590 return ret;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200591}
592
593int extract_attr_from_ios(struct exofs_io_state *ios, struct osd_attr *attr)
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200594{
595 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
596 void *iter = NULL;
597 int nelem;
598
599 do {
600 nelem = 1;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200601 osd_req_decode_get_attr_list(ios->per_dev[0].or,
602 &cur_attr, &nelem, &iter);
Boaz Harroshb14f8ab2008-10-27 18:27:55 +0200603 if ((cur_attr.attr_page == attr->attr_page) &&
604 (cur_attr.attr_id == attr->attr_id)) {
605 attr->len = cur_attr.len;
606 attr->val_ptr = cur_attr.val_ptr;
607 return 0;
608 }
609 } while (iter);
610
611 return -EIO;
612}
Boaz Harrosh06886a52009-11-08 14:54:08 +0200613
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200614static int _truncate_mirrors(struct exofs_io_state *ios, unsigned cur_comp,
615 struct osd_attr *attr)
616{
617 int last_comp = cur_comp + ios->layout->mirrors_p1;
618
619 for (; cur_comp < last_comp; ++cur_comp) {
620 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
621 struct osd_request *or;
622
623 or = osd_start_request(exofs_ios_od(ios, cur_comp), GFP_KERNEL);
624 if (unlikely(!or)) {
625 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
626 return -ENOMEM;
627 }
628 per_dev->or = or;
629
630 osd_req_set_attributes(or, &ios->obj);
631 osd_req_add_set_attr_list(or, attr, 1);
632 }
633
634 return 0;
635}
636
Boaz Harrosh06886a52009-11-08 14:54:08 +0200637int exofs_oi_truncate(struct exofs_i_info *oi, u64 size)
638{
639 struct exofs_sb_info *sbi = oi->vfs_inode.i_sb->s_fs_info;
640 struct exofs_io_state *ios;
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200641 struct exofs_trunc_attr {
642 struct osd_attr attr;
643 __be64 newsize;
644 } *size_attrs;
645 u64 this_obj_size;
646 unsigned dev;
647 unsigned unit_off;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200648 int i, ret;
649
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200650 ret = exofs_get_io_state(&sbi->layout, &ios);
651 if (unlikely(ret))
652 return ret;
653
654 size_attrs = kcalloc(ios->layout->group_width, sizeof(*size_attrs),
655 GFP_KERNEL);
656 if (unlikely(!size_attrs)) {
657 ret = -ENOMEM;
658 goto out;
659 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200660
661 ios->obj.id = exofs_oi_objno(oi);
662 ios->cred = oi->i_cred;
663
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200664 ios->numdevs = ios->layout->s_numdevs;
665 _offset_dev_unit_off(ios, size, &this_obj_size, &dev, &unit_off);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200666
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200667 for (i = 0; i < ios->layout->group_width; ++i) {
668 struct exofs_trunc_attr *size_attr = &size_attrs[i];
669 u64 obj_size;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200670
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200671 if (i < dev)
672 obj_size = this_obj_size +
673 ios->layout->stripe_unit - unit_off;
674 else if (i == dev)
675 obj_size = this_obj_size;
676 else /* i > dev */
677 obj_size = this_obj_size - unit_off;
678
679 size_attr->newsize = cpu_to_be64(obj_size);
680 size_attr->attr = g_attr_logical_length;
681 size_attr->attr.val_ptr = &size_attr->newsize;
682
683 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
684 &size_attr->attr);
685 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200686 goto out;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200687 }
688 ret = exofs_io_execute(ios);
689
690out:
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200691 kfree(size_attrs);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200692 exofs_put_io_state(ios);
693 return ret;
694}