osd_initiator.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /*
  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. #include <scsi/osd_initiator.h>
  42. #include <scsi/osd_sec.h>
  43. #include <scsi/scsi_device.h>
  44. #include "osd_debug.h"
  45. #ifndef __unused
  46. # define __unused __attribute__((unused))
  47. #endif
  48. enum { OSD_REQ_RETRIES = 1 };
  49. MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
  50. MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
  51. MODULE_LICENSE("GPL");
  52. static inline void build_test(void)
  53. {
  54. /* structures were not packed */
  55. BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
  56. BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
  57. }
  58. static unsigned _osd_req_cdb_len(struct osd_request *or)
  59. {
  60. return OSDv1_TOTAL_CDB_LEN;
  61. }
  62. static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
  63. {
  64. return osdv1_attr_list_elem_size(len);
  65. }
  66. static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
  67. {
  68. return osdv1_list_size(list_head);
  69. }
  70. static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
  71. {
  72. return sizeof(struct osdv1_attributes_list_header);
  73. }
  74. static void _osd_req_set_alist_type(struct osd_request *or,
  75. void *list, int list_type)
  76. {
  77. struct osdv1_attributes_list_header *attr_list = list;
  78. memset(attr_list, 0, sizeof(*attr_list));
  79. attr_list->type = list_type;
  80. }
  81. static bool _osd_req_is_alist_type(struct osd_request *or,
  82. void *list, int list_type)
  83. {
  84. if (!list)
  85. return false;
  86. if (1) {
  87. struct osdv1_attributes_list_header *attr_list = list;
  88. return attr_list->type == list_type;
  89. }
  90. }
  91. static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
  92. u64 offset, unsigned *padding)
  93. {
  94. return __osd_encode_offset(offset, padding,
  95. OSDv1_OFFSET_MIN_SHIFT, OSD_OFFSET_MAX_SHIFT);
  96. }
  97. void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
  98. {
  99. memset(osdd, 0, sizeof(*osdd));
  100. osdd->scsi_device = scsi_device;
  101. osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
  102. /* TODO: Allocate pools for osd_request attributes ... */
  103. }
  104. EXPORT_SYMBOL(osd_dev_init);
  105. void osd_dev_fini(struct osd_dev *osdd)
  106. {
  107. /* TODO: De-allocate pools */
  108. osdd->scsi_device = NULL;
  109. }
  110. EXPORT_SYMBOL(osd_dev_fini);
  111. static struct osd_request *_osd_request_alloc(gfp_t gfp)
  112. {
  113. struct osd_request *or;
  114. /* TODO: Use mempool with one saved request */
  115. or = kzalloc(sizeof(*or), gfp);
  116. return or;
  117. }
  118. static void _osd_request_free(struct osd_request *or)
  119. {
  120. kfree(or);
  121. }
  122. struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
  123. {
  124. struct osd_request *or;
  125. or = _osd_request_alloc(gfp);
  126. if (!or)
  127. return NULL;
  128. or->osd_dev = dev;
  129. or->alloc_flags = gfp;
  130. or->timeout = dev->def_timeout;
  131. or->retries = OSD_REQ_RETRIES;
  132. return or;
  133. }
  134. EXPORT_SYMBOL(osd_start_request);
  135. /*
  136. * If osd_finalize_request() was called but the request was not executed through
  137. * the block layer, then we must release BIOs.
  138. */
  139. static void _abort_unexecuted_bios(struct request *rq)
  140. {
  141. struct bio *bio;
  142. while ((bio = rq->bio) != NULL) {
  143. rq->bio = bio->bi_next;
  144. bio_endio(bio, 0);
  145. }
  146. }
  147. static void _osd_free_seg(struct osd_request *or __unused,
  148. struct _osd_req_data_segment *seg)
  149. {
  150. if (!seg->buff || !seg->alloc_size)
  151. return;
  152. kfree(seg->buff);
  153. seg->buff = NULL;
  154. seg->alloc_size = 0;
  155. }
  156. void osd_end_request(struct osd_request *or)
  157. {
  158. struct request *rq = or->request;
  159. _osd_free_seg(or, &or->set_attr);
  160. _osd_free_seg(or, &or->enc_get_attr);
  161. _osd_free_seg(or, &or->get_attr);
  162. if (rq) {
  163. if (rq->next_rq) {
  164. _abort_unexecuted_bios(rq->next_rq);
  165. blk_put_request(rq->next_rq);
  166. }
  167. _abort_unexecuted_bios(rq);
  168. blk_put_request(rq);
  169. }
  170. _osd_request_free(or);
  171. }
  172. EXPORT_SYMBOL(osd_end_request);
  173. int osd_execute_request(struct osd_request *or)
  174. {
  175. return blk_execute_rq(or->request->q, NULL, or->request, 0);
  176. }
  177. EXPORT_SYMBOL(osd_execute_request);
  178. static void osd_request_async_done(struct request *req, int error)
  179. {
  180. struct osd_request *or = req->end_io_data;
  181. or->async_error = error;
  182. if (error)
  183. OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
  184. if (or->async_done)
  185. or->async_done(or, or->async_private);
  186. else
  187. osd_end_request(or);
  188. }
  189. int osd_execute_request_async(struct osd_request *or,
  190. osd_req_done_fn *done, void *private)
  191. {
  192. or->request->end_io_data = or;
  193. or->async_private = private;
  194. or->async_done = done;
  195. blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
  196. osd_request_async_done);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL(osd_execute_request_async);
  200. u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  201. u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  202. static int _osd_realloc_seg(struct osd_request *or,
  203. struct _osd_req_data_segment *seg, unsigned max_bytes)
  204. {
  205. void *buff;
  206. if (seg->alloc_size >= max_bytes)
  207. return 0;
  208. buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
  209. if (!buff) {
  210. OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
  211. seg->alloc_size);
  212. return -ENOMEM;
  213. }
  214. memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
  215. seg->buff = buff;
  216. seg->alloc_size = max_bytes;
  217. return 0;
  218. }
  219. static int _alloc_set_attr_list(struct osd_request *or,
  220. const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
  221. {
  222. unsigned total_bytes = add_bytes;
  223. for (; nelem; --nelem, ++oa)
  224. total_bytes += _osd_req_alist_elem_size(or, oa->len);
  225. OSD_DEBUG("total_bytes=%d\n", total_bytes);
  226. return _osd_realloc_seg(or, &or->set_attr, total_bytes);
  227. }
  228. static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
  229. {
  230. OSD_DEBUG("total_bytes=%d\n", max_bytes);
  231. return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
  232. }
  233. static int _alloc_get_attr_list(struct osd_request *or)
  234. {
  235. OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
  236. return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
  237. }
  238. /*
  239. * Common to all OSD commands
  240. */
  241. static void _osdv1_req_encode_common(struct osd_request *or,
  242. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  243. {
  244. struct osdv1_cdb *ocdb = &or->cdb.v1;
  245. /*
  246. * For speed, the commands
  247. * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
  248. * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
  249. * are not supported here. Should pass zero and set after the call
  250. */
  251. act &= cpu_to_be16(~0x0080); /* V1 action code */
  252. OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
  253. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  254. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  255. ocdb->h.varlen_cdb.service_action = act;
  256. ocdb->h.partition = cpu_to_be64(obj->partition);
  257. ocdb->h.object = cpu_to_be64(obj->id);
  258. ocdb->h.v1.length = cpu_to_be64(len);
  259. ocdb->h.v1.start_address = cpu_to_be64(offset);
  260. }
  261. static void _osd_req_encode_common(struct osd_request *or,
  262. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  263. {
  264. _osdv1_req_encode_common(or, act, obj, offset, len);
  265. }
  266. /*
  267. * Device commands
  268. */
  269. void osd_req_format(struct osd_request *or, u64 tot_capacity)
  270. {
  271. _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
  272. tot_capacity);
  273. }
  274. EXPORT_SYMBOL(osd_req_format);
  275. /*
  276. * Partition commands
  277. */
  278. static void _osd_req_encode_partition(struct osd_request *or,
  279. __be16 act, osd_id partition)
  280. {
  281. struct osd_obj_id par = {
  282. .partition = partition,
  283. .id = 0,
  284. };
  285. _osd_req_encode_common(or, act, &par, 0, 0);
  286. }
  287. void osd_req_create_partition(struct osd_request *or, osd_id partition)
  288. {
  289. _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
  290. }
  291. EXPORT_SYMBOL(osd_req_create_partition);
  292. void osd_req_remove_partition(struct osd_request *or, osd_id partition)
  293. {
  294. _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
  295. }
  296. EXPORT_SYMBOL(osd_req_remove_partition);
  297. /*
  298. * Object commands
  299. */
  300. void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
  301. {
  302. _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
  303. }
  304. EXPORT_SYMBOL(osd_req_create_object);
  305. void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
  306. {
  307. _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
  308. }
  309. EXPORT_SYMBOL(osd_req_remove_object);
  310. void osd_req_write(struct osd_request *or,
  311. const struct osd_obj_id *obj, struct bio *bio, u64 offset)
  312. {
  313. _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
  314. WARN_ON(or->out.bio || or->out.total_bytes);
  315. bio->bi_rw |= (1 << BIO_RW);
  316. or->out.bio = bio;
  317. or->out.total_bytes = bio->bi_size;
  318. }
  319. EXPORT_SYMBOL(osd_req_write);
  320. void osd_req_read(struct osd_request *or,
  321. const struct osd_obj_id *obj, struct bio *bio, u64 offset)
  322. {
  323. _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
  324. WARN_ON(or->in.bio || or->in.total_bytes);
  325. bio->bi_rw &= ~(1 << BIO_RW);
  326. or->in.bio = bio;
  327. or->in.total_bytes = bio->bi_size;
  328. }
  329. EXPORT_SYMBOL(osd_req_read);
  330. void osd_req_get_attributes(struct osd_request *or,
  331. const struct osd_obj_id *obj)
  332. {
  333. _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
  334. }
  335. EXPORT_SYMBOL(osd_req_get_attributes);
  336. void osd_req_set_attributes(struct osd_request *or,
  337. const struct osd_obj_id *obj)
  338. {
  339. _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
  340. }
  341. EXPORT_SYMBOL(osd_req_set_attributes);
  342. /*
  343. * Attributes List-mode
  344. */
  345. int osd_req_add_set_attr_list(struct osd_request *or,
  346. const struct osd_attr *oa, unsigned nelem)
  347. {
  348. unsigned total_bytes = or->set_attr.total_bytes;
  349. void *attr_last;
  350. int ret;
  351. if (or->attributes_mode &&
  352. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  353. WARN_ON(1);
  354. return -EINVAL;
  355. }
  356. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  357. if (!total_bytes) { /* first-time: allocate and put list header */
  358. total_bytes = _osd_req_sizeof_alist_header(or);
  359. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  360. if (ret)
  361. return ret;
  362. _osd_req_set_alist_type(or, or->set_attr.buff,
  363. OSD_ATTR_LIST_SET_RETRIEVE);
  364. }
  365. attr_last = or->set_attr.buff + total_bytes;
  366. for (; nelem; --nelem) {
  367. struct osd_attributes_list_element *attr;
  368. unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
  369. total_bytes += elem_size;
  370. if (unlikely(or->set_attr.alloc_size < total_bytes)) {
  371. or->set_attr.total_bytes = total_bytes - elem_size;
  372. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  373. if (ret)
  374. return ret;
  375. attr_last =
  376. or->set_attr.buff + or->set_attr.total_bytes;
  377. }
  378. attr = attr_last;
  379. attr->attr_page = cpu_to_be32(oa->attr_page);
  380. attr->attr_id = cpu_to_be32(oa->attr_id);
  381. attr->attr_bytes = cpu_to_be16(oa->len);
  382. memcpy(attr->attr_val, oa->val_ptr, oa->len);
  383. attr_last += elem_size;
  384. ++oa;
  385. }
  386. or->set_attr.total_bytes = total_bytes;
  387. return 0;
  388. }
  389. EXPORT_SYMBOL(osd_req_add_set_attr_list);
  390. static int _append_map_kern(struct request *req,
  391. void *buff, unsigned len, gfp_t flags)
  392. {
  393. struct bio *bio;
  394. int ret;
  395. bio = bio_map_kern(req->q, buff, len, flags);
  396. if (IS_ERR(bio)) {
  397. OSD_ERR("Failed bio_map_kern(%p, %d) => %ld\n", buff, len,
  398. PTR_ERR(bio));
  399. return PTR_ERR(bio);
  400. }
  401. ret = blk_rq_append_bio(req->q, req, bio);
  402. if (ret) {
  403. OSD_ERR("Failed blk_rq_append_bio(%p) => %d\n", bio, ret);
  404. bio_put(bio);
  405. }
  406. return ret;
  407. }
  408. static int _req_append_segment(struct osd_request *or,
  409. unsigned padding, struct _osd_req_data_segment *seg,
  410. struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
  411. {
  412. void *pad_buff;
  413. int ret;
  414. if (padding) {
  415. /* check if we can just add it to last buffer */
  416. if (last_seg &&
  417. (padding <= last_seg->alloc_size - last_seg->total_bytes))
  418. pad_buff = last_seg->buff + last_seg->total_bytes;
  419. else
  420. pad_buff = io->pad_buff;
  421. ret = _append_map_kern(io->req, pad_buff, padding,
  422. or->alloc_flags);
  423. if (ret)
  424. return ret;
  425. io->total_bytes += padding;
  426. }
  427. ret = _append_map_kern(io->req, seg->buff, seg->total_bytes,
  428. or->alloc_flags);
  429. if (ret)
  430. return ret;
  431. io->total_bytes += seg->total_bytes;
  432. OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
  433. seg->total_bytes);
  434. return 0;
  435. }
  436. static int _osd_req_finalize_set_attr_list(struct osd_request *or)
  437. {
  438. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  439. unsigned padding;
  440. int ret;
  441. if (!or->set_attr.total_bytes) {
  442. cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
  443. return 0;
  444. }
  445. cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
  446. cdbh->attrs_list.set_attr_offset =
  447. osd_req_encode_offset(or, or->out.total_bytes, &padding);
  448. ret = _req_append_segment(or, padding, &or->set_attr,
  449. or->out.last_seg, &or->out);
  450. if (ret)
  451. return ret;
  452. or->out.last_seg = &or->set_attr;
  453. return 0;
  454. }
  455. int osd_req_add_get_attr_list(struct osd_request *or,
  456. const struct osd_attr *oa, unsigned nelem)
  457. {
  458. unsigned total_bytes = or->enc_get_attr.total_bytes;
  459. void *attr_last;
  460. int ret;
  461. if (or->attributes_mode &&
  462. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  463. WARN_ON(1);
  464. return -EINVAL;
  465. }
  466. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  467. /* first time calc data-in list header size */
  468. if (!or->get_attr.total_bytes)
  469. or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
  470. /* calc data-out info */
  471. if (!total_bytes) { /* first-time: allocate and put list header */
  472. unsigned max_bytes;
  473. total_bytes = _osd_req_sizeof_alist_header(or);
  474. max_bytes = total_bytes +
  475. nelem * sizeof(struct osd_attributes_list_attrid);
  476. ret = _alloc_get_attr_desc(or, max_bytes);
  477. if (ret)
  478. return ret;
  479. _osd_req_set_alist_type(or, or->enc_get_attr.buff,
  480. OSD_ATTR_LIST_GET);
  481. }
  482. attr_last = or->enc_get_attr.buff + total_bytes;
  483. for (; nelem; --nelem) {
  484. struct osd_attributes_list_attrid *attrid;
  485. const unsigned cur_size = sizeof(*attrid);
  486. total_bytes += cur_size;
  487. if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
  488. or->enc_get_attr.total_bytes = total_bytes - cur_size;
  489. ret = _alloc_get_attr_desc(or,
  490. total_bytes + nelem * sizeof(*attrid));
  491. if (ret)
  492. return ret;
  493. attr_last = or->enc_get_attr.buff +
  494. or->enc_get_attr.total_bytes;
  495. }
  496. attrid = attr_last;
  497. attrid->attr_page = cpu_to_be32(oa->attr_page);
  498. attrid->attr_id = cpu_to_be32(oa->attr_id);
  499. attr_last += cur_size;
  500. /* calc data-in size */
  501. or->get_attr.total_bytes +=
  502. _osd_req_alist_elem_size(or, oa->len);
  503. ++oa;
  504. }
  505. or->enc_get_attr.total_bytes = total_bytes;
  506. OSD_DEBUG(
  507. "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
  508. or->get_attr.total_bytes,
  509. or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
  510. or->enc_get_attr.total_bytes,
  511. (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
  512. / sizeof(struct osd_attributes_list_attrid));
  513. return 0;
  514. }
  515. EXPORT_SYMBOL(osd_req_add_get_attr_list);
  516. static int _osd_req_finalize_get_attr_list(struct osd_request *or)
  517. {
  518. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  519. unsigned out_padding;
  520. unsigned in_padding;
  521. int ret;
  522. if (!or->enc_get_attr.total_bytes) {
  523. cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
  524. cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
  525. return 0;
  526. }
  527. ret = _alloc_get_attr_list(or);
  528. if (ret)
  529. return ret;
  530. /* The out-going buffer info update */
  531. OSD_DEBUG("out-going\n");
  532. cdbh->attrs_list.get_attr_desc_bytes =
  533. cpu_to_be32(or->enc_get_attr.total_bytes);
  534. cdbh->attrs_list.get_attr_desc_offset =
  535. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  536. ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
  537. or->out.last_seg, &or->out);
  538. if (ret)
  539. return ret;
  540. or->out.last_seg = &or->enc_get_attr;
  541. /* The incoming buffer info update */
  542. OSD_DEBUG("in-coming\n");
  543. cdbh->attrs_list.get_attr_alloc_length =
  544. cpu_to_be32(or->get_attr.total_bytes);
  545. cdbh->attrs_list.get_attr_offset =
  546. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  547. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  548. &or->in);
  549. if (ret)
  550. return ret;
  551. or->in.last_seg = &or->get_attr;
  552. return 0;
  553. }
  554. int osd_req_decode_get_attr_list(struct osd_request *or,
  555. struct osd_attr *oa, int *nelem, void **iterator)
  556. {
  557. unsigned cur_bytes, returned_bytes;
  558. int n;
  559. const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
  560. void *cur_p;
  561. if (!_osd_req_is_alist_type(or, or->get_attr.buff,
  562. OSD_ATTR_LIST_SET_RETRIEVE)) {
  563. oa->attr_page = 0;
  564. oa->attr_id = 0;
  565. oa->val_ptr = NULL;
  566. oa->len = 0;
  567. *iterator = NULL;
  568. return 0;
  569. }
  570. if (*iterator) {
  571. BUG_ON((*iterator < or->get_attr.buff) ||
  572. (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
  573. cur_p = *iterator;
  574. cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
  575. returned_bytes = or->get_attr.total_bytes;
  576. } else { /* first time decode the list header */
  577. cur_bytes = sizeof_attr_list;
  578. returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
  579. sizeof_attr_list;
  580. cur_p = or->get_attr.buff + sizeof_attr_list;
  581. if (returned_bytes > or->get_attr.alloc_size) {
  582. OSD_DEBUG("target report: space was not big enough! "
  583. "Allocate=%u Needed=%u\n",
  584. or->get_attr.alloc_size,
  585. returned_bytes + sizeof_attr_list);
  586. returned_bytes =
  587. or->get_attr.alloc_size - sizeof_attr_list;
  588. }
  589. or->get_attr.total_bytes = returned_bytes;
  590. }
  591. for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
  592. struct osd_attributes_list_element *attr = cur_p;
  593. unsigned inc;
  594. oa->len = be16_to_cpu(attr->attr_bytes);
  595. inc = _osd_req_alist_elem_size(or, oa->len);
  596. OSD_DEBUG("oa->len=%d inc=%d cur_bytes=%d\n",
  597. oa->len, inc, cur_bytes);
  598. cur_bytes += inc;
  599. if (cur_bytes > returned_bytes) {
  600. OSD_ERR("BAD FOOD from target. list not valid!"
  601. "c=%d r=%d n=%d\n",
  602. cur_bytes, returned_bytes, n);
  603. oa->val_ptr = NULL;
  604. break;
  605. }
  606. oa->attr_page = be32_to_cpu(attr->attr_page);
  607. oa->attr_id = be32_to_cpu(attr->attr_id);
  608. oa->val_ptr = attr->attr_val;
  609. cur_p += inc;
  610. ++oa;
  611. }
  612. *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
  613. *nelem = n;
  614. return returned_bytes - cur_bytes;
  615. }
  616. EXPORT_SYMBOL(osd_req_decode_get_attr_list);
  617. /*
  618. * Attributes Page-mode
  619. */
  620. int osd_req_add_get_attr_page(struct osd_request *or,
  621. u32 page_id, void *attar_page, unsigned max_page_len,
  622. const struct osd_attr *set_one_attr)
  623. {
  624. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  625. if (or->attributes_mode &&
  626. or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  627. WARN_ON(1);
  628. return -EINVAL;
  629. }
  630. or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
  631. or->get_attr.buff = attar_page;
  632. or->get_attr.total_bytes = max_page_len;
  633. or->set_attr.buff = set_one_attr->val_ptr;
  634. or->set_attr.total_bytes = set_one_attr->len;
  635. cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
  636. cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
  637. /* ocdb->attrs_page.get_attr_offset; */
  638. cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
  639. cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
  640. cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
  641. /* ocdb->attrs_page.set_attr_offset; */
  642. return 0;
  643. }
  644. EXPORT_SYMBOL(osd_req_add_get_attr_page);
  645. static int _osd_req_finalize_attr_page(struct osd_request *or)
  646. {
  647. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  648. unsigned in_padding, out_padding;
  649. int ret;
  650. /* returned page */
  651. cdbh->attrs_page.get_attr_offset =
  652. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  653. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  654. &or->in);
  655. if (ret)
  656. return ret;
  657. /* set one value */
  658. cdbh->attrs_page.set_attr_offset =
  659. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  660. ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
  661. &or->out);
  662. return ret;
  663. }
  664. /*
  665. * osd_finalize_request and helpers
  666. */
  667. static int _init_blk_request(struct osd_request *or,
  668. bool has_in, bool has_out)
  669. {
  670. gfp_t flags = or->alloc_flags;
  671. struct scsi_device *scsi_device = or->osd_dev->scsi_device;
  672. struct request_queue *q = scsi_device->request_queue;
  673. struct request *req;
  674. int ret = -ENOMEM;
  675. req = blk_get_request(q, has_out, flags);
  676. if (!req)
  677. goto out;
  678. or->request = req;
  679. req->cmd_type = REQ_TYPE_BLOCK_PC;
  680. req->timeout = or->timeout;
  681. req->retries = or->retries;
  682. req->sense = or->sense;
  683. req->sense_len = 0;
  684. if (has_out) {
  685. or->out.req = req;
  686. if (has_in) {
  687. /* allocate bidi request */
  688. req = blk_get_request(q, READ, flags);
  689. if (!req) {
  690. OSD_DEBUG("blk_get_request for bidi failed\n");
  691. goto out;
  692. }
  693. req->cmd_type = REQ_TYPE_BLOCK_PC;
  694. or->in.req = or->request->next_rq = req;
  695. }
  696. } else if (has_in)
  697. or->in.req = req;
  698. ret = 0;
  699. out:
  700. OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
  701. or, has_in, has_out, ret, or->request);
  702. return ret;
  703. }
  704. int osd_finalize_request(struct osd_request *or,
  705. u8 options, const void *cap, const u8 *cap_key)
  706. {
  707. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  708. bool has_in, has_out;
  709. int ret;
  710. if (options & OSD_REQ_FUA)
  711. cdbh->options |= OSD_CDB_FUA;
  712. if (options & OSD_REQ_DPO)
  713. cdbh->options |= OSD_CDB_DPO;
  714. if (options & OSD_REQ_BYPASS_TIMESTAMPS)
  715. cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
  716. osd_set_caps(&or->cdb, cap);
  717. has_in = or->in.bio || or->get_attr.total_bytes;
  718. has_out = or->out.bio || or->set_attr.total_bytes ||
  719. or->enc_get_attr.total_bytes;
  720. ret = _init_blk_request(or, has_in, has_out);
  721. if (ret) {
  722. OSD_DEBUG("_init_blk_request failed\n");
  723. return ret;
  724. }
  725. if (or->out.bio) {
  726. ret = blk_rq_append_bio(or->request->q, or->out.req,
  727. or->out.bio);
  728. if (ret) {
  729. OSD_DEBUG("blk_rq_append_bio out failed\n");
  730. return ret;
  731. }
  732. OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
  733. _LLU(or->out.total_bytes), or->out.req->data_len);
  734. }
  735. if (or->in.bio) {
  736. ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
  737. if (ret) {
  738. OSD_DEBUG("blk_rq_append_bio in failed\n");
  739. return ret;
  740. }
  741. OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
  742. _LLU(or->in.total_bytes), or->in.req->data_len);
  743. }
  744. or->out.pad_buff = sg_out_pad_buffer;
  745. or->in.pad_buff = sg_in_pad_buffer;
  746. if (!or->attributes_mode)
  747. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  748. cdbh->command_specific_options |= or->attributes_mode;
  749. if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  750. ret = _osd_req_finalize_attr_page(or);
  751. } else {
  752. /* TODO: I think that for the GET_ATTR command these 2 should
  753. * be reversed to keep them in execution order (for embeded
  754. * targets with low memory footprint)
  755. */
  756. ret = _osd_req_finalize_set_attr_list(or);
  757. if (ret) {
  758. OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
  759. return ret;
  760. }
  761. ret = _osd_req_finalize_get_attr_list(or);
  762. if (ret) {
  763. OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
  764. return ret;
  765. }
  766. }
  767. or->request->cmd = or->cdb.buff;
  768. or->request->cmd_len = _osd_req_cdb_len(or);
  769. return 0;
  770. }
  771. EXPORT_SYMBOL(osd_finalize_request);
  772. /*
  773. * Implementation of osd_sec.h API
  774. * TODO: Move to a separate osd_sec.c file at a later stage.
  775. */
  776. enum { OSD_SEC_CAP_V1_ALL_CAPS =
  777. OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
  778. OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
  779. OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
  780. OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
  781. };
  782. void osd_sec_init_nosec_doall_caps(void *caps,
  783. const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
  784. {
  785. struct osd_capability *cap = caps;
  786. u8 type;
  787. u8 descriptor_type;
  788. if (likely(obj->id)) {
  789. if (unlikely(is_collection)) {
  790. type = OSD_SEC_OBJ_COLLECTION;
  791. descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
  792. OSD_SEC_OBJ_DESC_COL;
  793. } else {
  794. type = OSD_SEC_OBJ_USER;
  795. descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
  796. }
  797. WARN_ON(!obj->partition);
  798. } else {
  799. type = obj->partition ? OSD_SEC_OBJ_PARTITION :
  800. OSD_SEC_OBJ_ROOT;
  801. descriptor_type = OSD_SEC_OBJ_DESC_PAR;
  802. }
  803. memset(cap, 0, sizeof(*cap));
  804. cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
  805. cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
  806. cap->h.security_method = OSD_SEC_NOSEC;
  807. /* cap->expiration_time;
  808. cap->AUDIT[30-10];
  809. cap->discriminator[42-30];
  810. cap->object_created_time; */
  811. cap->h.object_type = type;
  812. osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
  813. cap->h.object_descriptor_type = descriptor_type;
  814. cap->od.obj_desc.policy_access_tag = 0;
  815. cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
  816. cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
  817. }
  818. EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
  819. void osd_set_caps(struct osd_cdb *cdb, const void *caps)
  820. {
  821. memcpy(&cdb->v1.caps, caps, OSDv1_CAP_LEN);
  822. }
  823. /*
  824. * Declared in osd_protocol.h
  825. * 4.12.5 Data-In and Data-Out buffer offsets
  826. * byte offset = mantissa * (2^(exponent+8))
  827. * Returns the smallest allowed encoded offset that contains given @offset
  828. * The actual encoded offset returned is @offset + *@padding.
  829. */
  830. osd_cdb_offset __osd_encode_offset(
  831. u64 offset, unsigned *padding, int min_shift, int max_shift)
  832. {
  833. u64 try_offset = -1, mod, align;
  834. osd_cdb_offset be32_offset;
  835. int shift;
  836. *padding = 0;
  837. if (!offset)
  838. return 0;
  839. for (shift = min_shift; shift < max_shift; ++shift) {
  840. try_offset = offset >> shift;
  841. if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
  842. break;
  843. }
  844. BUG_ON(shift == max_shift);
  845. align = 1 << shift;
  846. mod = offset & (align - 1);
  847. if (mod) {
  848. *padding = align - mod;
  849. try_offset += 1;
  850. }
  851. try_offset |= ((shift - 8) & 0xf) << 28;
  852. be32_offset = cpu_to_be32((u32)try_offset);
  853. OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
  854. _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
  855. be32_offset, *padding);
  856. return be32_offset;
  857. }