blob: 86a76cccfebd55f5cca80e7f031e7e49d3eab49c [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
42#include <scsi/osd_initiator.h>
43#include <scsi/osd_sec.h>
44#include <scsi/scsi_device.h>
45
46#include "osd_debug.h"
47
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020048#ifndef __unused
49# define __unused __attribute__((unused))
50#endif
51
Boaz Harrosh02941a52009-01-25 16:55:30 +020052enum { OSD_REQ_RETRIES = 1 };
53
54MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
55MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
56MODULE_LICENSE("GPL");
57
58static inline void build_test(void)
59{
60 /* structures were not packed */
61 BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
Boaz Harroshc6572c92009-01-25 17:09:40 +020062 BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN);
Boaz Harrosh02941a52009-01-25 16:55:30 +020063 BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
64}
65
66static unsigned _osd_req_cdb_len(struct osd_request *or)
67{
Boaz Harroshc6572c92009-01-25 17:09:40 +020068 return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
Boaz Harrosh02941a52009-01-25 16:55:30 +020069}
70
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020071static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
72{
Boaz Harroshc6572c92009-01-25 17:09:40 +020073 return osd_req_is_ver1(or) ?
74 osdv1_attr_list_elem_size(len) :
75 osdv2_attr_list_elem_size(len);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020076}
77
78static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
79{
Boaz Harroshc6572c92009-01-25 17:09:40 +020080 return osd_req_is_ver1(or) ?
81 osdv1_list_size(list_head) :
82 osdv2_list_size(list_head);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020083}
84
85static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
86{
Boaz Harroshc6572c92009-01-25 17:09:40 +020087 return osd_req_is_ver1(or) ?
88 sizeof(struct osdv1_attributes_list_header) :
89 sizeof(struct osdv2_attributes_list_header);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020090}
91
92static void _osd_req_set_alist_type(struct osd_request *or,
93 void *list, int list_type)
94{
Boaz Harroshc6572c92009-01-25 17:09:40 +020095 if (osd_req_is_ver1(or)) {
96 struct osdv1_attributes_list_header *attr_list = list;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +020097
Boaz Harroshc6572c92009-01-25 17:09:40 +020098 memset(attr_list, 0, sizeof(*attr_list));
99 attr_list->type = list_type;
100 } else {
101 struct osdv2_attributes_list_header *attr_list = list;
102
103 memset(attr_list, 0, sizeof(*attr_list));
104 attr_list->type = list_type;
105 }
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200106}
107
108static bool _osd_req_is_alist_type(struct osd_request *or,
109 void *list, int list_type)
110{
111 if (!list)
112 return false;
113
Boaz Harroshc6572c92009-01-25 17:09:40 +0200114 if (osd_req_is_ver1(or)) {
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200115 struct osdv1_attributes_list_header *attr_list = list;
116
117 return attr_list->type == list_type;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200118 } else {
119 struct osdv2_attributes_list_header *attr_list = list;
120
121 return attr_list->type == list_type;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200122 }
123}
124
Boaz Harrosh3e086132009-01-25 17:05:07 +0200125/* This is for List-objects not Attributes-Lists */
126static void _osd_req_encode_olist(struct osd_request *or,
127 struct osd_obj_id_list *list)
128{
129 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
130
Boaz Harroshc6572c92009-01-25 17:09:40 +0200131 if (osd_req_is_ver1(or)) {
132 cdbh->v1.list_identifier = list->list_identifier;
133 cdbh->v1.start_address = list->continuation_id;
134 } else {
135 cdbh->v2.list_identifier = list->list_identifier;
136 cdbh->v2.start_address = list->continuation_id;
137 }
Boaz Harrosh3e086132009-01-25 17:05:07 +0200138}
139
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200140static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
141 u64 offset, unsigned *padding)
142{
143 return __osd_encode_offset(offset, padding,
Boaz Harroshc6572c92009-01-25 17:09:40 +0200144 osd_req_is_ver1(or) ?
145 OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
146 OSD_OFFSET_MAX_SHIFT);
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200147}
148
Boaz Harrosh345c4352009-01-25 17:03:07 +0200149static struct osd_security_parameters *
150_osd_req_sec_params(struct osd_request *or)
151{
152 struct osd_cdb *ocdb = &or->cdb;
153
Boaz Harroshc6572c92009-01-25 17:09:40 +0200154 if (osd_req_is_ver1(or))
155 return &ocdb->v1.sec_params;
156 else
157 return &ocdb->v2.sec_params;
Boaz Harrosh345c4352009-01-25 17:03:07 +0200158}
159
Boaz Harrosh02941a52009-01-25 16:55:30 +0200160void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
161{
162 memset(osdd, 0, sizeof(*osdd));
163 osdd->scsi_device = scsi_device;
164 osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
Boaz Harroshc6572c92009-01-25 17:09:40 +0200165#ifdef OSD_VER1_SUPPORT
166 osdd->version = OSD_VER2;
167#endif
Boaz Harrosh02941a52009-01-25 16:55:30 +0200168 /* TODO: Allocate pools for osd_request attributes ... */
169}
170EXPORT_SYMBOL(osd_dev_init);
171
172void osd_dev_fini(struct osd_dev *osdd)
173{
174 /* TODO: De-allocate pools */
175
176 osdd->scsi_device = NULL;
177}
178EXPORT_SYMBOL(osd_dev_fini);
179
180static struct osd_request *_osd_request_alloc(gfp_t gfp)
181{
182 struct osd_request *or;
183
184 /* TODO: Use mempool with one saved request */
185 or = kzalloc(sizeof(*or), gfp);
186 return or;
187}
188
189static void _osd_request_free(struct osd_request *or)
190{
191 kfree(or);
192}
193
194struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
195{
196 struct osd_request *or;
197
198 or = _osd_request_alloc(gfp);
199 if (!or)
200 return NULL;
201
202 or->osd_dev = dev;
203 or->alloc_flags = gfp;
204 or->timeout = dev->def_timeout;
205 or->retries = OSD_REQ_RETRIES;
206
207 return or;
208}
209EXPORT_SYMBOL(osd_start_request);
210
211/*
212 * If osd_finalize_request() was called but the request was not executed through
213 * the block layer, then we must release BIOs.
214 */
215static void _abort_unexecuted_bios(struct request *rq)
216{
217 struct bio *bio;
218
219 while ((bio = rq->bio) != NULL) {
220 rq->bio = bio->bi_next;
221 bio_endio(bio, 0);
222 }
223}
224
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200225static void _osd_free_seg(struct osd_request *or __unused,
226 struct _osd_req_data_segment *seg)
227{
228 if (!seg->buff || !seg->alloc_size)
229 return;
230
231 kfree(seg->buff);
232 seg->buff = NULL;
233 seg->alloc_size = 0;
234}
235
Boaz Harrosh02941a52009-01-25 16:55:30 +0200236void osd_end_request(struct osd_request *or)
237{
238 struct request *rq = or->request;
239
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200240 _osd_free_seg(or, &or->set_attr);
241 _osd_free_seg(or, &or->enc_get_attr);
242 _osd_free_seg(or, &or->get_attr);
243
Boaz Harrosh02941a52009-01-25 16:55:30 +0200244 if (rq) {
245 if (rq->next_rq) {
246 _abort_unexecuted_bios(rq->next_rq);
247 blk_put_request(rq->next_rq);
248 }
249
250 _abort_unexecuted_bios(rq);
251 blk_put_request(rq);
252 }
253 _osd_request_free(or);
254}
255EXPORT_SYMBOL(osd_end_request);
256
257int osd_execute_request(struct osd_request *or)
258{
259 return blk_execute_rq(or->request->q, NULL, or->request, 0);
260}
261EXPORT_SYMBOL(osd_execute_request);
262
263static void osd_request_async_done(struct request *req, int error)
264{
265 struct osd_request *or = req->end_io_data;
266
267 or->async_error = error;
268
269 if (error)
270 OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
271
272 if (or->async_done)
273 or->async_done(or, or->async_private);
274 else
275 osd_end_request(or);
276}
277
278int osd_execute_request_async(struct osd_request *or,
279 osd_req_done_fn *done, void *private)
280{
281 or->request->end_io_data = or;
282 or->async_private = private;
283 or->async_done = done;
284
285 blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
286 osd_request_async_done);
287 return 0;
288}
289EXPORT_SYMBOL(osd_execute_request_async);
290
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200291u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
292u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
293
294static int _osd_realloc_seg(struct osd_request *or,
295 struct _osd_req_data_segment *seg, unsigned max_bytes)
296{
297 void *buff;
298
299 if (seg->alloc_size >= max_bytes)
300 return 0;
301
302 buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
303 if (!buff) {
304 OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
305 seg->alloc_size);
306 return -ENOMEM;
307 }
308
309 memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
310 seg->buff = buff;
311 seg->alloc_size = max_bytes;
312 return 0;
313}
314
315static int _alloc_set_attr_list(struct osd_request *or,
316 const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
317{
318 unsigned total_bytes = add_bytes;
319
320 for (; nelem; --nelem, ++oa)
321 total_bytes += _osd_req_alist_elem_size(or, oa->len);
322
323 OSD_DEBUG("total_bytes=%d\n", total_bytes);
324 return _osd_realloc_seg(or, &or->set_attr, total_bytes);
325}
326
327static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
328{
329 OSD_DEBUG("total_bytes=%d\n", max_bytes);
330 return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
331}
332
333static int _alloc_get_attr_list(struct osd_request *or)
334{
335 OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
336 return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
337}
338
Boaz Harrosh02941a52009-01-25 16:55:30 +0200339/*
340 * Common to all OSD commands
341 */
342
343static void _osdv1_req_encode_common(struct osd_request *or,
344 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
345{
346 struct osdv1_cdb *ocdb = &or->cdb.v1;
347
348 /*
349 * For speed, the commands
350 * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
351 * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
352 * are not supported here. Should pass zero and set after the call
353 */
354 act &= cpu_to_be16(~0x0080); /* V1 action code */
355
356 OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
357
358 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
359 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
360 ocdb->h.varlen_cdb.service_action = act;
361
362 ocdb->h.partition = cpu_to_be64(obj->partition);
363 ocdb->h.object = cpu_to_be64(obj->id);
364 ocdb->h.v1.length = cpu_to_be64(len);
365 ocdb->h.v1.start_address = cpu_to_be64(offset);
366}
367
Boaz Harroshc6572c92009-01-25 17:09:40 +0200368static void _osdv2_req_encode_common(struct osd_request *or,
369 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
370{
371 struct osdv2_cdb *ocdb = &or->cdb.v2;
372
373 OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
374
375 ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
376 ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
377 ocdb->h.varlen_cdb.service_action = act;
378
379 ocdb->h.partition = cpu_to_be64(obj->partition);
380 ocdb->h.object = cpu_to_be64(obj->id);
381 ocdb->h.v2.length = cpu_to_be64(len);
382 ocdb->h.v2.start_address = cpu_to_be64(offset);
383}
384
Boaz Harrosh02941a52009-01-25 16:55:30 +0200385static void _osd_req_encode_common(struct osd_request *or,
386 __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
387{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200388 if (osd_req_is_ver1(or))
389 _osdv1_req_encode_common(or, act, obj, offset, len);
390 else
391 _osdv2_req_encode_common(or, act, obj, offset, len);
Boaz Harrosh02941a52009-01-25 16:55:30 +0200392}
393
394/*
395 * Device commands
396 */
Boaz Harroshae30c992009-01-25 17:07:14 +0200397/*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
398/*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
399
Boaz Harrosh02941a52009-01-25 16:55:30 +0200400void osd_req_format(struct osd_request *or, u64 tot_capacity)
401{
402 _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
403 tot_capacity);
404}
405EXPORT_SYMBOL(osd_req_format);
406
Boaz Harrosh3e086132009-01-25 17:05:07 +0200407int osd_req_list_dev_partitions(struct osd_request *or,
408 osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
409{
410 return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
411}
412EXPORT_SYMBOL(osd_req_list_dev_partitions);
413
414static void _osd_req_encode_flush(struct osd_request *or,
415 enum osd_options_flush_scope_values op)
416{
417 struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
418
419 ocdb->command_specific_options = op;
420}
421
422void osd_req_flush_obsd(struct osd_request *or,
423 enum osd_options_flush_scope_values op)
424{
425 _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
426 _osd_req_encode_flush(or, op);
427}
428EXPORT_SYMBOL(osd_req_flush_obsd);
429
Boaz Harroshae30c992009-01-25 17:07:14 +0200430/*TODO: void osd_req_perform_scsi_command(struct osd_request *,
431 const u8 *cdb, ...); */
432/*TODO: void osd_req_task_management(struct osd_request *, ...); */
433
Boaz Harrosh02941a52009-01-25 16:55:30 +0200434/*
435 * Partition commands
436 */
437static void _osd_req_encode_partition(struct osd_request *or,
438 __be16 act, osd_id partition)
439{
440 struct osd_obj_id par = {
441 .partition = partition,
442 .id = 0,
443 };
444
445 _osd_req_encode_common(or, act, &par, 0, 0);
446}
447
448void osd_req_create_partition(struct osd_request *or, osd_id partition)
449{
450 _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
451}
452EXPORT_SYMBOL(osd_req_create_partition);
453
454void osd_req_remove_partition(struct osd_request *or, osd_id partition)
455{
456 _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
457}
458EXPORT_SYMBOL(osd_req_remove_partition);
459
Boaz Harroshae30c992009-01-25 17:07:14 +0200460/*TODO: void osd_req_set_partition_key(struct osd_request *,
461 osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
462 u8 seed[OSD_CRYPTO_SEED_SIZE]); */
463
Boaz Harrosh3e086132009-01-25 17:05:07 +0200464static int _osd_req_list_objects(struct osd_request *or,
465 __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
466 struct osd_obj_id_list *list, unsigned nelem)
467{
468 struct request_queue *q = or->osd_dev->scsi_device->request_queue;
469 u64 len = nelem * sizeof(osd_id) + sizeof(*list);
470 struct bio *bio;
471
472 _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
473
474 if (list->list_identifier)
475 _osd_req_encode_olist(or, list);
476
477 WARN_ON(or->in.bio);
478 bio = bio_map_kern(q, list, len, or->alloc_flags);
479 if (!bio) {
480 OSD_ERR("!!! Failed to allocate list_objects BIO\n");
481 return -ENOMEM;
482 }
483
484 bio->bi_rw &= ~(1 << BIO_RW);
485 or->in.bio = bio;
486 or->in.total_bytes = bio->bi_size;
487 return 0;
488}
489
490int osd_req_list_partition_collections(struct osd_request *or,
491 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
492 unsigned nelem)
493{
494 struct osd_obj_id par = {
495 .partition = partition,
496 .id = 0,
497 };
498
499 return osd_req_list_collection_objects(or, &par, initial_id, list,
500 nelem);
501}
502EXPORT_SYMBOL(osd_req_list_partition_collections);
503
504int osd_req_list_partition_objects(struct osd_request *or,
505 osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
506 unsigned nelem)
507{
508 struct osd_obj_id par = {
509 .partition = partition,
510 .id = 0,
511 };
512
513 return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
514 nelem);
515}
516EXPORT_SYMBOL(osd_req_list_partition_objects);
517
518void osd_req_flush_partition(struct osd_request *or,
519 osd_id partition, enum osd_options_flush_scope_values op)
520{
521 _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
522 _osd_req_encode_flush(or, op);
523}
524EXPORT_SYMBOL(osd_req_flush_partition);
525
526/*
527 * Collection commands
528 */
Boaz Harroshae30c992009-01-25 17:07:14 +0200529/*TODO: void osd_req_create_collection(struct osd_request *,
530 const struct osd_obj_id *); */
531/*TODO: void osd_req_remove_collection(struct osd_request *,
532 const struct osd_obj_id *); */
533
Boaz Harrosh3e086132009-01-25 17:05:07 +0200534int osd_req_list_collection_objects(struct osd_request *or,
535 const struct osd_obj_id *obj, osd_id initial_id,
536 struct osd_obj_id_list *list, unsigned nelem)
537{
538 return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
539 initial_id, list, nelem);
540}
541EXPORT_SYMBOL(osd_req_list_collection_objects);
542
Boaz Harroshae30c992009-01-25 17:07:14 +0200543/*TODO: void query(struct osd_request *, ...); V2 */
544
Boaz Harrosh3e086132009-01-25 17:05:07 +0200545void osd_req_flush_collection(struct osd_request *or,
546 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
547{
548 _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
549 _osd_req_encode_flush(or, op);
550}
551EXPORT_SYMBOL(osd_req_flush_collection);
552
Boaz Harroshae30c992009-01-25 17:07:14 +0200553/*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
554/*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
555
Boaz Harrosh02941a52009-01-25 16:55:30 +0200556/*
557 * Object commands
558 */
559void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
560{
561 _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
562}
563EXPORT_SYMBOL(osd_req_create_object);
564
565void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
566{
567 _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
568}
569EXPORT_SYMBOL(osd_req_remove_object);
570
Boaz Harroshae30c992009-01-25 17:07:14 +0200571
572/*TODO: void osd_req_create_multi(struct osd_request *or,
573 struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
574*/
575
Boaz Harrosh02941a52009-01-25 16:55:30 +0200576void osd_req_write(struct osd_request *or,
577 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
578{
579 _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
580 WARN_ON(or->out.bio || or->out.total_bytes);
581 bio->bi_rw |= (1 << BIO_RW);
582 or->out.bio = bio;
583 or->out.total_bytes = bio->bi_size;
584}
585EXPORT_SYMBOL(osd_req_write);
586
Boaz Harroshae30c992009-01-25 17:07:14 +0200587/*TODO: void osd_req_append(struct osd_request *,
588 const struct osd_obj_id *, struct bio *data_out); */
589/*TODO: void osd_req_create_write(struct osd_request *,
590 const struct osd_obj_id *, struct bio *data_out, u64 offset); */
591/*TODO: void osd_req_clear(struct osd_request *,
592 const struct osd_obj_id *, u64 offset, u64 len); */
593/*TODO: void osd_req_punch(struct osd_request *,
594 const struct osd_obj_id *, u64 offset, u64 len); V2 */
595
Boaz Harrosh3e086132009-01-25 17:05:07 +0200596void osd_req_flush_object(struct osd_request *or,
597 const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
598 /*V2*/ u64 offset, /*V2*/ u64 len)
599{
Boaz Harroshc6572c92009-01-25 17:09:40 +0200600 if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
601 OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
602 offset = 0;
603 len = 0;
604 }
605
Boaz Harrosh3e086132009-01-25 17:05:07 +0200606 _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
607 _osd_req_encode_flush(or, op);
608}
609EXPORT_SYMBOL(osd_req_flush_object);
610
Boaz Harrosh02941a52009-01-25 16:55:30 +0200611void osd_req_read(struct osd_request *or,
612 const struct osd_obj_id *obj, struct bio *bio, u64 offset)
613{
614 _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
615 WARN_ON(or->in.bio || or->in.total_bytes);
616 bio->bi_rw &= ~(1 << BIO_RW);
617 or->in.bio = bio;
618 or->in.total_bytes = bio->bi_size;
619}
620EXPORT_SYMBOL(osd_req_read);
621
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +0200622void osd_req_get_attributes(struct osd_request *or,
623 const struct osd_obj_id *obj)
624{
625 _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
626}
627EXPORT_SYMBOL(osd_req_get_attributes);
628
629void osd_req_set_attributes(struct osd_request *or,
630 const struct osd_obj_id *obj)
631{
632 _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
633}
634EXPORT_SYMBOL(osd_req_set_attributes);
635
636/*
637 * Attributes List-mode
638 */
639
640int osd_req_add_set_attr_list(struct osd_request *or,
641 const struct osd_attr *oa, unsigned nelem)
642{
643 unsigned total_bytes = or->set_attr.total_bytes;
644 void *attr_last;
645 int ret;
646
647 if (or->attributes_mode &&
648 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
649 WARN_ON(1);
650 return -EINVAL;
651 }
652 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
653
654 if (!total_bytes) { /* first-time: allocate and put list header */
655 total_bytes = _osd_req_sizeof_alist_header(or);
656 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
657 if (ret)
658 return ret;
659 _osd_req_set_alist_type(or, or->set_attr.buff,
660 OSD_ATTR_LIST_SET_RETRIEVE);
661 }
662 attr_last = or->set_attr.buff + total_bytes;
663
664 for (; nelem; --nelem) {
665 struct osd_attributes_list_element *attr;
666 unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
667
668 total_bytes += elem_size;
669 if (unlikely(or->set_attr.alloc_size < total_bytes)) {
670 or->set_attr.total_bytes = total_bytes - elem_size;
671 ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
672 if (ret)
673 return ret;
674 attr_last =
675 or->set_attr.buff + or->set_attr.total_bytes;
676 }
677
678 attr = attr_last;
679 attr->attr_page = cpu_to_be32(oa->attr_page);
680 attr->attr_id = cpu_to_be32(oa->attr_id);
681 attr->attr_bytes = cpu_to_be16(oa->len);
682 memcpy(attr->attr_val, oa->val_ptr, oa->len);
683
684 attr_last += elem_size;
685 ++oa;
686 }
687
688 or->set_attr.total_bytes = total_bytes;
689 return 0;
690}
691EXPORT_SYMBOL(osd_req_add_set_attr_list);
692
693static int _append_map_kern(struct request *req,
694 void *buff, unsigned len, gfp_t flags)
695{
696 struct bio *bio;
697 int ret;
698
699 bio = bio_map_kern(req->q, buff, len, flags);
700 if (IS_ERR(bio)) {
701 OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
702 PTR_ERR(bio));
703 return PTR_ERR(bio);
704 }
705 ret = blk_rq_append_bio(req->q, req, bio);
706 if (ret) {
707 OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
708 bio_put(bio);
709 }
710 return ret;
711}
712
713static int _req_append_segment(struct osd_request *or,
714 unsigned padding, struct _osd_req_data_segment *seg,
715 struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
716{
717 void *pad_buff;
718 int ret;
719
720 if (padding) {
721 /* check if we can just add it to last buffer */
722 if (last_seg &&
723 (padding <= last_seg->alloc_size - last_seg->total_bytes))
724 pad_buff = last_seg->buff + last_seg->total_bytes;
725 else
726 pad_buff = io->pad_buff;
727
728 ret = _append_map_kern(io->req, pad_buff, padding,
729 or->alloc_flags);
730 if (ret)
731 return ret;
732 io->total_bytes += padding;
733 }
734
735 ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
736 or->alloc_flags);
737 if (ret)
738 return ret;
739
740 io->total_bytes += seg->total_bytes;
741 OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
742 seg->total_bytes);
743 return 0;
744}
745
746static int _osd_req_finalize_set_attr_list(struct osd_request *or)
747{
748 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
749 unsigned padding;
750 int ret;
751
752 if (!or->set_attr.total_bytes) {
753 cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
754 return 0;
755 }
756
757 cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
758 cdbh->attrs_list.set_attr_offset =
759 osd_req_encode_offset(or, or->out.total_bytes, &padding);
760
761 ret = _req_append_segment(or, padding, &or->set_attr,
762 or->out.last_seg, &or->out);
763 if (ret)
764 return ret;
765
766 or->out.last_seg = &or->set_attr;
767 return 0;
768}
769
770int osd_req_add_get_attr_list(struct osd_request *or,
771 const struct osd_attr *oa, unsigned nelem)
772{
773 unsigned total_bytes = or->enc_get_attr.total_bytes;
774 void *attr_last;
775 int ret;
776
777 if (or->attributes_mode &&
778 or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
779 WARN_ON(1);
780 return -EINVAL;
781 }
782 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
783
784 /* first time calc data-in list header size */
785 if (!or->get_attr.total_bytes)
786 or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
787
788 /* calc data-out info */
789 if (!total_bytes) { /* first-time: allocate and put list header */
790 unsigned max_bytes;
791
792 total_bytes = _osd_req_sizeof_alist_header(or);
793 max_bytes = total_bytes +
794 nelem * sizeof(struct osd_attributes_list_attrid);
795 ret = _alloc_get_attr_desc(or, max_bytes);
796 if (ret)
797 return ret;
798
799 _osd_req_set_alist_type(or, or->enc_get_attr.buff,
800 OSD_ATTR_LIST_GET);
801 }
802 attr_last = or->enc_get_attr.buff + total_bytes;
803
804 for (; nelem; --nelem) {
805 struct osd_attributes_list_attrid *attrid;
806 const unsigned cur_size = sizeof(*attrid);
807
808 total_bytes += cur_size;
809 if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
810 or->enc_get_attr.total_bytes = total_bytes - cur_size;
811 ret = _alloc_get_attr_desc(or,
812 total_bytes + nelem * sizeof(*attrid));
813 if (ret)
814 return ret;
815 attr_last = or->enc_get_attr.buff +
816 or->enc_get_attr.total_bytes;
817 }
818
819 attrid = attr_last;
820 attrid->attr_page = cpu_to_be32(oa->attr_page);
821 attrid->attr_id = cpu_to_be32(oa->attr_id);
822
823 attr_last += cur_size;
824
825 /* calc data-in size */
826 or->get_attr.total_bytes +=
827 _osd_req_alist_elem_size(or, oa->len);
828 ++oa;
829 }
830
831 or->enc_get_attr.total_bytes = total_bytes;
832
833 OSD_DEBUG(
834 "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
835 or->get_attr.total_bytes,
836 or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
837 or->enc_get_attr.total_bytes,
838 (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
839 / sizeof(struct osd_attributes_list_attrid));
840
841 return 0;
842}
843EXPORT_SYMBOL(osd_req_add_get_attr_list);
844
845static int _osd_req_finalize_get_attr_list(struct osd_request *or)
846{
847 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
848 unsigned out_padding;
849 unsigned in_padding;
850 int ret;
851
852 if (!or->enc_get_attr.total_bytes) {
853 cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
854 cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
855 return 0;
856 }
857
858 ret = _alloc_get_attr_list(or);
859 if (ret)
860 return ret;
861
862 /* The out-going buffer info update */
863 OSD_DEBUG("out-going\n");
864 cdbh->attrs_list.get_attr_desc_bytes =
865 cpu_to_be32(or->enc_get_attr.total_bytes);
866
867 cdbh->attrs_list.get_attr_desc_offset =
868 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
869
870 ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
871 or->out.last_seg, &or->out);
872 if (ret)
873 return ret;
874 or->out.last_seg = &or->enc_get_attr;
875
876 /* The incoming buffer info update */
877 OSD_DEBUG("in-coming\n");
878 cdbh->attrs_list.get_attr_alloc_length =
879 cpu_to_be32(or->get_attr.total_bytes);
880
881 cdbh->attrs_list.get_attr_offset =
882 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
883
884 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
885 &or->in);
886 if (ret)
887 return ret;
888 or->in.last_seg = &or->get_attr;
889
890 return 0;
891}
892
893int osd_req_decode_get_attr_list(struct osd_request *or,
894 struct osd_attr *oa, int *nelem, void **iterator)
895{
896 unsigned cur_bytes, returned_bytes;
897 int n;
898 const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
899 void *cur_p;
900
901 if (!_osd_req_is_alist_type(or, or->get_attr.buff,
902 OSD_ATTR_LIST_SET_RETRIEVE)) {
903 oa->attr_page = 0;
904 oa->attr_id = 0;
905 oa->val_ptr = NULL;
906 oa->len = 0;
907 *iterator = NULL;
908 return 0;
909 }
910
911 if (*iterator) {
912 BUG_ON((*iterator < or->get_attr.buff) ||
913 (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
914 cur_p = *iterator;
915 cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
916 returned_bytes = or->get_attr.total_bytes;
917 } else { /* first time decode the list header */
918 cur_bytes = sizeof_attr_list;
919 returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
920 sizeof_attr_list;
921
922 cur_p = or->get_attr.buff + sizeof_attr_list;
923
924 if (returned_bytes > or->get_attr.alloc_size) {
925 OSD_DEBUG("target report: space was not big enough! "
926 "Allocate=%u Needed=%u\n",
927 or->get_attr.alloc_size,
928 returned_bytes + sizeof_attr_list);
929
930 returned_bytes =
931 or->get_attr.alloc_size - sizeof_attr_list;
932 }
933 or->get_attr.total_bytes = returned_bytes;
934 }
935
936 for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
937 struct osd_attributes_list_element *attr = cur_p;
938 unsigned inc;
939
940 oa->len = be16_to_cpu(attr->attr_bytes);
941 inc = _osd_req_alist_elem_size(or, oa->len);
942 OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
943 oa->len, inc, cur_bytes);
944 cur_bytes += inc;
945 if (cur_bytes > returned_bytes) {
946 OSD_ERR("BAD FOOD from target. list not valid!"
947 "c=%d r=%d n=%d\n",
948 cur_bytes, returned_bytes, n);
949 oa->val_ptr = NULL;
950 break;
951 }
952
953 oa->attr_page = be32_to_cpu(attr->attr_page);
954 oa->attr_id = be32_to_cpu(attr->attr_id);
955 oa->val_ptr = attr->attr_val;
956
957 cur_p += inc;
958 ++oa;
959 }
960
961 *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
962 *nelem = n;
963 return returned_bytes - cur_bytes;
964}
965EXPORT_SYMBOL(osd_req_decode_get_attr_list);
966
967/*
968 * Attributes Page-mode
969 */
970
971int osd_req_add_get_attr_page(struct osd_request *or,
972 u32 page_id, void *attar_page, unsigned max_page_len,
973 const struct osd_attr *set_one_attr)
974{
975 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
976
977 if (or->attributes_mode &&
978 or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
979 WARN_ON(1);
980 return -EINVAL;
981 }
982 or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
983
984 or->get_attr.buff = attar_page;
985 or->get_attr.total_bytes = max_page_len;
986
987 or->set_attr.buff = set_one_attr->val_ptr;
988 or->set_attr.total_bytes = set_one_attr->len;
989
990 cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
991 cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
992 /* ocdb->attrs_page.get_attr_offset; */
993
994 cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
995 cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
996 cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
997 /* ocdb->attrs_page.set_attr_offset; */
998 return 0;
999}
1000EXPORT_SYMBOL(osd_req_add_get_attr_page);
1001
1002static int _osd_req_finalize_attr_page(struct osd_request *or)
1003{
1004 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1005 unsigned in_padding, out_padding;
1006 int ret;
1007
1008 /* returned page */
1009 cdbh->attrs_page.get_attr_offset =
1010 osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
1011
1012 ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
1013 &or->in);
1014 if (ret)
1015 return ret;
1016
1017 /* set one value */
1018 cdbh->attrs_page.set_attr_offset =
1019 osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
1020
1021 ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
1022 &or->out);
1023 return ret;
1024}
1025
Boaz Harrosh345c4352009-01-25 17:03:07 +02001026static int _osd_req_finalize_data_integrity(struct osd_request *or,
1027 bool has_in, bool has_out, const u8 *cap_key)
1028{
1029 struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
1030 int ret;
1031
1032 if (!osd_is_sec_alldata(sec_parms))
1033 return 0;
1034
1035 if (has_out) {
1036 struct _osd_req_data_segment seg = {
1037 .buff = &or->out_data_integ,
1038 .total_bytes = sizeof(or->out_data_integ),
1039 };
1040 unsigned pad;
1041
1042 or->out_data_integ.data_bytes = cpu_to_be64(
1043 or->out.bio ? or->out.bio->bi_size : 0);
1044 or->out_data_integ.set_attributes_bytes = cpu_to_be64(
1045 or->set_attr.total_bytes);
1046 or->out_data_integ.get_attributes_bytes = cpu_to_be64(
1047 or->enc_get_attr.total_bytes);
1048
1049 sec_parms->data_out_integrity_check_offset =
1050 osd_req_encode_offset(or, or->out.total_bytes, &pad);
1051
1052 ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
1053 &or->out);
1054 if (ret)
1055 return ret;
1056 or->out.last_seg = NULL;
1057
1058 /* they are now all chained to request sign them all together */
1059 osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
1060 cap_key);
1061 }
1062
1063 if (has_in) {
1064 struct _osd_req_data_segment seg = {
1065 .buff = &or->in_data_integ,
1066 .total_bytes = sizeof(or->in_data_integ),
1067 };
1068 unsigned pad;
1069
1070 sec_parms->data_in_integrity_check_offset =
1071 osd_req_encode_offset(or, or->in.total_bytes, &pad);
1072
1073 ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
1074 &or->in);
1075 if (ret)
1076 return ret;
1077
1078 or->in.last_seg = NULL;
1079 }
1080
1081 return 0;
1082}
1083
Boaz Harrosh02941a52009-01-25 16:55:30 +02001084/*
1085 * osd_finalize_request and helpers
1086 */
1087
1088static int _init_blk_request(struct osd_request *or,
1089 bool has_in, bool has_out)
1090{
1091 gfp_t flags = or->alloc_flags;
1092 struct scsi_device *scsi_device = or->osd_dev->scsi_device;
1093 struct request_queue *q = scsi_device->request_queue;
1094 struct request *req;
1095 int ret = -ENOMEM;
1096
1097 req = blk_get_request(q, has_out, flags);
1098 if (!req)
1099 goto out;
1100
1101 or->request = req;
1102 req->cmd_type = REQ_TYPE_BLOCK_PC;
1103 req->timeout = or->timeout;
1104 req->retries = or->retries;
1105 req->sense = or->sense;
1106 req->sense_len = 0;
1107
1108 if (has_out) {
1109 or->out.req = req;
1110 if (has_in) {
1111 /* allocate bidi request */
1112 req = blk_get_request(q, READ, flags);
1113 if (!req) {
1114 OSD_DEBUG("blk_get_request for bidi failed\n");
1115 goto out;
1116 }
1117 req->cmd_type = REQ_TYPE_BLOCK_PC;
1118 or->in.req = or->request->next_rq = req;
1119 }
1120 } else if (has_in)
1121 or->in.req = req;
1122
1123 ret = 0;
1124out:
1125 OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
1126 or, has_in, has_out, ret, or->request);
1127 return ret;
1128}
1129
1130int osd_finalize_request(struct osd_request *or,
1131 u8 options, const void *cap, const u8 *cap_key)
1132{
1133 struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
1134 bool has_in, has_out;
1135 int ret;
1136
1137 if (options & OSD_REQ_FUA)
1138 cdbh->options |= OSD_CDB_FUA;
1139
1140 if (options & OSD_REQ_DPO)
1141 cdbh->options |= OSD_CDB_DPO;
1142
1143 if (options & OSD_REQ_BYPASS_TIMESTAMPS)
1144 cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
1145
1146 osd_set_caps(&or->cdb, cap);
1147
1148 has_in = or->in.bio || or->get_attr.total_bytes;
1149 has_out = or->out.bio || or->set_attr.total_bytes ||
1150 or->enc_get_attr.total_bytes;
1151
1152 ret = _init_blk_request(or, has_in, has_out);
1153 if (ret) {
1154 OSD_DEBUG("_init_blk_request failed\n");
1155 return ret;
1156 }
1157
1158 if (or->out.bio) {
1159 ret = blk_rq_append_bio(or->request->q, or->out.req,
1160 or->out.bio);
1161 if (ret) {
1162 OSD_DEBUG("blk_rq_append_bio out failed\n");
1163 return ret;
1164 }
1165 OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
1166 _LLU(or->out.total_bytes), or->out.req->data_len);
1167 }
1168 if (or->in.bio) {
1169 ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
1170 if (ret) {
1171 OSD_DEBUG("blk_rq_append_bio in failed\n");
1172 return ret;
1173 }
1174 OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
1175 _LLU(or->in.total_bytes), or->in.req->data_len);
1176 }
1177
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001178 or->out.pad_buff = sg_out_pad_buffer;
1179 or->in.pad_buff = sg_in_pad_buffer;
1180
Boaz Harrosh02941a52009-01-25 16:55:30 +02001181 if (!or->attributes_mode)
1182 or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
1183 cdbh->command_specific_options |= or->attributes_mode;
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001184 if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
1185 ret = _osd_req_finalize_attr_page(or);
1186 } else {
1187 /* TODO: I think that for the GET_ATTR command these 2 should
1188 * be reversed to keep them in execution order (for embeded
1189 * targets with low memory footprint)
1190 */
1191 ret = _osd_req_finalize_set_attr_list(or);
1192 if (ret) {
1193 OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
1194 return ret;
1195 }
1196
1197 ret = _osd_req_finalize_get_attr_list(or);
1198 if (ret) {
1199 OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
1200 return ret;
1201 }
1202 }
Boaz Harrosh02941a52009-01-25 16:55:30 +02001203
Boaz Harrosh345c4352009-01-25 17:03:07 +02001204 ret = _osd_req_finalize_data_integrity(or, has_in, has_out, cap_key);
1205 if (ret)
1206 return ret;
1207
1208 osd_sec_sign_cdb(&or->cdb, cap_key);
1209
Boaz Harrosh02941a52009-01-25 16:55:30 +02001210 or->request->cmd = or->cdb.buff;
1211 or->request->cmd_len = _osd_req_cdb_len(or);
1212
1213 return 0;
1214}
1215EXPORT_SYMBOL(osd_finalize_request);
1216
1217/*
1218 * Implementation of osd_sec.h API
1219 * TODO: Move to a separate osd_sec.c file at a later stage.
1220 */
1221
1222enum { OSD_SEC_CAP_V1_ALL_CAPS =
1223 OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
1224 OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
1225 OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
1226 OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
1227};
1228
Boaz Harroshc6572c92009-01-25 17:09:40 +02001229enum { OSD_SEC_CAP_V2_ALL_CAPS =
1230 OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
1231};
1232
Boaz Harrosh02941a52009-01-25 16:55:30 +02001233void osd_sec_init_nosec_doall_caps(void *caps,
1234 const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
1235{
1236 struct osd_capability *cap = caps;
1237 u8 type;
1238 u8 descriptor_type;
1239
1240 if (likely(obj->id)) {
1241 if (unlikely(is_collection)) {
1242 type = OSD_SEC_OBJ_COLLECTION;
1243 descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
1244 OSD_SEC_OBJ_DESC_COL;
1245 } else {
1246 type = OSD_SEC_OBJ_USER;
1247 descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
1248 }
1249 WARN_ON(!obj->partition);
1250 } else {
1251 type = obj->partition ? OSD_SEC_OBJ_PARTITION :
1252 OSD_SEC_OBJ_ROOT;
1253 descriptor_type = OSD_SEC_OBJ_DESC_PAR;
1254 }
1255
1256 memset(cap, 0, sizeof(*cap));
1257
1258 cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
1259 cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
1260 cap->h.security_method = OSD_SEC_NOSEC;
1261/* cap->expiration_time;
1262 cap->AUDIT[30-10];
1263 cap->discriminator[42-30];
1264 cap->object_created_time; */
1265 cap->h.object_type = type;
1266 osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
1267 cap->h.object_descriptor_type = descriptor_type;
1268 cap->od.obj_desc.policy_access_tag = 0;
1269 cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
1270 cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
1271}
1272EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
1273
Boaz Harroshc6572c92009-01-25 17:09:40 +02001274/* FIXME: Extract version from caps pointer.
1275 * Also Pete's target only supports caps from OSDv1 for now
1276 */
Boaz Harrosh02941a52009-01-25 16:55:30 +02001277void osd_set_caps(struct osd_cdb *cdb, const void *caps)
1278{
Boaz Harroshc6572c92009-01-25 17:09:40 +02001279 bool is_ver1 = true;
1280 /* NOTE: They start at same address */
1281 memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
Boaz Harrosh02941a52009-01-25 16:55:30 +02001282}
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001283
Boaz Harrosh345c4352009-01-25 17:03:07 +02001284bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
1285{
1286 return false;
1287}
1288
1289void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
1290{
1291}
1292
1293void osd_sec_sign_data(void *data_integ __unused,
1294 struct bio *bio __unused, const u8 *cap_key __unused)
1295{
1296}
1297
Boaz Harrosh4ef1a3d2009-01-25 16:59:50 +02001298/*
1299 * Declared in osd_protocol.h
1300 * 4.12.5 Data-In and Data-Out buffer offsets
1301 * byte offset = mantissa * (2^(exponent+8))
1302 * Returns the smallest allowed encoded offset that contains given @offset
1303 * The actual encoded offset returned is @offset + *@padding.
1304 */
1305osd_cdb_offset __osd_encode_offset(
1306 u64 offset, unsigned *padding, int min_shift, int max_shift)
1307{
1308 u64 try_offset = -1, mod, align;
1309 osd_cdb_offset be32_offset;
1310 int shift;
1311
1312 *padding = 0;
1313 if (!offset)
1314 return 0;
1315
1316 for (shift = min_shift; shift < max_shift; ++shift) {
1317 try_offset = offset >> shift;
1318 if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
1319 break;
1320 }
1321
1322 BUG_ON(shift == max_shift);
1323
1324 align = 1 << shift;
1325 mod = offset & (align - 1);
1326 if (mod) {
1327 *padding = align - mod;
1328 try_offset += 1;
1329 }
1330
1331 try_offset |= ((shift - 8) & 0xf) << 28;
1332 be32_offset = cpu_to_be32((u32)try_offset);
1333
1334 OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
1335 _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
1336 be32_offset, *padding);
1337 return be32_offset;
1338}