Linux 2.6.31-rc6
[linux-2.6] / fs / exofs / osd.c
1 /*
2  * Copyright (C) 2005, 2006
3  * Avishay Traeger (avishay@gmail.com)
4  * 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>
26 #include <scsi/osd_sense.h>
27
28 #include "exofs.h"
29
30 int exofs_check_ok_resid(struct osd_request *or, u64 *in_resid, u64 *out_resid)
31 {
32         struct osd_sense_info osi;
33         int ret = osd_req_decode_sense(or, &osi);
34
35         if (ret) { /* translate to Linux codes */
36                 if (osi.additional_code == scsi_invalid_field_in_cdb) {
37                         if (osi.cdb_field_offset == OSD_CFO_STARTING_BYTE)
38                                 ret = -EFAULT;
39                         if (osi.cdb_field_offset == OSD_CFO_OBJECT_ID)
40                                 ret = -ENOENT;
41                         else
42                                 ret = -EINVAL;
43                 } else if (osi.additional_code == osd_quota_error)
44                         ret = -ENOSPC;
45                 else
46                         ret = -EIO;
47         }
48
49         /* FIXME: should be include in osd_sense_info */
50         if (in_resid)
51                 *in_resid = or->in.req ? or->in.req->resid_len : 0;
52
53         if (out_resid)
54                 *out_resid = or->out.req ? or->out.req->resid_len : 0;
55
56         return ret;
57 }
58
59 void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj)
60 {
61         osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
62 }
63
64 /*
65  * Perform a synchronous OSD operation.
66  */
67 int exofs_sync_op(struct osd_request *or, int timeout, uint8_t *credential)
68 {
69         int ret;
70
71         or->timeout = timeout;
72         ret = osd_finalize_request(or, 0, credential, NULL);
73         if (ret) {
74                 EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n", ret);
75                 return ret;
76         }
77
78         ret = osd_execute_request(or);
79
80         if (ret)
81                 EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
82         /* osd_req_decode_sense(or, ret); */
83         return ret;
84 }
85
86 /*
87  * Perform an asynchronous OSD operation.
88  */
89 int exofs_async_op(struct osd_request *or, osd_req_done_fn *async_done,
90                    void *caller_context, u8 *cred)
91 {
92         int ret;
93
94         ret = osd_finalize_request(or, 0, cred, NULL);
95         if (ret) {
96                 EXOFS_DBGMSG("Faild to osd_finalize_request() => %d\n", ret);
97                 return ret;
98         }
99
100         ret = osd_execute_request_async(or, async_done, caller_context);
101
102         if (ret)
103                 EXOFS_DBGMSG("osd_execute_request_async() => %d\n", ret);
104         return ret;
105 }
106
107 int extract_attr_from_req(struct osd_request *or, struct osd_attr *attr)
108 {
109         struct osd_attr cur_attr = {.attr_page = 0}; /* start with zeros */
110         void *iter = NULL;
111         int nelem;
112
113         do {
114                 nelem = 1;
115                 osd_req_decode_get_attr_list(or, &cur_attr, &nelem, &iter);
116                 if ((cur_attr.attr_page == attr->attr_page) &&
117                     (cur_attr.attr_id == attr->attr_id)) {
118                         attr->len = cur_attr.len;
119                         attr->val_ptr = cur_attr.val_ptr;
120                         return 0;
121                 }
122         } while (iter);
123
124         return -EIO;
125 }