2 * Copyright (C) 2005, 2006
4 * Copyright (C) 2008, 2009
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 void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj)
31 osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
34 int exofs_read_kern(struct osd_dev *od, u8 *cred, struct osd_obj_id *obj,
35 u64 offset, void *p, unsigned length)
37 struct osd_request *or = osd_start_request(od, GFP_KERNEL);
38 /* struct osd_sense_info osi = {.key = 0};*/
42 EXOFS_DBGMSG("%s: osd_start_request failed.\n", __func__);
45 ret = osd_req_read_kern(or, obj, offset, p, length);
47 EXOFS_DBGMSG("%s: osd_req_read_kern failed.\n", __func__);
51 ret = osd_finalize_request(or, 0, cred, NULL);
53 EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n", ret);
57 ret = osd_execute_request(or);
59 EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
60 /* osd_req_decode_sense(or, ret); */
67 int exofs_get_io_state(struct exofs_sb_info *sbi, struct exofs_io_state** pios)
69 struct exofs_io_state *ios;
71 /*TODO: Maybe use kmem_cach per sbi of size
72 * exofs_io_state_size(sbi->s_numdevs)
74 ios = kzalloc(exofs_io_state_size(sbi->s_numdevs), GFP_KERNEL);
81 ios->obj.partition = sbi->s_pid;
86 void exofs_put_io_state(struct exofs_io_state *ios)
91 for (i = 0; i < ios->numdevs; i++) {
92 struct exofs_per_dev_state *per_dev = &ios->per_dev[i];
95 osd_end_request(per_dev->or);
97 bio_put(per_dev->bio);
104 static void _sync_done(struct exofs_io_state *ios, void *p)
106 struct completion *waiting = p;
111 static void _last_io(struct kref *kref)
113 struct exofs_io_state *ios = container_of(
114 kref, struct exofs_io_state, kref);
116 ios->done(ios, ios->private);
119 static void _done_io(struct osd_request *or, void *p)
121 struct exofs_io_state *ios = p;
123 kref_put(&ios->kref, _last_io);
126 static int exofs_io_execute(struct exofs_io_state *ios)
128 DECLARE_COMPLETION_ONSTACK(wait);
129 bool sync = (ios->done == NULL);
133 ios->done = _sync_done;
134 ios->private = &wait;
137 for (i = 0; i < ios->numdevs; i++) {
138 struct osd_request *or = ios->per_dev[i].or;
142 ret = osd_finalize_request(or, 0, ios->cred, NULL);
144 EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n",
150 kref_init(&ios->kref);
152 for (i = 0; i < ios->numdevs; i++) {
153 struct osd_request *or = ios->per_dev[i].or;
157 kref_get(&ios->kref);
158 osd_execute_request_async(or, _done_io, ios);
161 kref_put(&ios->kref, _last_io);
165 wait_for_completion(&wait);
166 ret = exofs_check_io(ios, NULL);
171 int exofs_check_io(struct exofs_io_state *ios, u64 *resid)
173 enum osd_err_priority acumulated_osd_err = 0;
174 int acumulated_lin_err = 0;
177 for (i = 0; i < ios->numdevs; i++) {
178 struct osd_sense_info osi;
179 int ret = osd_req_decode_sense(ios->per_dev[i].or, &osi);
184 if (unlikely(ret == -EFAULT)) {
185 EXOFS_DBGMSG("%s: EFAULT Need page clear\n", __func__);
186 /*FIXME: All the pages in this device range should:
187 * clear_highpage(page);
191 if (osi.osd_err_pri >= acumulated_osd_err) {
192 acumulated_osd_err = osi.osd_err_pri;
193 acumulated_lin_err = ret;
197 /* TODO: raid specific residual calculations */
199 if (likely(!acumulated_lin_err))
202 *resid = ios->length;
205 return acumulated_lin_err;
208 int exofs_sbi_create(struct exofs_io_state *ios)
212 for (i = 0; i < ios->sbi->s_numdevs; i++) {
213 struct osd_request *or;
215 or = osd_start_request(ios->sbi->s_ods[i], GFP_KERNEL);
217 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
221 ios->per_dev[i].or = or;
224 osd_req_create_object(or, &ios->obj);
226 ret = exofs_io_execute(ios);
232 int exofs_sbi_remove(struct exofs_io_state *ios)
236 for (i = 0; i < ios->sbi->s_numdevs; i++) {
237 struct osd_request *or;
239 or = osd_start_request(ios->sbi->s_ods[i], GFP_KERNEL);
241 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
245 ios->per_dev[i].or = or;
248 osd_req_remove_object(or, &ios->obj);
250 ret = exofs_io_execute(ios);
256 int exofs_sbi_write(struct exofs_io_state *ios)
260 for (i = 0; i < ios->sbi->s_numdevs; i++) {
261 struct osd_request *or;
263 or = osd_start_request(ios->sbi->s_ods[i], GFP_KERNEL);
265 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
269 ios->per_dev[i].or = or;
276 bio = bio_kmalloc(GFP_KERNEL,
277 ios->bio->bi_max_vecs);
278 if (unlikely(!bio)) {
283 __bio_clone(bio, ios->bio);
286 ios->per_dev[i].bio = bio;
291 osd_req_write(or, &ios->obj, ios->offset, bio,
293 /* EXOFS_DBGMSG("write sync=%d\n", sync);*/
294 } else if (ios->kern_buff) {
295 osd_req_write_kern(or, &ios->obj, ios->offset,
296 ios->kern_buff, ios->length);
297 /* EXOFS_DBGMSG("write_kern sync=%d\n", sync);*/
299 osd_req_set_attributes(or, &ios->obj);
300 /* EXOFS_DBGMSG("set_attributes sync=%d\n", sync);*/
304 osd_req_add_set_attr_list(or, ios->out_attr,
308 osd_req_add_get_attr_list(or, ios->in_attr,
311 ret = exofs_io_execute(ios);
317 int exofs_sbi_read(struct exofs_io_state *ios)
321 for (i = 0; i < 1; i++) {
322 struct osd_request *or;
323 unsigned first_dev = (unsigned)ios->obj.id;
325 first_dev %= ios->sbi->s_numdevs;
326 or = osd_start_request(ios->sbi->s_ods[first_dev], GFP_KERNEL);
328 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
332 ios->per_dev[i].or = or;
336 osd_req_read(or, &ios->obj, ios->offset, ios->bio,
338 /* EXOFS_DBGMSG("read sync=%d\n", sync);*/
339 } else if (ios->kern_buff) {
340 osd_req_read_kern(or, &ios->obj, ios->offset,
341 ios->kern_buff, ios->length);
342 /* EXOFS_DBGMSG("read_kern sync=%d\n", sync);*/
344 osd_req_get_attributes(or, &ios->obj);
345 /* EXOFS_DBGMSG("get_attributes sync=%d\n", sync);*/
349 osd_req_add_set_attr_list(or, ios->out_attr,
353 osd_req_add_get_attr_list(or, ios->in_attr,
356 ret = exofs_io_execute(ios);
362 int extract_attr_from_ios(struct exofs_io_state *ios, struct osd_attr *attr)
364 struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
370 osd_req_decode_get_attr_list(ios->per_dev[0].or,
371 &cur_attr, &nelem, &iter);
372 if ((cur_attr.attr_page == attr->attr_page) &&
373 (cur_attr.attr_id == attr->attr_id)) {
374 attr->len = cur_attr.len;
375 attr->val_ptr = cur_attr.val_ptr;
383 int exofs_oi_truncate(struct exofs_i_info *oi, u64 size)
385 struct exofs_sb_info *sbi = oi->vfs_inode.i_sb->s_fs_info;
386 struct exofs_io_state *ios;
387 struct osd_attr attr;
391 if (exofs_get_io_state(sbi, &ios))
394 ios->obj.id = exofs_oi_objno(oi);
395 ios->cred = oi->i_cred;
397 newsize = cpu_to_be64(size);
398 attr = g_attr_logical_length;
399 attr.val_ptr = &newsize;
401 for (i = 0; i < sbi->s_numdevs; i++) {
402 struct osd_request *or;
404 or = osd_start_request(sbi->s_ods[i], GFP_KERNEL);
406 EXOFS_ERR("%s: osd_start_request failed\n", __func__);
410 ios->per_dev[i].or = or;
413 osd_req_set_attributes(or, &ios->obj);
414 osd_req_add_set_attr_list(or, &attr, 1);
416 ret = exofs_io_execute(ios);
419 exofs_put_io_state(ios);