2 * Copyright (C) 2005, 2006
3 * Avishay Traeger (avishay@gmail.com)
4 * Copyright (C) 2008, 2009
5 * Boaz Harrosh <bharrosh@panasas.com>
7 * This file is part of exofs.
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.
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.
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
25 #include <linux/slab.h>
26 #include <scsi/scsi_device.h>
27 #include <asm/div64.h>
31 #define EXOFS_DBGMSG2(M...) do {} while (0)
32 /* #define EXOFS_DBGMSG2 EXOFS_DBGMSG */
34 int exofs_get_rw_state(struct exofs_layout *layout, bool is_reading,
35 u64 offset, u64 length, struct exofs_io_state **pios)
37 struct exofs_io_state *ios;
39 /*TODO: Maybe use kmem_cach per sbi of size
40 * exofs_io_state_size(layout->s_numdevs)
42 ios = kzalloc(exofs_io_state_size(layout->s_numdevs), GFP_KERNEL);
44 EXOFS_DBGMSG("Failed kzalloc bytes=%d\n",
45 exofs_io_state_size(layout->s_numdevs));
51 ios->obj.partition = layout->s_pid;
54 ios->reading = is_reading;
60 int exofs_get_io_state(struct exofs_layout *layout,
61 struct exofs_io_state **ios)
63 return exofs_get_rw_state(layout, true, 0, 0, ios);
66 void exofs_put_io_state(struct exofs_io_state *ios)
71 for (i = 0; i < ios->numdevs; i++) {
72 struct exofs_per_dev_state *per_dev = &ios->per_dev[i];
75 osd_end_request(per_dev->or);
77 bio_put(per_dev->bio);
84 static void _sync_done(struct exofs_io_state *ios, void *p)
86 struct completion *waiting = p;
91 static void _last_io(struct kref *kref)
93 struct exofs_io_state *ios = container_of(
94 kref, struct exofs_io_state, kref);
96 ios->done(ios, ios->private);
99 static void _done_io(struct osd_request *or, void *p)
101 struct exofs_io_state *ios = p;
103 kref_put(&ios->kref, _last_io);
106 static int exofs_io_execute(struct exofs_io_state *ios)
108 DECLARE_COMPLETION_ONSTACK(wait);
109 bool sync = (ios->done == NULL);
113 ios->done = _sync_done;
114 ios->private = &wait;
117 for (i = 0; i < ios->numdevs; i++) {
118 struct osd_request *or = ios->per_dev[i].or;
122 ret = osd_finalize_request(or, 0, ios->cred, NULL);
124 EXOFS_DBGMSG("Failed to osd_finalize_request() => %d\n",
130 kref_init(&ios->kref);
132 for (i = 0; i < ios->numdevs; i++) {
133 struct osd_request *or = ios->per_dev[i].or;
137 kref_get(&ios->kref);
138 osd_execute_request_async(or, _done_io, ios);
141 kref_put(&ios->kref, _last_io);
145 wait_for_completion(&wait);
146 ret = exofs_check_io(ios, NULL);
151 static void _clear_bio(struct bio *bio)
156 __bio_for_each_segment(bv, bio, i, 0) {
157 unsigned this_count = bv->bv_len;
159 if (likely(PAGE_SIZE == this_count))
160 clear_highpage(bv->bv_page);
162 zero_user(bv->bv_page, bv->bv_offset, this_count);
166 int exofs_check_io(struct exofs_io_state *ios, u64 *resid)
168 enum osd_err_priority acumulated_osd_err = 0;
169 int acumulated_lin_err = 0;
172 for (i = 0; i < ios->numdevs; i++) {
173 struct osd_sense_info osi;
174 struct osd_request *or = ios->per_dev[i].or;
180 ret = osd_req_decode_sense(or, &osi);
184 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
185 /* start read offset passed endof file */
186 _clear_bio(ios->per_dev[i].bio);
187 EXOFS_DBGMSG("start read offset passed end of file "
188 "offset=0x%llx, length=0x%llx\n",
189 _LLU(ios->per_dev[i].offset),
190 _LLU(ios->per_dev[i].length));
192 continue; /* we recovered */
195 if (osi.osd_err_pri >= acumulated_osd_err) {
196 acumulated_osd_err = osi.osd_err_pri;
197 acumulated_lin_err = ret;
201 /* TODO: raid specific residual calculations */
203 if (likely(!acumulated_lin_err))
206 *resid = ios->length;
209 return acumulated_lin_err;
213 * L - logical offset into the file
215 * U - The number of bytes in a stripe within a group
217 * U = stripe_unit * group_width
219 * T - The number of bytes striped within a group of component objects
220 * (before advancing to the next group)
222 * T = stripe_unit * group_width * group_depth
224 * S - The number of bytes striped across all component objects
225 * before the pattern repeats
227 * S = stripe_unit * group_width * group_depth * group_count
229 * M - The "major" (i.e., across all components) stripe number
233 * G - Counts the groups from the beginning of the major stripe
235 * G = (L - (M * S)) / T [or (L % S) / T]
237 * H - The byte offset within the group
239 * H = (L - (M * S)) % T [or (L % S) % T]
241 * N - The "minor" (i.e., across the group) stripe number
245 * C - The component index coresponding to L
247 * C = (H - (N * U)) / stripe_unit + G * group_width
248 * [or (L % U) / stripe_unit + G * group_width]
250 * O - The component offset coresponding to L
252 * O = L % stripe_unit + N * stripe_unit + M * group_depth * stripe_unit
254 struct _striping_info {
257 u64 M; /* for truncate */
262 static void _calc_stripe_info(struct exofs_layout *layout, u64 file_offset,
263 struct _striping_info *si)
265 u32 stripe_unit = layout->stripe_unit;
266 u32 group_width = layout->group_width;
267 u64 group_depth = layout->group_depth;
269 u32 U = stripe_unit * group_width;
270 u64 T = U * group_depth;
271 u64 S = T * layout->group_count;
272 u64 M = div64_u64(file_offset, S);
275 G = (L - (M * S)) / T
276 H = (L - (M * S)) % T
278 u64 LmodS = file_offset - M * S;
279 u32 G = div64_u64(LmodS, T);
280 u64 H = LmodS - G * T;
282 u32 N = div_u64(H, U);
284 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
285 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
286 si->dev *= layout->mirrors_p1;
288 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
290 si->obj_offset = si->unit_off + (N * stripe_unit) +
291 (M * group_depth * stripe_unit);
293 si->group_length = T - H;
297 static int _add_stripe_unit(struct exofs_io_state *ios, unsigned *cur_pg,
298 unsigned pgbase, struct exofs_per_dev_state *per_dev,
301 unsigned pg = *cur_pg;
302 struct request_queue *q =
303 osd_request_queue(exofs_ios_od(ios, per_dev->dev));
305 per_dev->length += cur_len;
307 if (per_dev->bio == NULL) {
308 unsigned pages_in_stripe = ios->layout->group_width *
309 (ios->layout->stripe_unit / PAGE_SIZE);
310 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
311 ios->layout->group_width;
313 per_dev->bio = bio_kmalloc(GFP_KERNEL, bio_size);
314 if (unlikely(!per_dev->bio)) {
315 EXOFS_DBGMSG("Failed to allocate BIO size=%u\n",
321 while (cur_len > 0) {
322 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
325 BUG_ON(ios->nr_pages <= pg);
328 added_len = bio_add_pc_page(q, per_dev->bio, ios->pages[pg],
330 if (unlikely(pglen != added_len))
341 static int _prepare_one_group(struct exofs_io_state *ios, u64 length,
342 struct _striping_info *si)
344 unsigned stripe_unit = ios->layout->stripe_unit;
345 unsigned mirrors_p1 = ios->layout->mirrors_p1;
346 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
347 unsigned dev = si->dev;
348 unsigned first_dev = dev - (dev % devs_in_group);
349 unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
350 unsigned cur_pg = ios->pages_consumed;
354 struct exofs_per_dev_state *per_dev = &ios->per_dev[dev];
355 unsigned cur_len, page_off = 0;
357 if (!per_dev->length) {
360 per_dev->offset = si->obj_offset + stripe_unit -
362 cur_len = stripe_unit;
363 } else if (dev == si->dev) {
364 per_dev->offset = si->obj_offset;
365 cur_len = stripe_unit - si->unit_off;
366 page_off = si->unit_off & ~PAGE_MASK;
367 BUG_ON(page_off && (page_off != ios->pgbase));
368 } else { /* dev > si->dev */
369 per_dev->offset = si->obj_offset - si->unit_off;
370 cur_len = stripe_unit;
376 cur_len = stripe_unit;
378 if (cur_len >= length)
381 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
387 dev = (dev % devs_in_group) + first_dev;
392 ios->numdevs = max_comp + mirrors_p1;
393 ios->pages_consumed = cur_pg;
397 static int _prepare_for_striping(struct exofs_io_state *ios)
399 u64 length = ios->length;
400 u64 offset = ios->offset;
401 struct _striping_info si;
405 if (ios->kern_buff) {
406 struct exofs_per_dev_state *per_dev = &ios->per_dev[0];
408 _calc_stripe_info(ios->layout, ios->offset, &si);
409 per_dev->offset = si.obj_offset;
410 per_dev->dev = si.dev;
412 /* no cross device without page array */
413 BUG_ON((ios->layout->group_width > 1) &&
414 (si.unit_off + ios->length >
415 ios->layout->stripe_unit));
417 ios->numdevs = ios->layout->mirrors_p1;
422 _calc_stripe_info(ios->layout, offset, &si);
424 if (length < si.group_length)
425 si.group_length = length;
427 ret = _prepare_one_group(ios, si.group_length, &si);
431 offset += si.group_length;
432 length -= si.group_length;
439 int exofs_sbi_create(struct exofs_io_state *ios)
443 for (i = 0; i < ios->layout->s_numdevs; i++) {
444 struct osd_request *or;
446 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
448 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
452 ios->per_dev[i].or = or;
455 osd_req_create_object(or, &ios->obj);
457 ret = exofs_io_execute(ios);
463 int exofs_sbi_remove(struct exofs_io_state *ios)
467 for (i = 0; i < ios->layout->s_numdevs; i++) {
468 struct osd_request *or;
470 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
472 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
476 ios->per_dev[i].or = or;
479 osd_req_remove_object(or, &ios->obj);
481 ret = exofs_io_execute(ios);
487 static int _sbi_write_mirror(struct exofs_io_state *ios, int cur_comp)
489 struct exofs_per_dev_state *master_dev = &ios->per_dev[cur_comp];
490 unsigned dev = ios->per_dev[cur_comp].dev;
491 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
494 if (ios->pages && !master_dev->length)
495 return 0; /* Just an empty slot */
497 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
498 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
499 struct osd_request *or;
501 or = osd_start_request(exofs_ios_od(ios, dev), GFP_KERNEL);
503 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
508 per_dev->offset = master_dev->offset;
513 if (per_dev != master_dev) {
514 bio = bio_kmalloc(GFP_KERNEL,
515 master_dev->bio->bi_max_vecs);
516 if (unlikely(!bio)) {
518 "Failed to allocate BIO size=%u\n",
519 master_dev->bio->bi_max_vecs);
524 __bio_clone(bio, master_dev->bio);
527 per_dev->length = master_dev->length;
531 bio = master_dev->bio;
532 /* FIXME: bio_set_dir() */
533 bio->bi_rw |= REQ_WRITE;
536 osd_req_write(or, &ios->obj, per_dev->offset, bio,
538 EXOFS_DBGMSG("write(0x%llx) offset=0x%llx "
539 "length=0x%llx dev=%d\n",
540 _LLU(ios->obj.id), _LLU(per_dev->offset),
541 _LLU(per_dev->length), dev);
542 } else if (ios->kern_buff) {
543 ret = osd_req_write_kern(or, &ios->obj, per_dev->offset,
544 ios->kern_buff, ios->length);
547 EXOFS_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
548 "length=0x%llx dev=%d\n",
549 _LLU(ios->obj.id), _LLU(per_dev->offset),
550 _LLU(ios->length), dev);
552 osd_req_set_attributes(or, &ios->obj);
553 EXOFS_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
554 _LLU(ios->obj.id), ios->out_attr_len, dev);
558 osd_req_add_set_attr_list(or, ios->out_attr,
562 osd_req_add_get_attr_list(or, ios->in_attr,
570 int exofs_sbi_write(struct exofs_io_state *ios)
575 ret = _prepare_for_striping(ios);
579 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
580 ret = _sbi_write_mirror(ios, i);
585 ret = exofs_io_execute(ios);
589 static int _sbi_read_mirror(struct exofs_io_state *ios, unsigned cur_comp)
591 struct osd_request *or;
592 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
593 unsigned first_dev = (unsigned)ios->obj.id;
595 if (ios->pages && !per_dev->length)
596 return 0; /* Just an empty slot */
598 first_dev = per_dev->dev + first_dev % ios->layout->mirrors_p1;
599 or = osd_start_request(exofs_ios_od(ios, first_dev), GFP_KERNEL);
601 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
607 osd_req_read(or, &ios->obj, per_dev->offset,
608 per_dev->bio, per_dev->length);
609 EXOFS_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
610 " dev=%d\n", _LLU(ios->obj.id),
611 _LLU(per_dev->offset), _LLU(per_dev->length),
613 } else if (ios->kern_buff) {
614 int ret = osd_req_read_kern(or, &ios->obj, per_dev->offset,
615 ios->kern_buff, ios->length);
616 EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
617 "length=0x%llx dev=%d ret=>%d\n",
618 _LLU(ios->obj.id), _LLU(per_dev->offset),
619 _LLU(ios->length), first_dev, ret);
623 osd_req_get_attributes(or, &ios->obj);
624 EXOFS_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
625 _LLU(ios->obj.id), ios->in_attr_len, first_dev);
628 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
631 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
636 int exofs_sbi_read(struct exofs_io_state *ios)
641 ret = _prepare_for_striping(ios);
645 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
646 ret = _sbi_read_mirror(ios, i);
651 ret = exofs_io_execute(ios);
655 int extract_attr_from_ios(struct exofs_io_state *ios, struct osd_attr *attr)
657 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
663 osd_req_decode_get_attr_list(ios->per_dev[0].or,
664 &cur_attr, &nelem, &iter);
665 if ((cur_attr.attr_page == attr->attr_page) &&
666 (cur_attr.attr_id == attr->attr_id)) {
667 attr->len = cur_attr.len;
668 attr->val_ptr = cur_attr.val_ptr;
676 static int _truncate_mirrors(struct exofs_io_state *ios, unsigned cur_comp,
677 struct osd_attr *attr)
679 int last_comp = cur_comp + ios->layout->mirrors_p1;
681 for (; cur_comp < last_comp; ++cur_comp) {
682 struct exofs_per_dev_state *per_dev = &ios->per_dev[cur_comp];
683 struct osd_request *or;
685 or = osd_start_request(exofs_ios_od(ios, cur_comp), GFP_KERNEL);
687 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
692 osd_req_set_attributes(or, &ios->obj);
693 osd_req_add_set_attr_list(or, attr, 1);
700 struct _striping_info si;
701 u64 prev_group_obj_off;
702 u64 next_group_obj_off;
704 unsigned first_group_dev;
705 unsigned nex_group_dev;
709 void _calc_trunk_info(struct exofs_layout *layout, u64 file_offset,
710 struct _trunc_info *ti)
712 unsigned stripe_unit = layout->stripe_unit;
714 _calc_stripe_info(layout, file_offset, &ti->si);
716 ti->prev_group_obj_off = ti->si.M * stripe_unit;
717 ti->next_group_obj_off = ti->si.M ? (ti->si.M - 1) * stripe_unit : 0;
719 ti->first_group_dev = ti->si.dev - (ti->si.dev % layout->group_width);
720 ti->nex_group_dev = ti->first_group_dev + layout->group_width;
721 ti->max_devs = layout->group_width * layout->group_count;
724 int exofs_oi_truncate(struct exofs_i_info *oi, u64 size)
726 struct exofs_sb_info *sbi = oi->vfs_inode.i_sb->s_fs_info;
727 struct exofs_io_state *ios;
728 struct exofs_trunc_attr {
729 struct osd_attr attr;
732 struct _trunc_info ti;
735 ret = exofs_get_io_state(&sbi->layout, &ios);
739 _calc_trunk_info(ios->layout, size, &ti);
741 size_attrs = kcalloc(ti.max_devs, sizeof(*size_attrs),
743 if (unlikely(!size_attrs)) {
748 ios->obj.id = exofs_oi_objno(oi);
749 ios->cred = oi->i_cred;
750 ios->numdevs = ios->layout->s_numdevs;
752 for (i = 0; i < ti.max_devs; ++i) {
753 struct exofs_trunc_attr *size_attr = &size_attrs[i];
756 if (i < ti.first_group_dev)
757 obj_size = ti.prev_group_obj_off;
758 else if (i >= ti.nex_group_dev)
759 obj_size = ti.next_group_obj_off;
760 else if (i < ti.si.dev) /* dev within this group */
761 obj_size = ti.si.obj_offset +
762 ios->layout->stripe_unit - ti.si.unit_off;
763 else if (i == ti.si.dev)
764 obj_size = ti.si.obj_offset;
765 else /* i > ti.dev */
766 obj_size = ti.si.obj_offset - ti.si.unit_off;
768 size_attr->newsize = cpu_to_be64(obj_size);
769 size_attr->attr = g_attr_logical_length;
770 size_attr->attr.val_ptr = &size_attr->newsize;
772 EXOFS_DBGMSG("trunc(0x%llx) obj_offset=0x%llx dev=%d\n",
773 _LLU(ios->obj.id), _LLU(obj_size), i);
774 ret = _truncate_mirrors(ios, i * ios->layout->mirrors_p1,
779 ret = exofs_io_execute(ios);
783 exofs_put_io_state(ios);
787 const struct osd_attr g_attr_logical_length = ATTR_DEF(
788 OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);