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 <scsi/scsi_device.h>
29 #define EXOFS_DBGMSG2(M...) do {} while (0)
30 /* #define EXOFS_DBGMSG2 EXOFS_DBGMSG */
32 void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj)
34 osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
37 int exofs_read_kern(struct osd_dev *od, u8 *cred, struct osd_obj_id *obj,
38 u64 offset, void *p, unsigned length)
40 struct osd_request *or = osd_start_request(od, GFP_KERNEL);
41 /* struct osd_sense_info osi = {.key = 0};*/
45 EXOFS_DBGMSG("%s: osd_start_request failed.\n", __func__);
48 ret = osd_req_read_kern(or, obj, offset, p, length);
50 EXOFS_DBGMSG("%s: osd_req_read_kern failed.\n", __func__);
54 ret = osd_finalize_request(or, 0, cred, NULL);
56 EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n", ret);
60 ret = osd_execute_request(or);
62 EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
63 /* osd_req_decode_sense(or, ret); */
70 int exofs_get_io_state(struct exofs_layout *layout,
71 struct exofs_io_state **pios)
73 struct exofs_io_state *ios;
75 /*TODO: Maybe use kmem_cach per sbi of size
76 * exofs_io_state_size(layout->s_numdevs)
78 ios = kzalloc(exofs_io_state_size(layout->s_numdevs), GFP_KERNEL);
80 EXOFS_DBGMSG("Faild kzalloc bytes=%d\n",
81 exofs_io_state_size(layout->s_numdevs));
87 ios->obj.partition = layout->s_pid;
92 void exofs_put_io_state(struct exofs_io_state *ios)
97 for (i = 0; i < ios->numdevs; i++) {
98 struct exofs_per_dev_state *per_dev = &ios->per_dev[i];
101 osd_end_request(per_dev->or);
103 bio_put(per_dev->bio);
110 unsigned exofs_layout_od_id(struct exofs_layout *layout,
111 osd_id obj_no, unsigned layout_index)
116 static inline struct osd_dev *exofs_ios_od(struct exofs_io_state *ios,
117 unsigned layout_index)
119 return ios->layout->s_ods[
120 exofs_layout_od_id(ios->layout, ios->obj.id, layout_index)];
123 static void _sync_done(struct exofs_io_state *ios, void *p)
125 struct completion *waiting = p;
130 static void _last_io(struct kref *kref)
132 struct exofs_io_state *ios = container_of(
133 kref, struct exofs_io_state, kref);
135 ios->done(ios, ios->private);
138 static void _done_io(struct osd_request *or, void *p)
140 struct exofs_io_state *ios = p;
142 kref_put(&ios->kref, _last_io);
145 static int exofs_io_execute(struct exofs_io_state *ios)
147 DECLARE_COMPLETION_ONSTACK(wait);
148 bool sync = (ios->done == NULL);
152 ios->done = _sync_done;
153 ios->private = &wait;
156 for (i = 0; i < ios->numdevs; i++) {
157 struct osd_request *or = ios->per_dev[i].or;
161 ret = osd_finalize_request(or, 0, ios->cred, NULL);
163 EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n",
169 kref_init(&ios->kref);
171 for (i = 0; i < ios->numdevs; i++) {
172 struct osd_request *or = ios->per_dev[i].or;
176 kref_get(&ios->kref);
177 osd_execute_request_async(or, _done_io, ios);
180 kref_put(&ios->kref, _last_io);
184 wait_for_completion(&wait);
185 ret = exofs_check_io(ios, NULL);
190 static void _clear_bio(struct bio *bio)
195 __bio_for_each_segment(bv, bio, i, 0) {
196 unsigned this_count = bv->bv_len;
198 if (likely(PAGE_SIZE == this_count))
199 clear_highpage(bv->bv_page);
201 zero_user(bv->bv_page, bv->bv_offset, this_count);
205 int exofs_check_io(struct exofs_io_state *ios, u64 *resid)
207 enum osd_err_priority acumulated_osd_err = 0;
208 int acumulated_lin_err = 0;
211 for (i = 0; i < ios->numdevs; i++) {
212 struct osd_sense_info osi;
213 struct osd_request *or = ios->per_dev[i].or;
219 ret = osd_req_decode_sense(or, &osi);
223 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
224 /* start read offset passed endof file */
225 _clear_bio(ios->per_dev[i].bio);
226 EXOFS_DBGMSG("start read offset passed end of file "
227 "offset=0x%llx, length=0x%llx\n",
231 continue; /* we recovered */
234 if (osi.osd_err_pri >= acumulated_osd_err) {
235 acumulated_osd_err = osi.osd_err_pri;
236 acumulated_lin_err = ret;
240 /* TODO: raid specific residual calculations */
242 if (likely(!acumulated_lin_err))
245 *resid = ios->length;
248 return acumulated_lin_err;
251 int exofs_sbi_create(struct exofs_io_state *ios)
255 for (i = 0; i < ios->layout->s_numdevs; i++) {
256 struct osd_request *or;
258 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
260 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
264 ios->per_dev[i].or = or;
267 osd_req_create_object(or, &ios->obj);
269 ret = exofs_io_execute(ios);
275 int exofs_sbi_remove(struct exofs_io_state *ios)
279 for (i = 0; i < ios->layout->s_numdevs; i++) {
280 struct osd_request *or;
282 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
284 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
288 ios->per_dev[i].or = or;
291 osd_req_remove_object(or, &ios->obj);
293 ret = exofs_io_execute(ios);
299 int exofs_sbi_write(struct exofs_io_state *ios)
303 for (i = 0; i < ios->layout->s_numdevs; i++) {
304 struct osd_request *or;
306 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
308 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
312 ios->per_dev[i].or = or;
319 bio = bio_kmalloc(GFP_KERNEL,
320 ios->bio->bi_max_vecs);
321 if (unlikely(!bio)) {
323 "Faild to allocate BIO size=%u\n",
324 ios->bio->bi_max_vecs);
329 __bio_clone(bio, ios->bio);
332 ios->per_dev[i].bio = bio;
337 osd_req_write(or, &ios->obj, ios->offset, bio,
339 EXOFS_DBGMSG("write(0x%llx) offset=0x%llx "
340 "length=0x%llx dev=%d\n",
341 _LLU(ios->obj.id), _LLU(ios->offset),
342 _LLU(ios->length), i);
343 } else if (ios->kern_buff) {
344 osd_req_write_kern(or, &ios->obj, ios->offset,
345 ios->kern_buff, ios->length);
346 EXOFS_DBGMSG2("write_kern(0x%llx) offset=0x%llx "
347 "length=0x%llx dev=%d\n",
348 _LLU(ios->obj.id), _LLU(ios->offset),
349 _LLU(ios->length), i);
351 osd_req_set_attributes(or, &ios->obj);
352 EXOFS_DBGMSG2("obj(0x%llx) set_attributes=%d dev=%d\n",
353 _LLU(ios->obj.id), ios->out_attr_len, i);
357 osd_req_add_set_attr_list(or, ios->out_attr,
361 osd_req_add_get_attr_list(or, ios->in_attr,
364 ret = exofs_io_execute(ios);
370 int exofs_sbi_read(struct exofs_io_state *ios)
372 struct osd_request *or;
373 struct exofs_per_dev_state *per_dev = &ios->per_dev[0];
374 unsigned first_dev = (unsigned)ios->obj.id;
376 first_dev %= ios->layout->s_numdevs;
377 or = osd_start_request(exofs_ios_od(ios, first_dev), GFP_KERNEL);
379 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
386 osd_req_read(or, &ios->obj, ios->offset, ios->bio, ios->length);
387 EXOFS_DBGMSG("read(0x%llx) offset=0x%llx length=0x%llx"
388 " dev=%d\n", _LLU(ios->obj.id),
389 _LLU(ios->offset), _LLU(ios->length),
391 } else if (ios->kern_buff) {
392 int ret = osd_req_read_kern(or, &ios->obj, ios->offset,
393 ios->kern_buff, ios->length);
395 EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
396 "length=0x%llx dev=%d ret=>%d\n",
397 _LLU(ios->obj.id), _LLU(ios->offset),
398 _LLU(ios->length), first_dev, ret);
402 osd_req_get_attributes(or, &ios->obj);
403 EXOFS_DBGMSG2("obj(0x%llx) get_attributes=%d dev=%d\n",
404 _LLU(ios->obj.id), ios->in_attr_len, first_dev);
408 osd_req_add_set_attr_list(or, ios->out_attr, ios->out_attr_len);
411 osd_req_add_get_attr_list(or, ios->in_attr, ios->in_attr_len);
413 return exofs_io_execute(ios);
416 int extract_attr_from_ios(struct exofs_io_state *ios, struct osd_attr *attr)
418 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
424 osd_req_decode_get_attr_list(ios->per_dev[0].or,
425 &cur_attr, &nelem, &iter);
426 if ((cur_attr.attr_page == attr->attr_page) &&
427 (cur_attr.attr_id == attr->attr_id)) {
428 attr->len = cur_attr.len;
429 attr->val_ptr = cur_attr.val_ptr;
437 int exofs_oi_truncate(struct exofs_i_info *oi, u64 size)
439 struct exofs_sb_info *sbi = oi->vfs_inode.i_sb->s_fs_info;
440 struct exofs_io_state *ios;
441 struct osd_attr attr;
445 if (exofs_get_io_state(&sbi->layout, &ios))
448 ios->obj.id = exofs_oi_objno(oi);
449 ios->cred = oi->i_cred;
451 newsize = cpu_to_be64(size);
452 attr = g_attr_logical_length;
453 attr.val_ptr = &newsize;
455 for (i = 0; i < sbi->layout.s_numdevs; i++) {
456 struct osd_request *or;
458 or = osd_start_request(exofs_ios_od(ios, i), GFP_KERNEL);
460 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
464 ios->per_dev[i].or = or;
467 osd_req_set_attributes(or, &ios->obj);
468 osd_req_add_set_attr_list(or, &attr, 1);
470 ret = exofs_io_execute(ios);
473 exofs_put_io_state(ios);