osd_initiator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. enum { OSD_REQ_RETRIES = 1 };
  46. MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
  47. MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
  48. MODULE_LICENSE("GPL");
  49. static inline void build_test(void)
  50. {
  51. /* structures were not packed */
  52. BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
  53. BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
  54. }
  55. static unsigned _osd_req_cdb_len(struct osd_request *or)
  56. {
  57. return OSDv1_TOTAL_CDB_LEN;
  58. }
  59. void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
  60. {
  61. memset(osdd, 0, sizeof(*osdd));
  62. osdd->scsi_device = scsi_device;
  63. osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
  64. /* TODO: Allocate pools for osd_request attributes ... */
  65. }
  66. EXPORT_SYMBOL(osd_dev_init);
  67. void osd_dev_fini(struct osd_dev *osdd)
  68. {
  69. /* TODO: De-allocate pools */
  70. osdd->scsi_device = NULL;
  71. }
  72. EXPORT_SYMBOL(osd_dev_fini);
  73. static struct osd_request *_osd_request_alloc(gfp_t gfp)
  74. {
  75. struct osd_request *or;
  76. /* TODO: Use mempool with one saved request */
  77. or = kzalloc(sizeof(*or), gfp);
  78. return or;
  79. }
  80. static void _osd_request_free(struct osd_request *or)
  81. {
  82. kfree(or);
  83. }
  84. struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
  85. {
  86. struct osd_request *or;
  87. or = _osd_request_alloc(gfp);
  88. if (!or)
  89. return NULL;
  90. or->osd_dev = dev;
  91. or->alloc_flags = gfp;
  92. or->timeout = dev->def_timeout;
  93. or->retries = OSD_REQ_RETRIES;
  94. return or;
  95. }
  96. EXPORT_SYMBOL(osd_start_request);
  97. /*
  98. * If osd_finalize_request() was called but the request was not executed through
  99. * the block layer, then we must release BIOs.
  100. */
  101. static void _abort_unexecuted_bios(struct request *rq)
  102. {
  103. struct bio *bio;
  104. while ((bio = rq->bio) != NULL) {
  105. rq->bio = bio->bi_next;
  106. bio_endio(bio, 0);
  107. }
  108. }
  109. void osd_end_request(struct osd_request *or)
  110. {
  111. struct request *rq = or->request;
  112. if (rq) {
  113. if (rq->next_rq) {
  114. _abort_unexecuted_bios(rq->next_rq);
  115. blk_put_request(rq->next_rq);
  116. }
  117. _abort_unexecuted_bios(rq);
  118. blk_put_request(rq);
  119. }
  120. _osd_request_free(or);
  121. }
  122. EXPORT_SYMBOL(osd_end_request);
  123. int osd_execute_request(struct osd_request *or)
  124. {
  125. return blk_execute_rq(or->request->q, NULL, or->request, 0);
  126. }
  127. EXPORT_SYMBOL(osd_execute_request);
  128. static void osd_request_async_done(struct request *req, int error)
  129. {
  130. struct osd_request *or = req->end_io_data;
  131. or->async_error = error;
  132. if (error)
  133. OSD_DEBUG("osd_request_async_done error recieved %d\n", error);
  134. if (or->async_done)
  135. or->async_done(or, or->async_private);
  136. else
  137. osd_end_request(or);
  138. }
  139. int osd_execute_request_async(struct osd_request *or,
  140. osd_req_done_fn *done, void *private)
  141. {
  142. or->request->end_io_data = or;
  143. or->async_private = private;
  144. or->async_done = done;
  145. blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
  146. osd_request_async_done);
  147. return 0;
  148. }
  149. EXPORT_SYMBOL(osd_execute_request_async);
  150. /*
  151. * Common to all OSD commands
  152. */
  153. static void _osdv1_req_encode_common(struct osd_request *or,
  154. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  155. {
  156. struct osdv1_cdb *ocdb = &or->cdb.v1;
  157. /*
  158. * For speed, the commands
  159. * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
  160. * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
  161. * are not supported here. Should pass zero and set after the call
  162. */
  163. act &= cpu_to_be16(~0x0080); /* V1 action code */
  164. OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
  165. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  166. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  167. ocdb->h.varlen_cdb.service_action = act;
  168. ocdb->h.partition = cpu_to_be64(obj->partition);
  169. ocdb->h.object = cpu_to_be64(obj->id);
  170. ocdb->h.v1.length = cpu_to_be64(len);
  171. ocdb->h.v1.start_address = cpu_to_be64(offset);
  172. }
  173. static void _osd_req_encode_common(struct osd_request *or,
  174. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  175. {
  176. _osdv1_req_encode_common(or, act, obj, offset, len);
  177. }
  178. /*
  179. * Device commands
  180. */
  181. void osd_req_format(struct osd_request *or, u64 tot_capacity)
  182. {
  183. _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
  184. tot_capacity);
  185. }
  186. EXPORT_SYMBOL(osd_req_format);
  187. /*
  188. * Partition commands
  189. */
  190. static void _osd_req_encode_partition(struct osd_request *or,
  191. __be16 act, osd_id partition)
  192. {
  193. struct osd_obj_id par = {
  194. .partition = partition,
  195. .id = 0,
  196. };
  197. _osd_req_encode_common(or, act, &par, 0, 0);
  198. }
  199. void osd_req_create_partition(struct osd_request *or, osd_id partition)
  200. {
  201. _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
  202. }
  203. EXPORT_SYMBOL(osd_req_create_partition);
  204. void osd_req_remove_partition(struct osd_request *or, osd_id partition)
  205. {
  206. _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
  207. }
  208. EXPORT_SYMBOL(osd_req_remove_partition);
  209. /*
  210. * Object commands
  211. */
  212. void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
  213. {
  214. _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
  215. }
  216. EXPORT_SYMBOL(osd_req_create_object);
  217. void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
  218. {
  219. _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
  220. }
  221. EXPORT_SYMBOL(osd_req_remove_object);
  222. void osd_req_write(struct osd_request *or,
  223. const struct osd_obj_id *obj, struct bio *bio, u64 offset)
  224. {
  225. _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, bio->bi_size);
  226. WARN_ON(or->out.bio || or->out.total_bytes);
  227. bio->bi_rw |= (1 << BIO_RW);
  228. or->out.bio = bio;
  229. or->out.total_bytes = bio->bi_size;
  230. }
  231. EXPORT_SYMBOL(osd_req_write);
  232. void osd_req_read(struct osd_request *or,
  233. const struct osd_obj_id *obj, struct bio *bio, u64 offset)
  234. {
  235. _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, bio->bi_size);
  236. WARN_ON(or->in.bio || or->in.total_bytes);
  237. bio->bi_rw &= ~(1 << BIO_RW);
  238. or->in.bio = bio;
  239. or->in.total_bytes = bio->bi_size;
  240. }
  241. EXPORT_SYMBOL(osd_req_read);
  242. /*
  243. * osd_finalize_request and helpers
  244. */
  245. static int _init_blk_request(struct osd_request *or,
  246. bool has_in, bool has_out)
  247. {
  248. gfp_t flags = or->alloc_flags;
  249. struct scsi_device *scsi_device = or->osd_dev->scsi_device;
  250. struct request_queue *q = scsi_device->request_queue;
  251. struct request *req;
  252. int ret = -ENOMEM;
  253. req = blk_get_request(q, has_out, flags);
  254. if (!req)
  255. goto out;
  256. or->request = req;
  257. req->cmd_type = REQ_TYPE_BLOCK_PC;
  258. req->timeout = or->timeout;
  259. req->retries = or->retries;
  260. req->sense = or->sense;
  261. req->sense_len = 0;
  262. if (has_out) {
  263. or->out.req = req;
  264. if (has_in) {
  265. /* allocate bidi request */
  266. req = blk_get_request(q, READ, flags);
  267. if (!req) {
  268. OSD_DEBUG("blk_get_request for bidi failed\n");
  269. goto out;
  270. }
  271. req->cmd_type = REQ_TYPE_BLOCK_PC;
  272. or->in.req = or->request->next_rq = req;
  273. }
  274. } else if (has_in)
  275. or->in.req = req;
  276. ret = 0;
  277. out:
  278. OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
  279. or, has_in, has_out, ret, or->request);
  280. return ret;
  281. }
  282. int osd_finalize_request(struct osd_request *or,
  283. u8 options, const void *cap, const u8 *cap_key)
  284. {
  285. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  286. bool has_in, has_out;
  287. int ret;
  288. if (options & OSD_REQ_FUA)
  289. cdbh->options |= OSD_CDB_FUA;
  290. if (options & OSD_REQ_DPO)
  291. cdbh->options |= OSD_CDB_DPO;
  292. if (options & OSD_REQ_BYPASS_TIMESTAMPS)
  293. cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
  294. osd_set_caps(&or->cdb, cap);
  295. has_in = or->in.bio || or->get_attr.total_bytes;
  296. has_out = or->out.bio || or->set_attr.total_bytes ||
  297. or->enc_get_attr.total_bytes;
  298. ret = _init_blk_request(or, has_in, has_out);
  299. if (ret) {
  300. OSD_DEBUG("_init_blk_request failed\n");
  301. return ret;
  302. }
  303. if (or->out.bio) {
  304. ret = blk_rq_append_bio(or->request->q, or->out.req,
  305. or->out.bio);
  306. if (ret) {
  307. OSD_DEBUG("blk_rq_append_bio out failed\n");
  308. return ret;
  309. }
  310. OSD_DEBUG("out bytes=%llu (bytes_req=%u)\n",
  311. _LLU(or->out.total_bytes), or->out.req->data_len);
  312. }
  313. if (or->in.bio) {
  314. ret = blk_rq_append_bio(or->request->q, or->in.req, or->in.bio);
  315. if (ret) {
  316. OSD_DEBUG("blk_rq_append_bio in failed\n");
  317. return ret;
  318. }
  319. OSD_DEBUG("in bytes=%llu (bytes_req=%u)\n",
  320. _LLU(or->in.total_bytes), or->in.req->data_len);
  321. }
  322. if (!or->attributes_mode)
  323. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  324. cdbh->command_specific_options |= or->attributes_mode;
  325. or->request->cmd = or->cdb.buff;
  326. or->request->cmd_len = _osd_req_cdb_len(or);
  327. return 0;
  328. }
  329. EXPORT_SYMBOL(osd_finalize_request);
  330. /*
  331. * Implementation of osd_sec.h API
  332. * TODO: Move to a separate osd_sec.c file at a later stage.
  333. */
  334. enum { OSD_SEC_CAP_V1_ALL_CAPS =
  335. OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
  336. OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
  337. OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
  338. OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
  339. };
  340. void osd_sec_init_nosec_doall_caps(void *caps,
  341. const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
  342. {
  343. struct osd_capability *cap = caps;
  344. u8 type;
  345. u8 descriptor_type;
  346. if (likely(obj->id)) {
  347. if (unlikely(is_collection)) {
  348. type = OSD_SEC_OBJ_COLLECTION;
  349. descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
  350. OSD_SEC_OBJ_DESC_COL;
  351. } else {
  352. type = OSD_SEC_OBJ_USER;
  353. descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
  354. }
  355. WARN_ON(!obj->partition);
  356. } else {
  357. type = obj->partition ? OSD_SEC_OBJ_PARTITION :
  358. OSD_SEC_OBJ_ROOT;
  359. descriptor_type = OSD_SEC_OBJ_DESC_PAR;
  360. }
  361. memset(cap, 0, sizeof(*cap));
  362. cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
  363. cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
  364. cap->h.security_method = OSD_SEC_NOSEC;
  365. /* cap->expiration_time;
  366. cap->AUDIT[30-10];
  367. cap->discriminator[42-30];
  368. cap->object_created_time; */
  369. cap->h.object_type = type;
  370. osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
  371. cap->h.object_descriptor_type = descriptor_type;
  372. cap->od.obj_desc.policy_access_tag = 0;
  373. cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
  374. cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
  375. }
  376. EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
  377. void osd_set_caps(struct osd_cdb *cdb, const void *caps)
  378. {
  379. memcpy(&cdb->v1.caps, caps, OSDv1_CAP_LEN);
  380. }