blob: f5b5735f76d58b9c25f751216d4a39db5c508010 [file] [log] [blame]
Boaz Harrosh02941a52009-01-25 16:55:30 +02001/*
2 * osd_initiator - Main body of the osd initiator library.
3 *
4 * Note: The file does not contain the advanced security functionality which
5 * is only needed by the security_manager's initiators.
6 *
7 * Copyright (C) 2008 Panasas Inc. All rights reserved.
8 *
9 * Authors:
10 * Boaz Harrosh <bharrosh@panasas.com>
11 * Benny Halevy <bhalevy@panasas.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. Neither the name of the Panasas company nor the names of its
26 * contributors may be used to endorse or promote products derived
27 * from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
43
Boaz Harrosh02941a52009-01-25 16:55:30 +020044#include <scsi/osd_initiator.h>
45#include <scsi/osd_sec.h>
Boaz Harrosh1b9dce92009-01-25 17:13:38 +020046#include <scsi/osd_attributes.h>
Boaz Harrosh98f3aea2009-01-25 17:15:16 +020047#include <scsi/osd_sense.h>
48
Boaz Harrosh02941a52009-01-25 16:55:30 +020049#include <scsi/scsi_device.h>
50
51#include "osd_debug.h"
52
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020053#ifndef __unused
54# define __unused __attribute__((unused))
55#endif
56
Boaz Harrosh02941a52009-01-25 16:55:30 +020057enum { OSD_REQ_RETRIES = 1 };
58
59MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
60MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
61MODULE_LICENSE("GPL");
62
63static inline void build_test(void)
64{
65 /* structures were not packed */
66 BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
Boaz Harroshc6572c92009-01-25 17:09:40 +020067 BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN);
Boaz Harrosh02941a52009-01-25 16:55:30 +020068 BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
69}
70
Boaz Harrosh1b9dce92009-01-25 17:13:38 +020071static const char *_osd_ver_desc(struct osd_request *or)
72{
73 return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
74}
75
76#define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
77
Boaz Harrosh2cdd6412009-11-29 16:26:45 +020078static int _osd_get_print_system_info(struct osd_dev *od,
79 void *caps, struct osd_dev_info *odi)
Boaz Harrosh1b9dce92009-01-25 17:13:38 +020080{
81 struct osd_request *or;
82 struct osd_attr get_attrs[] = {
83 ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION, 8),
84 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION, 16),
85 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL, 32),
86 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL, 4),
87 ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER, 64 /*variable*/),
88 ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME, 64 /*variable*/),
89 ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY, 8),
90 ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY, 8),
91 ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS, 8),
92 ATTR_DEF_RI(OSD_ATTR_RI_CLOCK, 6),
93 /* IBM-OSD-SIM Has a bug with this one put it last */
94 ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID, 20),
95 };
96 void *iter = NULL, *pFirst;
97 int nelem = ARRAY_SIZE(get_attrs), a = 0;
98 int ret;
99
100 or = osd_start_request(od, GFP_KERNEL);
101 if (!or)
102 return -ENOMEM;
103
104 /* get attrs */
105 osd_req_get_attributes(or, &osd_root_object);
106 osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs));
107
108 ret = osd_finalize_request(or, 0, caps, NULL);
109 if (ret)
110 goto out;
111
112 ret = osd_execute_request(or);
113 if (ret) {
114 OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
115 goto out;
116 }
117
118 osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter);
119
120 OSD_INFO("Detected %s device\n",
121 _osd_ver_desc(or));
122
123 pFirst = get_attrs[a++].val_ptr;
Boaz Harroshde6b2032009-05-24 20:01:26 +0300124 OSD_INFO("VENDOR_IDENTIFICATION [%s]\n",
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200125 (char *)pFirst);
126
127 pFirst = get_attrs[a++].val_ptr;
Boaz Harroshde6b2032009-05-24 20:01:26 +0300128 OSD_INFO("PRODUCT_IDENTIFICATION [%s]\n",
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200129 (char *)pFirst);
130
131 pFirst = get_attrs[a++].val_ptr;
Boaz Harroshde6b2032009-05-24 20:01:26 +0300132 OSD_INFO("PRODUCT_MODEL [%s]\n",
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200133 (char *)pFirst);
134
135 pFirst = get_attrs[a++].val_ptr;
Boaz Harroshde6b2032009-05-24 20:01:26 +0300136 OSD_INFO("PRODUCT_REVISION_LEVEL [%u]\n",
Boaz Harrosh97218a12009-02-08 18:02:22 +0200137 pFirst ? get_unaligned_be32(pFirst) : ~0U);
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200138
139 pFirst = get_attrs[a++].val_ptr;
Boaz Harroshde6b2032009-05-24 20:01:26 +0300140 OSD_INFO("PRODUCT_SERIAL_NUMBER [%s]\n",
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200141 (char *)pFirst);
142
Boaz Harrosh2cdd6412009-11-29 16:26:45 +0200143 odi->osdname_len = get_attrs[a].len;
144 /* Avoid NULL for memcmp optimization 0-length is good enough */
145 odi->osdname = kzalloc(odi->osdname_len + 1, GFP_KERNEL);
146 if (odi->osdname_len)
147 memcpy(odi->osdname, get_attrs[a].val_ptr, odi->osdname_len);
148 OSD_INFO("OSD_NAME [%s]\n", odi->osdname);
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200149 a++;
150
151 pFirst = get_attrs[a++].val_ptr;
Boaz Harroshde6b2032009-05-24 20:01:26 +0300152 OSD_INFO("TOTAL_CAPACITY [0x%llx]\n",
Boaz Harrosh97218a12009-02-08 18:02:22 +0200153 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200154
155 pFirst = get_attrs[a++].val_ptr;
Boaz Harroshde6b2032009-05-24 20:01:26 +0300156 OSD_INFO("USED_CAPACITY [0x%llx]\n",
Boaz Harrosh97218a12009-02-08 18:02:22 +0200157 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200158
159 pFirst = get_attrs[a++].val_ptr;
Boaz Harroshde6b2032009-05-24 20:01:26 +0300160 OSD_INFO("NUMBER_OF_PARTITIONS [%llu]\n",
Boaz Harrosh97218a12009-02-08 18:02:22 +0200161 pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
162
163 if (a >= nelem)
164 goto out;
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200165
166 /* FIXME: Where are the time utilities */
167 pFirst = get_attrs[a++].val_ptr;
Boaz Harroshde6b2032009-05-24 20:01:26 +0300168 OSD_INFO("CLOCK [0x%02x%02x%02x%02x%02x%02x]\n",
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200169 ((char *)pFirst)[0], ((char *)pFirst)[1],
170 ((char *)pFirst)[2], ((char *)pFirst)[3],
171 ((char *)pFirst)[4], ((char *)pFirst)[5]);
172
173 if (a < nelem) { /* IBM-OSD-SIM bug, Might not have it */
174 unsigned len = get_attrs[a].len;
175 char sid_dump[32*4 + 2]; /* 2nibbles+space+ASCII */
176
177 hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1,
178 sid_dump, sizeof(sid_dump), true);
Boaz Harroshde6b2032009-05-24 20:01:26 +0300179 OSD_INFO("OSD_SYSTEM_ID(%d)\n"
180 " [%s]\n", len, sid_dump);
Boaz Harrosh2cdd6412009-11-29 16:26:45 +0200181
182 if (unlikely(len > sizeof(odi->systemid))) {
183 OSD_ERR("OSD Target error: OSD_SYSTEM_ID too long(%d). "
184 "device idetification might not work\n", len);
185 len = sizeof(odi->systemid);
186 }
187 odi->systemid_len = len;
188 memcpy(odi->systemid, get_attrs[a].val_ptr, len);
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200189 a++;
190 }
191out:
192 osd_end_request(or);
193 return ret;
194}
195
Boaz Harrosh2cdd6412009-11-29 16:26:45 +0200196int osd_auto_detect_ver(struct osd_dev *od,
197 void *caps, struct osd_dev_info *odi)
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200198{
199 int ret;
200
201 /* Auto-detect the osd version */
Boaz Harrosh2cdd6412009-11-29 16:26:45 +0200202 ret = _osd_get_print_system_info(od, caps, odi);
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200203 if (ret) {
204 osd_dev_set_ver(od, OSD_VER1);
205 OSD_DEBUG("converting to OSD1\n");
Boaz Harrosh2cdd6412009-11-29 16:26:45 +0200206 ret = _osd_get_print_system_info(od, caps, odi);
Boaz Harrosh1b9dce92009-01-25 17:13:38 +0200207 }
208
209 return ret;
210}
211EXPORT_SYMBOL(osd_auto_detect_ver);
212
Boaz Harrosh02941a52009-01-25 16:55:30 +0200213static unsigned _osd_req_cdb_len(struct osd_request *or)
214{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200215 return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
Boaz Harrosh02941a52009-01-25 16:55:30 +0200216}
217
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200218static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
219{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200220 return osd_req_is_ver1(or) ?
221 osdv1_attr_list_elem_size(len) :
222 osdv2_attr_list_elem_size(len);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200223}
224
Boaz Harrosh71f32e32009-04-19 19:11:42 +0300225static void _osd_req_alist_elem_encode(struct osd_request *or,
226 void *attr_last, const struct osd_attr *oa)
227{
228 if (osd_req_is_ver1(or)) {
229 struct osdv1_attributes_list_element *attr = attr_last;
230
231 attr->attr_page = cpu_to_be32(oa->attr_page);
232 attr->attr_id = cpu_to_be32(oa->attr_id);
233 attr->attr_bytes = cpu_to_be16(oa->len);
234 memcpy(attr->attr_val, oa->val_ptr, oa->len);
235 } else {
236 struct osdv2_attributes_list_element *attr = attr_last;
237
238 attr->attr_page = cpu_to_be32(oa->attr_page);
239 attr->attr_id = cpu_to_be32(oa->attr_id);
240 attr->attr_bytes = cpu_to_be16(oa->len);
241 memcpy(attr->attr_val, oa->val_ptr, oa->len);
242 }
243}
244
245static int _osd_req_alist_elem_decode(struct osd_request *or,
246 void *cur_p, struct osd_attr *oa, unsigned max_bytes)
247{
248 unsigned inc;
249 if (osd_req_is_ver1(or)) {
250 struct osdv1_attributes_list_element *attr = cur_p;
251
252 if (max_bytes < sizeof(*attr))
253 return -1;
254
255 oa->len = be16_to_cpu(attr->attr_bytes);
256 inc = _osd_req_alist_elem_size(or, oa->len);
257 if (inc > max_bytes)
258 return -1;
259
260 oa->attr_page = be32_to_cpu(attr->attr_page);
261 oa->attr_id = be32_to_cpu(attr->attr_id);
262
263 /* OSD1: On empty attributes we return a pointer to 2 bytes
264 * of zeros. This keeps similar behaviour with OSD2.
265 * (See below)
266 */
267 oa->val_ptr = likely(oa->len) ? attr->attr_val :
268 (u8 *)&attr->attr_bytes;
269 } else {
270 struct osdv2_attributes_list_element *attr = cur_p;
271
272 if (max_bytes < sizeof(*attr))
273 return -1;
274
275 oa->len = be16_to_cpu(attr->attr_bytes);
276 inc = _osd_req_alist_elem_size(or, oa->len);
277 if (inc > max_bytes)
278 return -1;
279
280 oa->attr_page = be32_to_cpu(attr->attr_page);
281 oa->attr_id = be32_to_cpu(attr->attr_id);
282
Boaz Harroshe9da4d72009-04-19 19:17:54 +0300283 /* OSD2: For convenience, on empty attributes, we return 8 bytes
284 * of zeros here. This keeps the same behaviour with OSD2r04,
285 * and is nice with null terminating ASCII fields.
286 * oa->val_ptr == NULL marks the end-of-list, or error.
287 */
288 oa->val_ptr = likely(oa->len) ? attr->attr_val : attr->reserved;
Boaz Harrosh71f32e32009-04-19 19:11:42 +0300289 }
290 return inc;
291}
292
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200293static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
294{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200295 return osd_req_is_ver1(or) ?
296 osdv1_list_size(list_head) :
297 osdv2_list_size(list_head);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200298}
299
300static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
301{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200302 return osd_req_is_ver1(or) ?
303 sizeof(struct osdv1_attributes_list_header) :
304 sizeof(struct osdv2_attributes_list_header);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200305}
306
307static void _osd_req_set_alist_type(struct osd_request *or,
308 void *list, int list_type)
309{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200310 if (osd_req_is_ver1(or)) {
311 struct osdv1_attributes_list_header *attr_list = list;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200312
Boaz Harroshc6572c92009-01-25 17:09:40 +0200313 memset(attr_list, 0, sizeof(*attr_list));
314 attr_list->type = list_type;
315 } else {
316 struct osdv2_attributes_list_header *attr_list = list;
317
318 memset(attr_list, 0, sizeof(*attr_list));
319 attr_list->type = list_type;
320 }
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200321}
322
323static bool _osd_req_is_alist_type(struct osd_request *or,
324 void *list, int list_type)
325{
326 if (!list)
327 return false;
328
Boaz Harroshc6572c92009-01-25 17:09:40 +0200329 if (osd_req_is_ver1(or)) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200330 struct osdv1_attributes_list_header *attr_list = list;
331
332 return attr_list->type == list_type;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200333 } else {
334 struct osdv2_attributes_list_header *attr_list = list;
335
336 return attr_list->type == list_type;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200337 }
338}
339
Boaz Harrosh3e086132009-01-25 17:05:07 +0200340/* This is for List-objects not Attributes-Lists */
341static void _osd_req_encode_olist(struct osd_request *or,
342 struct osd_obj_id_list *list)
343{
344 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
345
Boaz Harroshc6572c92009-01-25 17:09:40 +0200346 if (osd_req_is_ver1(or)) {
347 cdbh->v1.list_identifier = list->list_identifier;
348 cdbh->v1.start_address = list->continuation_id;
349 } else {
350 cdbh->v2.list_identifier = list->list_identifier;
351 cdbh->v2.start_address = list->continuation_id;
352 }
Boaz Harrosh3e086132009-01-25 17:05:07 +0200353}
354
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200355static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
356 u64 offset, unsigned *padding)
357{
358 return __osd_encode_offset(offset, padding,
Boaz Harroshc6572c92009-01-25 17:09:40 +0200359 osd_req_is_ver1(or) ?
360 OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
361 OSD_OFFSET_MAX_SHIFT);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200362}
363
Boaz Harrosh345c4352009-01-25 17:03:07 +0200364static struct osd_security_parameters *
365_osd_req_sec_params(struct osd_request *or)
366{
367 struct osd_cdb *ocdb = &or->cdb;
368
Boaz Harroshc6572c92009-01-25 17:09:40 +0200369 if (osd_req_is_ver1(or))
Boaz Harroshf8d3a642009-04-19 19:13:39 +0300370 return (struct osd_security_parameters *)&ocdb->v1.sec_params;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200371 else
Boaz Harroshf8d3a642009-04-19 19:13:39 +0300372 return (struct osd_security_parameters *)&ocdb->v2.sec_params;
Boaz Harrosh345c4352009-01-25 17:03:07 +0200373}
374
Boaz Harrosh02941a52009-01-25 16:55:30 +0200375void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
376{
377 memset(osdd, 0, sizeof(*osdd));
378 osdd->scsi_device = scsi_device;
379 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200380#ifdef OSD_VER1_SUPPORT
381 osdd->version = OSD_VER2;
382#endif
Boaz Harrosh02941a52009-01-25 16:55:30 +0200383 /* TODO: Allocate pools for osd_request attributes ... */
384}
385EXPORT_SYMBOL(osd_dev_init);
386
387void osd_dev_fini(struct osd_dev *osdd)
388{
389 /* TODO: De-allocate pools */
390
391 osdd->scsi_device = NULL;
392}
393EXPORT_SYMBOL(osd_dev_fini);
394
395static struct osd_request *_osd_request_alloc(gfp_t gfp)
396{
397 struct osd_request *or;
398
399 /* TODO: Use mempool with one saved request */
400 or = kzalloc(sizeof(*or), gfp);
401 return or;
402}
403
404static void _osd_request_free(struct osd_request *or)
405{
406 kfree(or);
407}
408
409struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
410{
411 struct osd_request *or;
412
413 or = _osd_request_alloc(gfp);
414 if (!or)
415 return NULL;
416
417 or->osd_dev = dev;
418 or->alloc_flags = gfp;
419 or->timeout = dev->def_timeout;
420 or->retries = OSD_REQ_RETRIES;
421
422 return or;
423}
424EXPORT_SYMBOL(osd_start_request);
425
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200426static void _osd_free_seg(struct osd_request *or __unused,
427 struct _osd_req_data_segment *seg)
428{
429 if (!seg->buff || !seg->alloc_size)
430 return;
431
432 kfree(seg->buff);
433 seg->buff = NULL;
434 seg->alloc_size = 0;
435}
436
Boaz Harrosh5d0961f2009-12-01 17:36:21 +0200437static void _put_request(struct request *rq)
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300438{
Boaz Harrosh5d0961f2009-12-01 17:36:21 +0200439 /*
440 * If osd_finalize_request() was called but the request was not
441 * executed through the block layer, then we must release BIOs.
442 * TODO: Keep error code in or->async_error. Need to audit all
443 * code paths.
444 */
445 if (unlikely(rq->bio))
446 blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
447 else
448 blk_put_request(rq);
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300449}
450
Boaz Harrosh02941a52009-01-25 16:55:30 +0200451void osd_end_request(struct osd_request *or)
452{
453 struct request *rq = or->request;
454
455 if (rq) {
456 if (rq->next_rq) {
Boaz Harrosh5d0961f2009-12-01 17:36:21 +0200457 _put_request(rq->next_rq);
Boaz Harrosh8c0bacc2009-03-31 20:05:29 +0300458 rq->next_rq = NULL;
Boaz Harrosh02941a52009-01-25 16:55:30 +0200459 }
460
Boaz Harrosh5d0961f2009-12-01 17:36:21 +0200461 _put_request(rq);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200462 }
Boaz Harroshc4df46c2010-10-19 14:21:34 +0200463
464 _osd_free_seg(or, &or->get_attr);
465 _osd_free_seg(or, &or->enc_get_attr);
466 _osd_free_seg(or, &or->set_attr);
Boaz Harroshe96e72c2010-10-19 14:22:21 +0200467 _osd_free_seg(or, &or->cdb_cont);
Boaz Harroshc4df46c2010-10-19 14:21:34 +0200468
Boaz Harrosh02941a52009-01-25 16:55:30 +0200469 _osd_request_free(or);
470}
471EXPORT_SYMBOL(osd_end_request);
472
Boaz Harrosh5d0961f2009-12-01 17:36:21 +0200473static void _set_error_resid(struct osd_request *or, struct request *req,
474 int error)
475{
476 or->async_error = error;
477 or->req_errors = req->errors ? : error;
478 or->sense_len = req->sense_len;
479 if (or->out.req)
480 or->out.residual = or->out.req->resid_len;
481 if (or->in.req)
482 or->in.residual = or->in.req->resid_len;
483}
484
Boaz Harrosh02941a52009-01-25 16:55:30 +0200485int osd_execute_request(struct osd_request *or)
486{
Boaz Harrosh5d0961f2009-12-01 17:36:21 +0200487 int error = blk_execute_rq(or->request->q, NULL, or->request, 0);
488
489 _set_error_resid(or, or->request, error);
490 return error;
Boaz Harrosh02941a52009-01-25 16:55:30 +0200491}
492EXPORT_SYMBOL(osd_execute_request);
493
494static void osd_request_async_done(struct request *req, int error)
495{
496 struct osd_request *or = req->end_io_data;
497
Boaz Harrosh5d0961f2009-12-01 17:36:21 +0200498 _set_error_resid(or, req, error);
499 if (req->next_rq) {
500 __blk_put_request(req->q, req->next_rq);
501 req->next_rq = NULL;
Boaz Harroshaa9fffb2009-11-16 20:48:38 +0200502 }
Boaz Harrosh02941a52009-01-25 16:55:30 +0200503
Boaz Harrosh5d0961f2009-12-01 17:36:21 +0200504 __blk_put_request(req->q, req);
505 or->request = NULL;
506 or->in.req = NULL;
507 or->out.req = NULL;
508
Boaz Harrosh02941a52009-01-25 16:55:30 +0200509 if (or->async_done)
510 or->async_done(or, or->async_private);
511 else
512 osd_end_request(or);
513}
514
515int osd_execute_request_async(struct osd_request *or,
516 osd_req_done_fn *done, void *private)
517{
518 or->request->end_io_data = or;
519 or->async_private = private;
520 or->async_done = done;
521
522 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
523 osd_request_async_done);
524 return 0;
525}
526EXPORT_SYMBOL(osd_execute_request_async);
527
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200528u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
529u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
530
531static int _osd_realloc_seg(struct osd_request *or,
532 struct _osd_req_data_segment *seg, unsigned max_bytes)
533{
534 void *buff;
535
536 if (seg->alloc_size >= max_bytes)
537 return 0;
538
539 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
540 if (!buff) {
541 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
542 seg->alloc_size);
543 return -ENOMEM;
544 }
545
546 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
547 seg->buff = buff;
548 seg->alloc_size = max_bytes;
549 return 0;
550}
551
Boaz Harroshe96e72c2010-10-19 14:22:21 +0200552static int _alloc_cdb_cont(struct osd_request *or, unsigned total_bytes)
553{
554 OSD_DEBUG("total_bytes=%d\n", total_bytes);
555 return _osd_realloc_seg(or, &or->cdb_cont, total_bytes);
556}
557
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200558static int _alloc_set_attr_list(struct osd_request *or,
559 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
560{
561 unsigned total_bytes = add_bytes;
562
563 for (; nelem; --nelem, ++oa)
564 total_bytes += _osd_req_alist_elem_size(or, oa->len);
565
566 OSD_DEBUG("total_bytes=%d\n", total_bytes);
567 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
568}
569
570static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
571{
572 OSD_DEBUG("total_bytes=%d\n", max_bytes);
573 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
574}
575
576static int _alloc_get_attr_list(struct osd_request *or)
577{
578 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
579 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
580}
581
Boaz Harrosh02941a52009-01-25 16:55:30 +0200582/*
583 * Common to all OSD commands
584 */
585
586static void _osdv1_req_encode_common(struct osd_request *or,
587 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
588{
589 struct osdv1_cdb *ocdb = &or->cdb.v1;
590
591 /*
592 * For speed, the commands
593 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
594 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
595 * are not supported here. Should pass zero and set after the call
596 */
597 act &= cpu_to_be16(~0x0080); /* V1 action code */
598
599 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
600
601 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
602 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
603 ocdb->h.varlen_cdb.service_action = act;
604
605 ocdb->h.partition = cpu_to_be64(obj->partition);
606 ocdb->h.object = cpu_to_be64(obj->id);
607 ocdb->h.v1.length = cpu_to_be64(len);
608 ocdb->h.v1.start_address = cpu_to_be64(offset);
609}
610
Boaz Harroshc6572c92009-01-25 17:09:40 +0200611static void _osdv2_req_encode_common(struct osd_request *or,
612 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
613{
614 struct osdv2_cdb *ocdb = &or->cdb.v2;
615
616 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
617
618 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
619 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
620 ocdb->h.varlen_cdb.service_action = act;
621
622 ocdb->h.partition = cpu_to_be64(obj->partition);
623 ocdb->h.object = cpu_to_be64(obj->id);
624 ocdb->h.v2.length = cpu_to_be64(len);
625 ocdb->h.v2.start_address = cpu_to_be64(offset);
626}
627
Boaz Harrosh02941a52009-01-25 16:55:30 +0200628static void _osd_req_encode_common(struct osd_request *or,
629 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
630{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200631 if (osd_req_is_ver1(or))
632 _osdv1_req_encode_common(or, act, obj, offset, len);
633 else
634 _osdv2_req_encode_common(or, act, obj, offset, len);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200635}
636
637/*
638 * Device commands
639 */
Boaz Harroshae30c992009-01-25 17:07:14 +0200640/*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
641/*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
642
Boaz Harrosh02941a52009-01-25 16:55:30 +0200643void osd_req_format(struct osd_request *or, u64 tot_capacity)
644{
645 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
646 tot_capacity);
647}
648EXPORT_SYMBOL(osd_req_format);
649
Boaz Harrosh3e086132009-01-25 17:05:07 +0200650int osd_req_list_dev_partitions(struct osd_request *or,
651 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
652{
653 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
654}
655EXPORT_SYMBOL(osd_req_list_dev_partitions);
656
657static void _osd_req_encode_flush(struct osd_request *or,
658 enum osd_options_flush_scope_values op)
659{
660 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
661
662 ocdb->command_specific_options = op;
663}
664
665void osd_req_flush_obsd(struct osd_request *or,
666 enum osd_options_flush_scope_values op)
667{
668 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
669 _osd_req_encode_flush(or, op);
670}
671EXPORT_SYMBOL(osd_req_flush_obsd);
672
Boaz Harroshae30c992009-01-25 17:07:14 +0200673/*TODO: void osd_req_perform_scsi_command(struct osd_request *,
674 const u8 *cdb, ...); */
675/*TODO: void osd_req_task_management(struct osd_request *, ...); */
676
Boaz Harrosh02941a52009-01-25 16:55:30 +0200677/*
678 * Partition commands
679 */
680static void _osd_req_encode_partition(struct osd_request *or,
681 __be16 act, osd_id partition)
682{
683 struct osd_obj_id par = {
684 .partition = partition,
685 .id = 0,
686 };
687
688 _osd_req_encode_common(or, act, &par, 0, 0);
689}
690
691void osd_req_create_partition(struct osd_request *or, osd_id partition)
692{
693 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
694}
695EXPORT_SYMBOL(osd_req_create_partition);
696
697void osd_req_remove_partition(struct osd_request *or, osd_id partition)
698{
699 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
700}
701EXPORT_SYMBOL(osd_req_remove_partition);
702
Boaz Harroshae30c992009-01-25 17:07:14 +0200703/*TODO: void osd_req_set_partition_key(struct osd_request *,
704 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
705 u8 seed[OSD_CRYPTO_SEED_SIZE]); */
706
Boaz Harrosh3e086132009-01-25 17:05:07 +0200707static int _osd_req_list_objects(struct osd_request *or,
708 __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
709 struct osd_obj_id_list *list, unsigned nelem)
710{
Boaz Harroshfc2fac52009-05-24 20:04:43 +0300711 struct request_queue *q = osd_request_queue(or->osd_dev);
Boaz Harrosh3e086132009-01-25 17:05:07 +0200712 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
713 struct bio *bio;
714
715 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
716
717 if (list->list_identifier)
718 _osd_req_encode_olist(or, list);
719
720 WARN_ON(or->in.bio);
721 bio = bio_map_kern(q, list, len, or->alloc_flags);
Dan Carpenterbf5e84f2009-04-19 19:07:47 +0300722 if (IS_ERR(bio)) {
Boaz Harrosh3e086132009-01-25 17:05:07 +0200723 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
Dan Carpenterbf5e84f2009-04-19 19:07:47 +0300724 return PTR_ERR(bio);
Boaz Harrosh3e086132009-01-25 17:05:07 +0200725 }
726
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200727 bio->bi_rw &= ~REQ_WRITE;
Boaz Harrosh3e086132009-01-25 17:05:07 +0200728 or->in.bio = bio;
729 or->in.total_bytes = bio->bi_size;
730 return 0;
731}
732
733int osd_req_list_partition_collections(struct osd_request *or,
734 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
735 unsigned nelem)
736{
737 struct osd_obj_id par = {
738 .partition = partition,
739 .id = 0,
740 };
741
742 return osd_req_list_collection_objects(or, &par, initial_id, list,
743 nelem);
744}
745EXPORT_SYMBOL(osd_req_list_partition_collections);
746
747int osd_req_list_partition_objects(struct osd_request *or,
748 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
749 unsigned nelem)
750{
751 struct osd_obj_id par = {
752 .partition = partition,
753 .id = 0,
754 };
755
756 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
757 nelem);
758}
759EXPORT_SYMBOL(osd_req_list_partition_objects);
760
761void osd_req_flush_partition(struct osd_request *or,
762 osd_id partition, enum osd_options_flush_scope_values op)
763{
764 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
765 _osd_req_encode_flush(or, op);
766}
767EXPORT_SYMBOL(osd_req_flush_partition);
768
769/*
770 * Collection commands
771 */
Boaz Harroshae30c992009-01-25 17:07:14 +0200772/*TODO: void osd_req_create_collection(struct osd_request *,
773 const struct osd_obj_id *); */
774/*TODO: void osd_req_remove_collection(struct osd_request *,
775 const struct osd_obj_id *); */
776
Boaz Harrosh3e086132009-01-25 17:05:07 +0200777int osd_req_list_collection_objects(struct osd_request *or,
778 const struct osd_obj_id *obj, osd_id initial_id,
779 struct osd_obj_id_list *list, unsigned nelem)
780{
781 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
782 initial_id, list, nelem);
783}
784EXPORT_SYMBOL(osd_req_list_collection_objects);
785
Boaz Harroshae30c992009-01-25 17:07:14 +0200786/*TODO: void query(struct osd_request *, ...); V2 */
787
Boaz Harrosh3e086132009-01-25 17:05:07 +0200788void osd_req_flush_collection(struct osd_request *or,
789 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
790{
791 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
792 _osd_req_encode_flush(or, op);
793}
794EXPORT_SYMBOL(osd_req_flush_collection);
795
Boaz Harroshae30c992009-01-25 17:07:14 +0200796/*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
797/*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
798
Boaz Harrosh02941a52009-01-25 16:55:30 +0200799/*
800 * Object commands
801 */
802void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
803{
804 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
805}
806EXPORT_SYMBOL(osd_req_create_object);
807
808void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
809{
810 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
811}
812EXPORT_SYMBOL(osd_req_remove_object);
813
Boaz Harroshae30c992009-01-25 17:07:14 +0200814
815/*TODO: void osd_req_create_multi(struct osd_request *or,
816 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
817*/
818
Boaz Harrosh02941a52009-01-25 16:55:30 +0200819void osd_req_write(struct osd_request *or,
Boaz Harrosh62f469b2009-05-24 20:04:26 +0300820 const struct osd_obj_id *obj, u64 offset,
821 struct bio *bio, u64 len)
Boaz Harrosh02941a52009-01-25 16:55:30 +0200822{
Boaz Harrosh62f469b2009-05-24 20:04:26 +0300823 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200824 WARN_ON(or->out.bio || or->out.total_bytes);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200825 WARN_ON(0 == (bio->bi_rw & REQ_WRITE));
Boaz Harrosh02941a52009-01-25 16:55:30 +0200826 or->out.bio = bio;
Boaz Harrosh62f469b2009-05-24 20:04:26 +0300827 or->out.total_bytes = len;
Boaz Harrosh02941a52009-01-25 16:55:30 +0200828}
829EXPORT_SYMBOL(osd_req_write);
830
Boaz Harrosh0e35afb2009-05-24 20:02:22 +0300831int osd_req_write_kern(struct osd_request *or,
832 const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
833{
Boaz Harroshfc2fac52009-05-24 20:04:43 +0300834 struct request_queue *req_q = osd_request_queue(or->osd_dev);
Boaz Harrosh0e35afb2009-05-24 20:02:22 +0300835 struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
836
837 if (IS_ERR(bio))
838 return PTR_ERR(bio);
839
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200840 bio->bi_rw |= REQ_WRITE; /* FIXME: bio_set_dir() */
Boaz Harrosh62f469b2009-05-24 20:04:26 +0300841 osd_req_write(or, obj, offset, bio, len);
Boaz Harrosh0e35afb2009-05-24 20:02:22 +0300842 return 0;
843}
844EXPORT_SYMBOL(osd_req_write_kern);
845
Boaz Harroshae30c992009-01-25 17:07:14 +0200846/*TODO: void osd_req_append(struct osd_request *,
847 const struct osd_obj_id *, struct bio *data_out); */
848/*TODO: void osd_req_create_write(struct osd_request *,
849 const struct osd_obj_id *, struct bio *data_out, u64 offset); */
850/*TODO: void osd_req_clear(struct osd_request *,
851 const struct osd_obj_id *, u64 offset, u64 len); */
852/*TODO: void osd_req_punch(struct osd_request *,
853 const struct osd_obj_id *, u64 offset, u64 len); V2 */
854
Boaz Harrosh3e086132009-01-25 17:05:07 +0200855void osd_req_flush_object(struct osd_request *or,
856 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
857 /*V2*/ u64 offset, /*V2*/ u64 len)
858{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200859 if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
860 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
861 offset = 0;
862 len = 0;
863 }
864
Boaz Harrosh3e086132009-01-25 17:05:07 +0200865 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
866 _osd_req_encode_flush(or, op);
867}
868EXPORT_SYMBOL(osd_req_flush_object);
869
Boaz Harrosh02941a52009-01-25 16:55:30 +0200870void osd_req_read(struct osd_request *or,
Boaz Harrosh62f469b2009-05-24 20:04:26 +0300871 const struct osd_obj_id *obj, u64 offset,
872 struct bio *bio, u64 len)
Boaz Harrosh02941a52009-01-25 16:55:30 +0200873{
Boaz Harrosh62f469b2009-05-24 20:04:26 +0300874 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200875 WARN_ON(or->in.bio || or->in.total_bytes);
Jiri Slabyd842a932010-08-12 14:31:05 +0200876 WARN_ON(bio->bi_rw & REQ_WRITE);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200877 or->in.bio = bio;
Boaz Harrosh62f469b2009-05-24 20:04:26 +0300878 or->in.total_bytes = len;
Boaz Harrosh02941a52009-01-25 16:55:30 +0200879}
880EXPORT_SYMBOL(osd_req_read);
881
Boaz Harrosh0e35afb2009-05-24 20:02:22 +0300882int osd_req_read_kern(struct osd_request *or,
883 const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
884{
Boaz Harroshfc2fac52009-05-24 20:04:43 +0300885 struct request_queue *req_q = osd_request_queue(or->osd_dev);
Boaz Harrosh0e35afb2009-05-24 20:02:22 +0300886 struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
887
888 if (IS_ERR(bio))
889 return PTR_ERR(bio);
890
Boaz Harrosh62f469b2009-05-24 20:04:26 +0300891 osd_req_read(or, obj, offset, bio, len);
Boaz Harrosh0e35afb2009-05-24 20:02:22 +0300892 return 0;
893}
894EXPORT_SYMBOL(osd_req_read_kern);
895
Boaz Harroshe96e72c2010-10-19 14:22:21 +0200896static int _add_sg_continuation_descriptor(struct osd_request *or,
897 const struct osd_sg_entry *sglist, unsigned numentries, u64 *len)
898{
899 struct osd_sg_continuation_descriptor *oscd;
900 u32 oscd_size;
901 unsigned i;
902 int ret;
903
904 oscd_size = sizeof(*oscd) + numentries * sizeof(oscd->entries[0]);
905
906 if (!or->cdb_cont.total_bytes) {
907 /* First time, jump over the header, we will write to:
908 * cdb_cont.buff + cdb_cont.total_bytes
909 */
910 or->cdb_cont.total_bytes =
911 sizeof(struct osd_continuation_segment_header);
912 }
913
914 ret = _alloc_cdb_cont(or, or->cdb_cont.total_bytes + oscd_size);
915 if (unlikely(ret))
916 return ret;
917
918 oscd = or->cdb_cont.buff + or->cdb_cont.total_bytes;
919 oscd->hdr.type = cpu_to_be16(SCATTER_GATHER_LIST);
920 oscd->hdr.pad_length = 0;
921 oscd->hdr.length = cpu_to_be32(oscd_size - sizeof(*oscd));
922
923 *len = 0;
924 /* copy the sg entries and convert to network byte order */
925 for (i = 0; i < numentries; i++) {
926 oscd->entries[i].offset = cpu_to_be64(sglist[i].offset);
927 oscd->entries[i].len = cpu_to_be64(sglist[i].len);
928 *len += sglist[i].len;
929 }
930
931 or->cdb_cont.total_bytes += oscd_size;
932 OSD_DEBUG("total_bytes=%d oscd_size=%d numentries=%d\n",
933 or->cdb_cont.total_bytes, oscd_size, numentries);
934 return 0;
935}
936
937static int _osd_req_finalize_cdb_cont(struct osd_request *or, const u8 *cap_key)
938{
939 struct request_queue *req_q = osd_request_queue(or->osd_dev);
940 struct bio *bio;
941 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
942 struct osd_continuation_segment_header *cont_seg_hdr;
943
944 if (!or->cdb_cont.total_bytes)
945 return 0;
946
947 cont_seg_hdr = or->cdb_cont.buff;
948 cont_seg_hdr->format = CDB_CONTINUATION_FORMAT_V2;
949 cont_seg_hdr->service_action = cdbh->varlen_cdb.service_action;
950
951 /* create a bio for continuation segment */
952 bio = bio_map_kern(req_q, or->cdb_cont.buff, or->cdb_cont.total_bytes,
953 GFP_KERNEL);
954 if (unlikely(!bio))
955 return -ENOMEM;
956
957 bio->bi_rw |= REQ_WRITE;
958
959 /* integrity check the continuation before the bio is linked
960 * with the other data segments since the continuation
961 * integrity is separate from the other data segments.
962 */
963 osd_sec_sign_data(cont_seg_hdr->integrity_check, bio, cap_key);
964
965 cdbh->v2.cdb_continuation_length = cpu_to_be32(or->cdb_cont.total_bytes);
966
967 /* we can't use _req_append_segment, because we need to link in the
968 * continuation bio to the head of the bio list - the
969 * continuation segment (if it exists) is always the first segment in
970 * the out data buffer.
971 */
972 bio->bi_next = or->out.bio;
973 or->out.bio = bio;
974 or->out.total_bytes += or->cdb_cont.total_bytes;
975
976 return 0;
977}
978
979/* osd_req_write_sg: Takes a @bio that points to the data out buffer and an
980 * @sglist that has the scatter gather entries. Scatter-gather enables a write
981 * of multiple none-contiguous areas of an object, in a single call. The extents
982 * may overlap and/or be in any order. The only constrain is that:
983 * total_bytes(sglist) >= total_bytes(bio)
984 */
985int osd_req_write_sg(struct osd_request *or,
986 const struct osd_obj_id *obj, struct bio *bio,
987 const struct osd_sg_entry *sglist, unsigned numentries)
988{
989 u64 len;
990 int ret = _add_sg_continuation_descriptor(or, sglist, numentries, &len);
991
992 if (ret)
993 return ret;
994 osd_req_write(or, obj, 0, bio, len);
995
996 return 0;
997}
998EXPORT_SYMBOL(osd_req_write_sg);
999
1000/* osd_req_read_sg: Read multiple extents of an object into @bio
1001 * See osd_req_write_sg
1002 */
1003int osd_req_read_sg(struct osd_request *or,
1004 const struct osd_obj_id *obj, struct bio *bio,
1005 const struct osd_sg_entry *sglist, unsigned numentries)
1006{
1007 u64 len;
1008 int ret = _add_sg_continuation_descriptor(or, sglist, numentries, &len);
1009
1010 if (ret)
1011 return ret;
1012 osd_req_read(or, obj, 0, bio, len);
1013
1014 return 0;
1015}
1016EXPORT_SYMBOL(osd_req_read_sg);
1017
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001018void osd_req_get_attributes(struct osd_request *or,
1019 const struct osd_obj_id *obj)
1020{
1021 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
1022}
1023EXPORT_SYMBOL(osd_req_get_attributes);
1024
1025void osd_req_set_attributes(struct osd_request *or,
1026 const struct osd_obj_id *obj)
1027{
1028 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
1029}
1030EXPORT_SYMBOL(osd_req_set_attributes);
1031
1032/*
1033 * Attributes List-mode
1034 */
1035
1036int osd_req_add_set_attr_list(struct osd_request *or,
1037 const struct osd_attr *oa, unsigned nelem)
1038{
1039 unsigned total_bytes = or->set_attr.total_bytes;
1040 void *attr_last;
1041 int ret;
1042
1043 if (or->attributes_mode &&
1044 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
1045 WARN_ON(1);
1046 return -EINVAL;
1047 }
1048 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1049
1050 if (!total_bytes) { /* first-time: allocate and put list header */
1051 total_bytes = _osd_req_sizeof_alist_header(or);
1052 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
1053 if (ret)
1054 return ret;
1055 _osd_req_set_alist_type(or, or->set_attr.buff,
1056 OSD_ATTR_LIST_SET_RETRIEVE);
1057 }
1058 attr_last = or->set_attr.buff + total_bytes;
1059
1060 for (; nelem; --nelem) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001061 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
1062
1063 total_bytes += elem_size;
1064 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
1065 or->set_attr.total_bytes = total_bytes - elem_size;
1066 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
1067 if (ret)
1068 return ret;
1069 attr_last =
1070 or->set_attr.buff + or->set_attr.total_bytes;
1071 }
1072
Boaz Harrosh71f32e32009-04-19 19:11:42 +03001073 _osd_req_alist_elem_encode(or, attr_last, oa);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001074
1075 attr_last += elem_size;
1076 ++oa;
1077 }
1078
1079 or->set_attr.total_bytes = total_bytes;
1080 return 0;
1081}
1082EXPORT_SYMBOL(osd_req_add_set_attr_list);
1083
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001084static int _req_append_segment(struct osd_request *or,
1085 unsigned padding, struct _osd_req_data_segment *seg,
1086 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
1087{
1088 void *pad_buff;
1089 int ret;
1090
1091 if (padding) {
1092 /* check if we can just add it to last buffer */
1093 if (last_seg &&
1094 (padding <= last_seg->alloc_size - last_seg->total_bytes))
1095 pad_buff = last_seg->buff + last_seg->total_bytes;
1096 else
1097 pad_buff = io->pad_buff;
1098
Boaz Harroshbc38bf12009-05-17 18:56:17 +03001099 ret = blk_rq_map_kern(io->req->q, io->req, pad_buff, padding,
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001100 or->alloc_flags);
1101 if (ret)
1102 return ret;
1103 io->total_bytes += padding;
1104 }
1105
Boaz Harroshbc38bf12009-05-17 18:56:17 +03001106 ret = blk_rq_map_kern(io->req->q, io->req, seg->buff, seg->total_bytes,
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001107 or->alloc_flags);
1108 if (ret)
1109 return ret;
1110
1111 io->total_bytes += seg->total_bytes;
1112 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
1113 seg->total_bytes);
1114 return 0;
1115}
1116
1117static int _osd_req_finalize_set_attr_list(struct osd_request *or)
1118{
1119 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1120 unsigned padding;
1121 int ret;
1122
1123 if (!or->set_attr.total_bytes) {
1124 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
1125 return 0;
1126 }
1127
1128 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
1129 cdbh->attrs_list.set_attr_offset =
1130 osd_req_encode_offset(or, or->out.total_bytes, &padding);
1131
1132 ret = _req_append_segment(or, padding, &or->set_attr,
1133 or->out.last_seg, &or->out);
1134 if (ret)
1135 return ret;
1136
1137 or->out.last_seg = &or->set_attr;
1138 return 0;
1139}
1140
1141int osd_req_add_get_attr_list(struct osd_request *or,
1142 const struct osd_attr *oa, unsigned nelem)
1143{
1144 unsigned total_bytes = or->enc_get_attr.total_bytes;
1145 void *attr_last;
1146 int ret;
1147
1148 if (or->attributes_mode &&
1149 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
1150 WARN_ON(1);
1151 return -EINVAL;
1152 }
1153 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1154
1155 /* first time calc data-in list header size */
1156 if (!or->get_attr.total_bytes)
1157 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
1158
1159 /* calc data-out info */
1160 if (!total_bytes) { /* first-time: allocate and put list header */
1161 unsigned max_bytes;
1162
1163 total_bytes = _osd_req_sizeof_alist_header(or);
1164 max_bytes = total_bytes +
1165 nelem * sizeof(struct osd_attributes_list_attrid);
1166 ret = _alloc_get_attr_desc(or, max_bytes);
1167 if (ret)
1168 return ret;
1169
1170 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
1171 OSD_ATTR_LIST_GET);
1172 }
1173 attr_last = or->enc_get_attr.buff + total_bytes;
1174
1175 for (; nelem; --nelem) {
1176 struct osd_attributes_list_attrid *attrid;
1177 const unsigned cur_size = sizeof(*attrid);
1178
1179 total_bytes += cur_size;
1180 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
1181 or->enc_get_attr.total_bytes = total_bytes - cur_size;
1182 ret = _alloc_get_attr_desc(or,
1183 total_bytes + nelem * sizeof(*attrid));
1184 if (ret)
1185 return ret;
1186 attr_last = or->enc_get_attr.buff +
1187 or->enc_get_attr.total_bytes;
1188 }
1189
1190 attrid = attr_last;
1191 attrid->attr_page = cpu_to_be32(oa->attr_page);
1192 attrid->attr_id = cpu_to_be32(oa->attr_id);
1193
1194 attr_last += cur_size;
1195
1196 /* calc data-in size */
1197 or->get_attr.total_bytes +=
1198 _osd_req_alist_elem_size(or, oa->len);
1199 ++oa;
1200 }
1201
1202 or->enc_get_attr.total_bytes = total_bytes;
1203
1204 OSD_DEBUG(
1205 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
1206 or->get_attr.total_bytes,
1207 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
1208 or->enc_get_attr.total_bytes,
1209 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
1210 / sizeof(struct osd_attributes_list_attrid));
1211
1212 return 0;
1213}
1214EXPORT_SYMBOL(osd_req_add_get_attr_list);
1215
1216static int _osd_req_finalize_get_attr_list(struct osd_request *or)
1217{
1218 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1219 unsigned out_padding;
1220 unsigned in_padding;
1221 int ret;
1222
1223 if (!or->enc_get_attr.total_bytes) {
1224 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
1225 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
1226 return 0;
1227 }
1228
1229 ret = _alloc_get_attr_list(or);
1230 if (ret)
1231 return ret;
1232
1233 /* The out-going buffer info update */
1234 OSD_DEBUG("out-going\n");
1235 cdbh->attrs_list.get_attr_desc_bytes =
1236 cpu_to_be32(or->enc_get_attr.total_bytes);
1237
1238 cdbh->attrs_list.get_attr_desc_offset =
1239 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1240
1241 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
1242 or->out.last_seg, &or->out);
1243 if (ret)
1244 return ret;
1245 or->out.last_seg = &or->enc_get_attr;
1246
1247 /* The incoming buffer info update */
1248 OSD_DEBUG("in-coming\n");
1249 cdbh->attrs_list.get_attr_alloc_length =
1250 cpu_to_be32(or->get_attr.total_bytes);
1251
1252 cdbh->attrs_list.get_attr_offset =
1253 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1254
1255 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1256 &or->in);
1257 if (ret)
1258 return ret;
1259 or->in.last_seg = &or->get_attr;
1260
1261 return 0;
1262}
1263
1264int osd_req_decode_get_attr_list(struct osd_request *or,
1265 struct osd_attr *oa, int *nelem, void **iterator)
1266{
1267 unsigned cur_bytes, returned_bytes;
1268 int n;
1269 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
1270 void *cur_p;
1271
1272 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
1273 OSD_ATTR_LIST_SET_RETRIEVE)) {
1274 oa->attr_page = 0;
1275 oa->attr_id = 0;
1276 oa->val_ptr = NULL;
1277 oa->len = 0;
1278 *iterator = NULL;
1279 return 0;
1280 }
1281
1282 if (*iterator) {
1283 BUG_ON((*iterator < or->get_attr.buff) ||
1284 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
1285 cur_p = *iterator;
1286 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
1287 returned_bytes = or->get_attr.total_bytes;
1288 } else { /* first time decode the list header */
1289 cur_bytes = sizeof_attr_list;
1290 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
1291 sizeof_attr_list;
1292
1293 cur_p = or->get_attr.buff + sizeof_attr_list;
1294
1295 if (returned_bytes > or->get_attr.alloc_size) {
1296 OSD_DEBUG("target report: space was not big enough! "
1297 "Allocate=%u Needed=%u\n",
1298 or->get_attr.alloc_size,
1299 returned_bytes + sizeof_attr_list);
1300
1301 returned_bytes =
1302 or->get_attr.alloc_size - sizeof_attr_list;
1303 }
1304 or->get_attr.total_bytes = returned_bytes;
1305 }
1306
1307 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
Boaz Harrosh71f32e32009-04-19 19:11:42 +03001308 int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
1309 returned_bytes - cur_bytes);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001310
Boaz Harrosh71f32e32009-04-19 19:11:42 +03001311 if (inc < 0) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001312 OSD_ERR("BAD FOOD from target. list not valid!"
1313 "c=%d r=%d n=%d\n",
1314 cur_bytes, returned_bytes, n);
1315 oa->val_ptr = NULL;
Boaz Harrosheff21492009-11-16 20:47:47 +02001316 cur_bytes = returned_bytes; /* break the caller loop */
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001317 break;
1318 }
1319
Boaz Harrosh71f32e32009-04-19 19:11:42 +03001320 cur_bytes += inc;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001321 cur_p += inc;
1322 ++oa;
1323 }
1324
1325 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
1326 *nelem = n;
1327 return returned_bytes - cur_bytes;
1328}
1329EXPORT_SYMBOL(osd_req_decode_get_attr_list);
1330
1331/*
1332 * Attributes Page-mode
1333 */
1334
1335int osd_req_add_get_attr_page(struct osd_request *or,
1336 u32 page_id, void *attar_page, unsigned max_page_len,
1337 const struct osd_attr *set_one_attr)
1338{
1339 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1340
1341 if (or->attributes_mode &&
1342 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1343 WARN_ON(1);
1344 return -EINVAL;
1345 }
1346 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
1347
1348 or->get_attr.buff = attar_page;
1349 or->get_attr.total_bytes = max_page_len;
1350
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001351 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
1352 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
Boaz Harroshec6a0a42010-10-19 14:20:31 +02001353
1354 if (!set_one_attr || !set_one_attr->attr_page)
1355 return 0; /* The set is optional */
1356
1357 or->set_attr.buff = set_one_attr->val_ptr;
1358 or->set_attr.total_bytes = set_one_attr->len;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001359
1360 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
1361 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
1362 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001363 return 0;
1364}
1365EXPORT_SYMBOL(osd_req_add_get_attr_page);
1366
1367static int _osd_req_finalize_attr_page(struct osd_request *or)
1368{
1369 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1370 unsigned in_padding, out_padding;
1371 int ret;
1372
1373 /* returned page */
1374 cdbh->attrs_page.get_attr_offset =
1375 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1376
1377 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1378 &or->in);
1379 if (ret)
1380 return ret;
1381
Boaz Harroshec6a0a42010-10-19 14:20:31 +02001382 if (or->set_attr.total_bytes == 0)
1383 return 0;
1384
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001385 /* set one value */
1386 cdbh->attrs_page.set_attr_offset =
1387 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1388
Boaz Harroshec6a0a42010-10-19 14:20:31 +02001389 ret = _req_append_segment(or, out_padding, &or->set_attr, NULL,
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001390 &or->out);
1391 return ret;
1392}
1393
Boaz Harroshf8d3a642009-04-19 19:13:39 +03001394static inline void osd_sec_parms_set_out_offset(bool is_v1,
1395 struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
1396{
1397 if (is_v1)
1398 sec_parms->v1.data_out_integrity_check_offset = offset;
1399 else
1400 sec_parms->v2.data_out_integrity_check_offset = offset;
1401}
1402
1403static inline void osd_sec_parms_set_in_offset(bool is_v1,
1404 struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
1405{
1406 if (is_v1)
1407 sec_parms->v1.data_in_integrity_check_offset = offset;
1408 else
1409 sec_parms->v2.data_in_integrity_check_offset = offset;
1410}
1411
Boaz Harrosh345c4352009-01-25 17:03:07 +02001412static int _osd_req_finalize_data_integrity(struct osd_request *or,
Boaz Harroshe96e72c2010-10-19 14:22:21 +02001413 bool has_in, bool has_out, struct bio *out_data_bio, u64 out_data_bytes,
1414 const u8 *cap_key)
Boaz Harrosh345c4352009-01-25 17:03:07 +02001415{
1416 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1417 int ret;
1418
1419 if (!osd_is_sec_alldata(sec_parms))
1420 return 0;
1421
1422 if (has_out) {
1423 struct _osd_req_data_segment seg = {
1424 .buff = &or->out_data_integ,
1425 .total_bytes = sizeof(or->out_data_integ),
1426 };
1427 unsigned pad;
1428
Boaz Harrosh546881a2009-05-24 20:04:00 +03001429 or->out_data_integ.data_bytes = cpu_to_be64(out_data_bytes);
Boaz Harrosh345c4352009-01-25 17:03:07 +02001430 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
1431 or->set_attr.total_bytes);
1432 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
1433 or->enc_get_attr.total_bytes);
1434
Boaz Harroshf8d3a642009-04-19 19:13:39 +03001435 osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
1436 osd_req_encode_offset(or, or->out.total_bytes, &pad));
Boaz Harrosh345c4352009-01-25 17:03:07 +02001437
1438 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1439 &or->out);
1440 if (ret)
1441 return ret;
1442 or->out.last_seg = NULL;
1443
1444 /* they are now all chained to request sign them all together */
Boaz Harroshe96e72c2010-10-19 14:22:21 +02001445 osd_sec_sign_data(&or->out_data_integ, out_data_bio,
Boaz Harrosh345c4352009-01-25 17:03:07 +02001446 cap_key);
1447 }
1448
1449 if (has_in) {
1450 struct _osd_req_data_segment seg = {
1451 .buff = &or->in_data_integ,
1452 .total_bytes = sizeof(or->in_data_integ),
1453 };
1454 unsigned pad;
1455
Boaz Harroshf8d3a642009-04-19 19:13:39 +03001456 osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
1457 osd_req_encode_offset(or, or->in.total_bytes, &pad));
Boaz Harrosh345c4352009-01-25 17:03:07 +02001458
1459 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1460 &or->in);
1461 if (ret)
1462 return ret;
1463
1464 or->in.last_seg = NULL;
1465 }
1466
1467 return 0;
1468}
1469
Boaz Harrosh02941a52009-01-25 16:55:30 +02001470/*
1471 * osd_finalize_request and helpers
1472 */
Boaz Harroshc29b70f2009-05-17 18:58:41 +03001473static struct request *_make_request(struct request_queue *q, bool has_write,
1474 struct _osd_io_info *oii, gfp_t flags)
1475{
1476 if (oii->bio)
1477 return blk_make_request(q, oii->bio, flags);
1478 else {
1479 struct request *req;
1480
1481 req = blk_get_request(q, has_write ? WRITE : READ, flags);
1482 if (unlikely(!req))
1483 return ERR_PTR(-ENOMEM);
1484
1485 return req;
1486 }
1487}
Boaz Harrosh02941a52009-01-25 16:55:30 +02001488
1489static int _init_blk_request(struct osd_request *or,
1490 bool has_in, bool has_out)
1491{
1492 gfp_t flags = or->alloc_flags;
1493 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1494 struct request_queue *q = scsi_device->request_queue;
1495 struct request *req;
Boaz Harroshc29b70f2009-05-17 18:58:41 +03001496 int ret;
Boaz Harrosh02941a52009-01-25 16:55:30 +02001497
Boaz Harroshc29b70f2009-05-17 18:58:41 +03001498 req = _make_request(q, has_out, has_out ? &or->out : &or->in, flags);
1499 if (IS_ERR(req)) {
1500 ret = PTR_ERR(req);
Boaz Harrosh02941a52009-01-25 16:55:30 +02001501 goto out;
Boaz Harroshc29b70f2009-05-17 18:58:41 +03001502 }
Boaz Harrosh02941a52009-01-25 16:55:30 +02001503
1504 or->request = req;
1505 req->cmd_type = REQ_TYPE_BLOCK_PC;
Boaz Harrosh03306792009-05-24 20:05:43 +03001506 req->cmd_flags |= REQ_QUIET;
1507
Boaz Harrosh02941a52009-01-25 16:55:30 +02001508 req->timeout = or->timeout;
1509 req->retries = or->retries;
1510 req->sense = or->sense;
1511 req->sense_len = 0;
1512
1513 if (has_out) {
1514 or->out.req = req;
1515 if (has_in) {
1516 /* allocate bidi request */
Boaz Harroshc29b70f2009-05-17 18:58:41 +03001517 req = _make_request(q, false, &or->in, flags);
1518 if (IS_ERR(req)) {
Boaz Harrosh02941a52009-01-25 16:55:30 +02001519 OSD_DEBUG("blk_get_request for bidi failed\n");
Boaz Harroshc29b70f2009-05-17 18:58:41 +03001520 ret = PTR_ERR(req);
Boaz Harrosh02941a52009-01-25 16:55:30 +02001521 goto out;
1522 }
1523 req->cmd_type = REQ_TYPE_BLOCK_PC;
1524 or->in.req = or->request->next_rq = req;
1525 }
1526 } else if (has_in)
1527 or->in.req = req;
1528
1529 ret = 0;
1530out:
1531 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1532 or, has_in, has_out, ret, or->request);
1533 return ret;
1534}
1535
1536int osd_finalize_request(struct osd_request *or,
1537 u8 options, const void *cap, const u8 *cap_key)
1538{
1539 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1540 bool has_in, has_out;
Boaz Harroshe96e72c2010-10-19 14:22:21 +02001541 /* Save for data_integrity without the cdb_continuation */
1542 struct bio *out_data_bio = or->out.bio;
Boaz Harrosh546881a2009-05-24 20:04:00 +03001543 u64 out_data_bytes = or->out.total_bytes;
Boaz Harrosh02941a52009-01-25 16:55:30 +02001544 int ret;
1545
1546 if (options & OSD_REQ_FUA)
1547 cdbh->options |= OSD_CDB_FUA;
1548
1549 if (options & OSD_REQ_DPO)
1550 cdbh->options |= OSD_CDB_DPO;
1551
1552 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1553 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1554
1555 osd_set_caps(&or->cdb, cap);
1556
1557 has_in = or->in.bio || or->get_attr.total_bytes;
Boaz Harroshe96e72c2010-10-19 14:22:21 +02001558 has_out = or->out.bio || or->cdb_cont.total_bytes ||
1559 or->set_attr.total_bytes || or->enc_get_attr.total_bytes;
Boaz Harrosh02941a52009-01-25 16:55:30 +02001560
Boaz Harroshe96e72c2010-10-19 14:22:21 +02001561 ret = _osd_req_finalize_cdb_cont(or, cap_key);
1562 if (ret) {
1563 OSD_DEBUG("_osd_req_finalize_cdb_cont failed\n");
1564 return ret;
1565 }
Boaz Harrosh02941a52009-01-25 16:55:30 +02001566 ret = _init_blk_request(or, has_in, has_out);
1567 if (ret) {
1568 OSD_DEBUG("_init_blk_request failed\n");
1569 return ret;
1570 }
1571
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001572 or->out.pad_buff = sg_out_pad_buffer;
1573 or->in.pad_buff = sg_in_pad_buffer;
1574
Boaz Harrosh02941a52009-01-25 16:55:30 +02001575 if (!or->attributes_mode)
1576 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1577 cdbh->command_specific_options |= or->attributes_mode;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001578 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1579 ret = _osd_req_finalize_attr_page(or);
Boaz Harrosh98e1e0f2010-02-19 11:46:24 -08001580 if (ret) {
1581 OSD_DEBUG("_osd_req_finalize_attr_page failed\n");
1582 return ret;
1583 }
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001584 } else {
1585 /* TODO: I think that for the GET_ATTR command these 2 should
1586 * be reversed to keep them in execution order (for embeded
1587 * targets with low memory footprint)
1588 */
1589 ret = _osd_req_finalize_set_attr_list(or);
1590 if (ret) {
1591 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1592 return ret;
1593 }
1594
1595 ret = _osd_req_finalize_get_attr_list(or);
1596 if (ret) {
1597 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1598 return ret;
1599 }
1600 }
Boaz Harrosh02941a52009-01-25 16:55:30 +02001601
Boaz Harrosh546881a2009-05-24 20:04:00 +03001602 ret = _osd_req_finalize_data_integrity(or, has_in, has_out,
Boaz Harroshe96e72c2010-10-19 14:22:21 +02001603 out_data_bio, out_data_bytes,
1604 cap_key);
Boaz Harrosh345c4352009-01-25 17:03:07 +02001605 if (ret)
1606 return ret;
1607
1608 osd_sec_sign_cdb(&or->cdb, cap_key);
1609
Boaz Harrosh02941a52009-01-25 16:55:30 +02001610 or->request->cmd = or->cdb.buff;
1611 or->request->cmd_len = _osd_req_cdb_len(or);
1612
1613 return 0;
1614}
1615EXPORT_SYMBOL(osd_finalize_request);
1616
Boaz Harroshaa9fffb2009-11-16 20:48:38 +02001617static bool _is_osd_security_code(int code)
1618{
1619 return (code == osd_security_audit_value_frozen) ||
1620 (code == osd_security_working_key_frozen) ||
1621 (code == osd_nonce_not_unique) ||
1622 (code == osd_nonce_timestamp_out_of_range) ||
1623 (code == osd_invalid_dataout_buffer_integrity_check_value);
1624}
1625
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001626#define OSD_SENSE_PRINT1(fmt, a...) \
1627 do { \
1628 if (__cur_sense_need_output) \
1629 OSD_ERR(fmt, ##a); \
1630 } while (0)
1631
1632#define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
1633
1634int osd_req_decode_sense_full(struct osd_request *or,
1635 struct osd_sense_info *osi, bool silent,
1636 struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
1637 struct osd_attr *bad_attr_list, int max_attr)
1638{
1639 int sense_len, original_sense_len;
1640 struct osd_sense_info local_osi;
1641 struct scsi_sense_descriptor_based *ssdb;
1642 void *cur_descriptor;
1643#if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
1644 const bool __cur_sense_need_output = false;
1645#else
1646 bool __cur_sense_need_output = !silent;
1647#endif
Boaz Harroshaa9fffb2009-11-16 20:48:38 +02001648 int ret;
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001649
Boaz Harrosh5d0961f2009-12-01 17:36:21 +02001650 if (likely(!or->req_errors))
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001651 return 0;
Boaz Harroshaa9fffb2009-11-16 20:48:38 +02001652
1653 osi = osi ? : &local_osi;
1654 memset(osi, 0, sizeof(*osi));
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001655
Boaz Harrosh5d0961f2009-12-01 17:36:21 +02001656 ssdb = (typeof(ssdb))or->sense;
1657 sense_len = or->sense_len;
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001658 if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
1659 OSD_ERR("Block-layer returned error(0x%x) but "
1660 "sense_len(%u) || key(%d) is empty\n",
Boaz Harrosh5d0961f2009-12-01 17:36:21 +02001661 or->req_errors, sense_len, ssdb->sense_key);
Boaz Harroshaa9fffb2009-11-16 20:48:38 +02001662 goto analyze;
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001663 }
1664
1665 if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
1666 OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
1667 ssdb->response_code, sense_len);
Boaz Harroshaa9fffb2009-11-16 20:48:38 +02001668 goto analyze;
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001669 }
1670
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001671 osi->key = ssdb->sense_key;
1672 osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
1673 original_sense_len = ssdb->additional_sense_length + 8;
1674
1675#if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
1676 if (__cur_sense_need_output)
1677 __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
1678#endif
1679 OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
Boaz Harroshaa9fffb2009-11-16 20:48:38 +02001680 "additional_code=0x%x async_error=%d errors=0x%x\n",
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001681 osi->key, original_sense_len, sense_len,
Boaz Harroshaa9fffb2009-11-16 20:48:38 +02001682 osi->additional_code, or->async_error,
Boaz Harrosh5d0961f2009-12-01 17:36:21 +02001683 or->req_errors);
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001684
1685 if (original_sense_len < sense_len)
1686 sense_len = original_sense_len;
1687
1688 cur_descriptor = ssdb->ssd;
1689 sense_len -= sizeof(*ssdb);
1690 while (sense_len > 0) {
1691 struct scsi_sense_descriptor *ssd = cur_descriptor;
1692 int cur_len = ssd->additional_length + 2;
1693
1694 sense_len -= cur_len;
1695
1696 if (sense_len < 0)
1697 break; /* sense was truncated */
1698
1699 switch (ssd->descriptor_type) {
1700 case scsi_sense_information:
1701 case scsi_sense_command_specific_information:
1702 {
1703 struct scsi_sense_command_specific_data_descriptor
1704 *sscd = cur_descriptor;
1705
1706 osi->command_info =
1707 get_unaligned_be64(&sscd->information) ;
1708 OSD_SENSE_PRINT2(
1709 "command_specific_information 0x%llx \n",
1710 _LLU(osi->command_info));
1711 break;
1712 }
1713 case scsi_sense_key_specific:
1714 {
1715 struct scsi_sense_key_specific_data_descriptor
1716 *ssks = cur_descriptor;
1717
1718 osi->sense_info = get_unaligned_be16(&ssks->value);
1719 OSD_SENSE_PRINT2(
1720 "sense_key_specific_information %u"
1721 "sksv_cd_bpv_bp (0x%x)\n",
1722 osi->sense_info, ssks->sksv_cd_bpv_bp);
1723 break;
1724 }
1725 case osd_sense_object_identification:
1726 { /*FIXME: Keep first not last, Store in array*/
1727 struct osd_sense_identification_data_descriptor
1728 *osidd = cur_descriptor;
1729
1730 osi->not_initiated_command_functions =
1731 le32_to_cpu(osidd->not_initiated_functions);
1732 osi->completed_command_functions =
1733 le32_to_cpu(osidd->completed_functions);
1734 osi->obj.partition = be64_to_cpu(osidd->partition_id);
1735 osi->obj.id = be64_to_cpu(osidd->object_id);
1736 OSD_SENSE_PRINT2(
1737 "object_identification pid=0x%llx oid=0x%llx\n",
1738 _LLU(osi->obj.partition), _LLU(osi->obj.id));
1739 OSD_SENSE_PRINT2(
1740 "not_initiated_bits(%x) "
1741 "completed_command_bits(%x)\n",
1742 osi->not_initiated_command_functions,
1743 osi->completed_command_functions);
1744 break;
1745 }
1746 case osd_sense_response_integrity_check:
1747 {
1748 struct osd_sense_response_integrity_check_descriptor
1749 *osricd = cur_descriptor;
1750 const unsigned len =
1751 sizeof(osricd->integrity_check_value);
1752 char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
1753
1754 hex_dump_to_buffer(osricd->integrity_check_value, len,
1755 32, 1, key_dump, sizeof(key_dump), true);
1756 OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
1757 }
1758 case osd_sense_attribute_identification:
1759 {
1760 struct osd_sense_attributes_data_descriptor
1761 *osadd = cur_descriptor;
Boaz Harrosh71ecb742009-11-16 20:47:07 +02001762 unsigned len = min(cur_len, sense_len);
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001763 struct osd_sense_attr *pattr = osadd->sense_attrs;
1764
Boaz Harrosh71ecb742009-11-16 20:47:07 +02001765 while (len >= sizeof(*pattr)) {
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001766 u32 attr_page = be32_to_cpu(pattr->attr_page);
1767 u32 attr_id = be32_to_cpu(pattr->attr_id);
1768
Boaz Harrosh71ecb742009-11-16 20:47:07 +02001769 if (!osi->attr.attr_page) {
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001770 osi->attr.attr_page = attr_page;
1771 osi->attr.attr_id = attr_id;
1772 }
1773
1774 if (bad_attr_list && max_attr) {
1775 bad_attr_list->attr_page = attr_page;
1776 bad_attr_list->attr_id = attr_id;
1777 bad_attr_list++;
1778 max_attr--;
1779 }
Boaz Harrosh71ecb742009-11-16 20:47:07 +02001780
1781 len -= sizeof(*pattr);
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001782 OSD_SENSE_PRINT2(
1783 "osd_sense_attribute_identification"
1784 "attr_page=0x%x attr_id=0x%x\n",
1785 attr_page, attr_id);
1786 }
1787 }
1788 /*These are not legal for OSD*/
1789 case scsi_sense_field_replaceable_unit:
1790 OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
1791 break;
1792 case scsi_sense_stream_commands:
1793 OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
1794 break;
1795 case scsi_sense_block_commands:
1796 OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
1797 break;
1798 case scsi_sense_ata_return:
1799 OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
1800 break;
1801 default:
1802 if (ssd->descriptor_type <= scsi_sense_Reserved_last)
1803 OSD_SENSE_PRINT2(
1804 "scsi_sense Reserved descriptor (0x%x)",
1805 ssd->descriptor_type);
1806 else
1807 OSD_SENSE_PRINT2(
1808 "scsi_sense Vendor descriptor (0x%x)",
1809 ssd->descriptor_type);
1810 }
1811
1812 cur_descriptor += cur_len;
1813 }
1814
Boaz Harroshaa9fffb2009-11-16 20:48:38 +02001815analyze:
1816 if (!osi->key) {
1817 /* scsi sense is Empty, the request was never issued to target
1818 * linux return code might tell us what happened.
1819 */
1820 if (or->async_error == -ENOMEM)
1821 osi->osd_err_pri = OSD_ERR_PRI_RESOURCE;
1822 else
1823 osi->osd_err_pri = OSD_ERR_PRI_UNREACHABLE;
1824 ret = or->async_error;
1825 } else if (osi->key <= scsi_sk_recovered_error) {
1826 osi->osd_err_pri = 0;
1827 ret = 0;
1828 } else if (osi->additional_code == scsi_invalid_field_in_cdb) {
1829 if (osi->cdb_field_offset == OSD_CFO_STARTING_BYTE) {
1830 osi->osd_err_pri = OSD_ERR_PRI_CLEAR_PAGES;
1831 ret = -EFAULT; /* caller should recover from this */
1832 } else if (osi->cdb_field_offset == OSD_CFO_OBJECT_ID) {
1833 osi->osd_err_pri = OSD_ERR_PRI_NOT_FOUND;
1834 ret = -ENOENT;
1835 } else if (osi->cdb_field_offset == OSD_CFO_PERMISSIONS) {
1836 osi->osd_err_pri = OSD_ERR_PRI_NO_ACCESS;
1837 ret = -EACCES;
1838 } else {
1839 osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
1840 ret = -EINVAL;
1841 }
1842 } else if (osi->additional_code == osd_quota_error) {
1843 osi->osd_err_pri = OSD_ERR_PRI_NO_SPACE;
1844 ret = -ENOSPC;
1845 } else if (_is_osd_security_code(osi->additional_code)) {
1846 osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
1847 ret = -EINVAL;
1848 } else {
1849 osi->osd_err_pri = OSD_ERR_PRI_EIO;
1850 ret = -EIO;
1851 }
1852
Boaz Harrosh5d0961f2009-12-01 17:36:21 +02001853 if (!or->out.residual)
1854 or->out.residual = or->out.total_bytes;
1855 if (!or->in.residual)
1856 or->in.residual = or->in.total_bytes;
Boaz Harroshaa9fffb2009-11-16 20:48:38 +02001857
1858 return ret;
Boaz Harrosh98f3aea2009-01-25 17:15:16 +02001859}
1860EXPORT_SYMBOL(osd_req_decode_sense_full);
1861
Boaz Harrosh02941a52009-01-25 16:55:30 +02001862/*
1863 * Implementation of osd_sec.h API
1864 * TODO: Move to a separate osd_sec.c file at a later stage.
1865 */
1866
1867enum { OSD_SEC_CAP_V1_ALL_CAPS =
1868 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1869 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1870 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1871 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1872};
1873
Boaz Harroshc6572c92009-01-25 17:09:40 +02001874enum { OSD_SEC_CAP_V2_ALL_CAPS =
1875 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1876};
1877
Boaz Harrosh02941a52009-01-25 16:55:30 +02001878void osd_sec_init_nosec_doall_caps(void *caps,
1879 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1880{
1881 struct osd_capability *cap = caps;
1882 u8 type;
1883 u8 descriptor_type;
1884
1885 if (likely(obj->id)) {
1886 if (unlikely(is_collection)) {
1887 type = OSD_SEC_OBJ_COLLECTION;
1888 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1889 OSD_SEC_OBJ_DESC_COL;
1890 } else {
1891 type = OSD_SEC_OBJ_USER;
1892 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1893 }
1894 WARN_ON(!obj->partition);
1895 } else {
1896 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1897 OSD_SEC_OBJ_ROOT;
1898 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1899 }
1900
1901 memset(cap, 0, sizeof(*cap));
1902
1903 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1904 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1905 cap->h.security_method = OSD_SEC_NOSEC;
1906/* cap->expiration_time;
1907 cap->AUDIT[30-10];
1908 cap->discriminator[42-30];
1909 cap->object_created_time; */
1910 cap->h.object_type = type;
1911 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1912 cap->h.object_descriptor_type = descriptor_type;
1913 cap->od.obj_desc.policy_access_tag = 0;
1914 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1915 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1916}
1917EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1918
Boaz Harroshc6572c92009-01-25 17:09:40 +02001919/* FIXME: Extract version from caps pointer.
1920 * Also Pete's target only supports caps from OSDv1 for now
1921 */
Boaz Harrosh02941a52009-01-25 16:55:30 +02001922void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1923{
Boaz Harroshc6572c92009-01-25 17:09:40 +02001924 bool is_ver1 = true;
1925 /* NOTE: They start at same address */
1926 memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
Boaz Harrosh02941a52009-01-25 16:55:30 +02001927}
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001928
Boaz Harrosh345c4352009-01-25 17:03:07 +02001929bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1930{
1931 return false;
1932}
1933
1934void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1935{
1936}
1937
1938void osd_sec_sign_data(void *data_integ __unused,
1939 struct bio *bio __unused, const u8 *cap_key __unused)
1940{
1941}
1942
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001943/*
1944 * Declared in osd_protocol.h
1945 * 4.12.5 Data-In and Data-Out buffer offsets
1946 * byte offset = mantissa * (2^(exponent+8))
1947 * Returns the smallest allowed encoded offset that contains given @offset
1948 * The actual encoded offset returned is @offset + *@padding.
1949 */
1950osd_cdb_offset __osd_encode_offset(
1951 u64 offset, unsigned *padding, int min_shift, int max_shift)
1952{
1953 u64 try_offset = -1, mod, align;
1954 osd_cdb_offset be32_offset;
1955 int shift;
1956
1957 *padding = 0;
1958 if (!offset)
1959 return 0;
1960
1961 for (shift = min_shift; shift < max_shift; ++shift) {
1962 try_offset = offset >> shift;
1963 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1964 break;
1965 }
1966
1967 BUG_ON(shift == max_shift);
1968
1969 align = 1 << shift;
1970 mod = offset & (align - 1);
1971 if (mod) {
1972 *padding = align - mod;
1973 try_offset += 1;
1974 }
1975
1976 try_offset |= ((shift - 8) & 0xf) << 28;
1977 be32_offset = cpu_to_be32((u32)try_offset);
1978
1979 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1980 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1981 be32_offset, *padding);
1982 return be32_offset;
1983}