osd_initiator.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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 <linux/slab.h>
  42. #include <scsi/osd_initiator.h>
  43. #include <scsi/osd_sec.h>
  44. #include <scsi/osd_attributes.h>
  45. #include <scsi/osd_sense.h>
  46. #include <scsi/scsi_device.h>
  47. #include "osd_debug.h"
  48. #ifndef __unused
  49. # define __unused __attribute__((unused))
  50. #endif
  51. enum { OSD_REQ_RETRIES = 1 };
  52. MODULE_AUTHOR("Boaz Harrosh <bharrosh@panasas.com>");
  53. MODULE_DESCRIPTION("open-osd initiator library libosd.ko");
  54. MODULE_LICENSE("GPL");
  55. static inline void build_test(void)
  56. {
  57. /* structures were not packed */
  58. BUILD_BUG_ON(sizeof(struct osd_capability) != OSD_CAP_LEN);
  59. BUILD_BUG_ON(sizeof(struct osdv2_cdb) != OSD_TOTAL_CDB_LEN);
  60. BUILD_BUG_ON(sizeof(struct osdv1_cdb) != OSDv1_TOTAL_CDB_LEN);
  61. }
  62. static const char *_osd_ver_desc(struct osd_request *or)
  63. {
  64. return osd_req_is_ver1(or) ? "OSD1" : "OSD2";
  65. }
  66. #define ATTR_DEF_RI(id, len) ATTR_DEF(OSD_APAGE_ROOT_INFORMATION, id, len)
  67. static int _osd_get_print_system_info(struct osd_dev *od,
  68. void *caps, struct osd_dev_info *odi)
  69. {
  70. struct osd_request *or;
  71. struct osd_attr get_attrs[] = {
  72. ATTR_DEF_RI(OSD_ATTR_RI_VENDOR_IDENTIFICATION, 8),
  73. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_IDENTIFICATION, 16),
  74. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_MODEL, 32),
  75. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_REVISION_LEVEL, 4),
  76. ATTR_DEF_RI(OSD_ATTR_RI_PRODUCT_SERIAL_NUMBER, 64 /*variable*/),
  77. ATTR_DEF_RI(OSD_ATTR_RI_OSD_NAME, 64 /*variable*/),
  78. ATTR_DEF_RI(OSD_ATTR_RI_TOTAL_CAPACITY, 8),
  79. ATTR_DEF_RI(OSD_ATTR_RI_USED_CAPACITY, 8),
  80. ATTR_DEF_RI(OSD_ATTR_RI_NUMBER_OF_PARTITIONS, 8),
  81. ATTR_DEF_RI(OSD_ATTR_RI_CLOCK, 6),
  82. /* IBM-OSD-SIM Has a bug with this one put it last */
  83. ATTR_DEF_RI(OSD_ATTR_RI_OSD_SYSTEM_ID, 20),
  84. };
  85. void *iter = NULL, *pFirst;
  86. int nelem = ARRAY_SIZE(get_attrs), a = 0;
  87. int ret;
  88. or = osd_start_request(od, GFP_KERNEL);
  89. if (!or)
  90. return -ENOMEM;
  91. /* get attrs */
  92. osd_req_get_attributes(or, &osd_root_object);
  93. osd_req_add_get_attr_list(or, get_attrs, ARRAY_SIZE(get_attrs));
  94. ret = osd_finalize_request(or, 0, caps, NULL);
  95. if (ret)
  96. goto out;
  97. ret = osd_execute_request(or);
  98. if (ret) {
  99. OSD_ERR("Failed to detect %s => %d\n", _osd_ver_desc(or), ret);
  100. goto out;
  101. }
  102. osd_req_decode_get_attr_list(or, get_attrs, &nelem, &iter);
  103. OSD_INFO("Detected %s device\n",
  104. _osd_ver_desc(or));
  105. pFirst = get_attrs[a++].val_ptr;
  106. OSD_INFO("VENDOR_IDENTIFICATION [%s]\n",
  107. (char *)pFirst);
  108. pFirst = get_attrs[a++].val_ptr;
  109. OSD_INFO("PRODUCT_IDENTIFICATION [%s]\n",
  110. (char *)pFirst);
  111. pFirst = get_attrs[a++].val_ptr;
  112. OSD_INFO("PRODUCT_MODEL [%s]\n",
  113. (char *)pFirst);
  114. pFirst = get_attrs[a++].val_ptr;
  115. OSD_INFO("PRODUCT_REVISION_LEVEL [%u]\n",
  116. pFirst ? get_unaligned_be32(pFirst) : ~0U);
  117. pFirst = get_attrs[a++].val_ptr;
  118. OSD_INFO("PRODUCT_SERIAL_NUMBER [%s]\n",
  119. (char *)pFirst);
  120. odi->osdname_len = get_attrs[a].len;
  121. /* Avoid NULL for memcmp optimization 0-length is good enough */
  122. odi->osdname = kzalloc(odi->osdname_len + 1, GFP_KERNEL);
  123. if (odi->osdname_len)
  124. memcpy(odi->osdname, get_attrs[a].val_ptr, odi->osdname_len);
  125. OSD_INFO("OSD_NAME [%s]\n", odi->osdname);
  126. a++;
  127. pFirst = get_attrs[a++].val_ptr;
  128. OSD_INFO("TOTAL_CAPACITY [0x%llx]\n",
  129. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  130. pFirst = get_attrs[a++].val_ptr;
  131. OSD_INFO("USED_CAPACITY [0x%llx]\n",
  132. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  133. pFirst = get_attrs[a++].val_ptr;
  134. OSD_INFO("NUMBER_OF_PARTITIONS [%llu]\n",
  135. pFirst ? _LLU(get_unaligned_be64(pFirst)) : ~0ULL);
  136. if (a >= nelem)
  137. goto out;
  138. /* FIXME: Where are the time utilities */
  139. pFirst = get_attrs[a++].val_ptr;
  140. OSD_INFO("CLOCK [0x%02x%02x%02x%02x%02x%02x]\n",
  141. ((char *)pFirst)[0], ((char *)pFirst)[1],
  142. ((char *)pFirst)[2], ((char *)pFirst)[3],
  143. ((char *)pFirst)[4], ((char *)pFirst)[5]);
  144. if (a < nelem) { /* IBM-OSD-SIM bug, Might not have it */
  145. unsigned len = get_attrs[a].len;
  146. char sid_dump[32*4 + 2]; /* 2nibbles+space+ASCII */
  147. hex_dump_to_buffer(get_attrs[a].val_ptr, len, 32, 1,
  148. sid_dump, sizeof(sid_dump), true);
  149. OSD_INFO("OSD_SYSTEM_ID(%d)\n"
  150. " [%s]\n", len, sid_dump);
  151. if (unlikely(len > sizeof(odi->systemid))) {
  152. OSD_ERR("OSD Target error: OSD_SYSTEM_ID too long(%d). "
  153. "device idetification might not work\n", len);
  154. len = sizeof(odi->systemid);
  155. }
  156. odi->systemid_len = len;
  157. memcpy(odi->systemid, get_attrs[a].val_ptr, len);
  158. a++;
  159. }
  160. out:
  161. osd_end_request(or);
  162. return ret;
  163. }
  164. int osd_auto_detect_ver(struct osd_dev *od,
  165. void *caps, struct osd_dev_info *odi)
  166. {
  167. int ret;
  168. /* Auto-detect the osd version */
  169. ret = _osd_get_print_system_info(od, caps, odi);
  170. if (ret) {
  171. osd_dev_set_ver(od, OSD_VER1);
  172. OSD_DEBUG("converting to OSD1\n");
  173. ret = _osd_get_print_system_info(od, caps, odi);
  174. }
  175. return ret;
  176. }
  177. EXPORT_SYMBOL(osd_auto_detect_ver);
  178. static unsigned _osd_req_cdb_len(struct osd_request *or)
  179. {
  180. return osd_req_is_ver1(or) ? OSDv1_TOTAL_CDB_LEN : OSD_TOTAL_CDB_LEN;
  181. }
  182. static unsigned _osd_req_alist_elem_size(struct osd_request *or, unsigned len)
  183. {
  184. return osd_req_is_ver1(or) ?
  185. osdv1_attr_list_elem_size(len) :
  186. osdv2_attr_list_elem_size(len);
  187. }
  188. static void _osd_req_alist_elem_encode(struct osd_request *or,
  189. void *attr_last, const struct osd_attr *oa)
  190. {
  191. if (osd_req_is_ver1(or)) {
  192. struct osdv1_attributes_list_element *attr = attr_last;
  193. attr->attr_page = cpu_to_be32(oa->attr_page);
  194. attr->attr_id = cpu_to_be32(oa->attr_id);
  195. attr->attr_bytes = cpu_to_be16(oa->len);
  196. memcpy(attr->attr_val, oa->val_ptr, oa->len);
  197. } else {
  198. struct osdv2_attributes_list_element *attr = attr_last;
  199. attr->attr_page = cpu_to_be32(oa->attr_page);
  200. attr->attr_id = cpu_to_be32(oa->attr_id);
  201. attr->attr_bytes = cpu_to_be16(oa->len);
  202. memcpy(attr->attr_val, oa->val_ptr, oa->len);
  203. }
  204. }
  205. static int _osd_req_alist_elem_decode(struct osd_request *or,
  206. void *cur_p, struct osd_attr *oa, unsigned max_bytes)
  207. {
  208. unsigned inc;
  209. if (osd_req_is_ver1(or)) {
  210. struct osdv1_attributes_list_element *attr = cur_p;
  211. if (max_bytes < sizeof(*attr))
  212. return -1;
  213. oa->len = be16_to_cpu(attr->attr_bytes);
  214. inc = _osd_req_alist_elem_size(or, oa->len);
  215. if (inc > max_bytes)
  216. return -1;
  217. oa->attr_page = be32_to_cpu(attr->attr_page);
  218. oa->attr_id = be32_to_cpu(attr->attr_id);
  219. /* OSD1: On empty attributes we return a pointer to 2 bytes
  220. * of zeros. This keeps similar behaviour with OSD2.
  221. * (See below)
  222. */
  223. oa->val_ptr = likely(oa->len) ? attr->attr_val :
  224. (u8 *)&attr->attr_bytes;
  225. } else {
  226. struct osdv2_attributes_list_element *attr = cur_p;
  227. if (max_bytes < sizeof(*attr))
  228. return -1;
  229. oa->len = be16_to_cpu(attr->attr_bytes);
  230. inc = _osd_req_alist_elem_size(or, oa->len);
  231. if (inc > max_bytes)
  232. return -1;
  233. oa->attr_page = be32_to_cpu(attr->attr_page);
  234. oa->attr_id = be32_to_cpu(attr->attr_id);
  235. /* OSD2: For convenience, on empty attributes, we return 8 bytes
  236. * of zeros here. This keeps the same behaviour with OSD2r04,
  237. * and is nice with null terminating ASCII fields.
  238. * oa->val_ptr == NULL marks the end-of-list, or error.
  239. */
  240. oa->val_ptr = likely(oa->len) ? attr->attr_val : attr->reserved;
  241. }
  242. return inc;
  243. }
  244. static unsigned _osd_req_alist_size(struct osd_request *or, void *list_head)
  245. {
  246. return osd_req_is_ver1(or) ?
  247. osdv1_list_size(list_head) :
  248. osdv2_list_size(list_head);
  249. }
  250. static unsigned _osd_req_sizeof_alist_header(struct osd_request *or)
  251. {
  252. return osd_req_is_ver1(or) ?
  253. sizeof(struct osdv1_attributes_list_header) :
  254. sizeof(struct osdv2_attributes_list_header);
  255. }
  256. static void _osd_req_set_alist_type(struct osd_request *or,
  257. void *list, int list_type)
  258. {
  259. if (osd_req_is_ver1(or)) {
  260. struct osdv1_attributes_list_header *attr_list = list;
  261. memset(attr_list, 0, sizeof(*attr_list));
  262. attr_list->type = list_type;
  263. } else {
  264. struct osdv2_attributes_list_header *attr_list = list;
  265. memset(attr_list, 0, sizeof(*attr_list));
  266. attr_list->type = list_type;
  267. }
  268. }
  269. static bool _osd_req_is_alist_type(struct osd_request *or,
  270. void *list, int list_type)
  271. {
  272. if (!list)
  273. return false;
  274. if (osd_req_is_ver1(or)) {
  275. struct osdv1_attributes_list_header *attr_list = list;
  276. return attr_list->type == list_type;
  277. } else {
  278. struct osdv2_attributes_list_header *attr_list = list;
  279. return attr_list->type == list_type;
  280. }
  281. }
  282. /* This is for List-objects not Attributes-Lists */
  283. static void _osd_req_encode_olist(struct osd_request *or,
  284. struct osd_obj_id_list *list)
  285. {
  286. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  287. if (osd_req_is_ver1(or)) {
  288. cdbh->v1.list_identifier = list->list_identifier;
  289. cdbh->v1.start_address = list->continuation_id;
  290. } else {
  291. cdbh->v2.list_identifier = list->list_identifier;
  292. cdbh->v2.start_address = list->continuation_id;
  293. }
  294. }
  295. static osd_cdb_offset osd_req_encode_offset(struct osd_request *or,
  296. u64 offset, unsigned *padding)
  297. {
  298. return __osd_encode_offset(offset, padding,
  299. osd_req_is_ver1(or) ?
  300. OSDv1_OFFSET_MIN_SHIFT : OSD_OFFSET_MIN_SHIFT,
  301. OSD_OFFSET_MAX_SHIFT);
  302. }
  303. static struct osd_security_parameters *
  304. _osd_req_sec_params(struct osd_request *or)
  305. {
  306. struct osd_cdb *ocdb = &or->cdb;
  307. if (osd_req_is_ver1(or))
  308. return (struct osd_security_parameters *)&ocdb->v1.sec_params;
  309. else
  310. return (struct osd_security_parameters *)&ocdb->v2.sec_params;
  311. }
  312. void osd_dev_init(struct osd_dev *osdd, struct scsi_device *scsi_device)
  313. {
  314. memset(osdd, 0, sizeof(*osdd));
  315. osdd->scsi_device = scsi_device;
  316. osdd->def_timeout = BLK_DEFAULT_SG_TIMEOUT;
  317. #ifdef OSD_VER1_SUPPORT
  318. osdd->version = OSD_VER2;
  319. #endif
  320. /* TODO: Allocate pools for osd_request attributes ... */
  321. }
  322. EXPORT_SYMBOL(osd_dev_init);
  323. void osd_dev_fini(struct osd_dev *osdd)
  324. {
  325. /* TODO: De-allocate pools */
  326. osdd->scsi_device = NULL;
  327. }
  328. EXPORT_SYMBOL(osd_dev_fini);
  329. static struct osd_request *_osd_request_alloc(gfp_t gfp)
  330. {
  331. struct osd_request *or;
  332. /* TODO: Use mempool with one saved request */
  333. or = kzalloc(sizeof(*or), gfp);
  334. return or;
  335. }
  336. static void _osd_request_free(struct osd_request *or)
  337. {
  338. kfree(or);
  339. }
  340. struct osd_request *osd_start_request(struct osd_dev *dev, gfp_t gfp)
  341. {
  342. struct osd_request *or;
  343. or = _osd_request_alloc(gfp);
  344. if (!or)
  345. return NULL;
  346. or->osd_dev = dev;
  347. or->alloc_flags = gfp;
  348. or->timeout = dev->def_timeout;
  349. or->retries = OSD_REQ_RETRIES;
  350. return or;
  351. }
  352. EXPORT_SYMBOL(osd_start_request);
  353. static void _osd_free_seg(struct osd_request *or __unused,
  354. struct _osd_req_data_segment *seg)
  355. {
  356. if (!seg->buff || !seg->alloc_size)
  357. return;
  358. kfree(seg->buff);
  359. seg->buff = NULL;
  360. seg->alloc_size = 0;
  361. }
  362. static void _put_request(struct request *rq)
  363. {
  364. /*
  365. * If osd_finalize_request() was called but the request was not
  366. * executed through the block layer, then we must release BIOs.
  367. * TODO: Keep error code in or->async_error. Need to audit all
  368. * code paths.
  369. */
  370. if (unlikely(rq->bio))
  371. blk_end_request(rq, -ENOMEM, blk_rq_bytes(rq));
  372. else
  373. blk_put_request(rq);
  374. }
  375. void osd_end_request(struct osd_request *or)
  376. {
  377. struct request *rq = or->request;
  378. _osd_free_seg(or, &or->set_attr);
  379. _osd_free_seg(or, &or->enc_get_attr);
  380. _osd_free_seg(or, &or->get_attr);
  381. if (rq) {
  382. if (rq->next_rq) {
  383. _put_request(rq->next_rq);
  384. rq->next_rq = NULL;
  385. }
  386. _put_request(rq);
  387. }
  388. _osd_request_free(or);
  389. }
  390. EXPORT_SYMBOL(osd_end_request);
  391. static void _set_error_resid(struct osd_request *or, struct request *req,
  392. int error)
  393. {
  394. or->async_error = error;
  395. or->req_errors = req->errors ? : error;
  396. or->sense_len = req->sense_len;
  397. if (or->out.req)
  398. or->out.residual = or->out.req->resid_len;
  399. if (or->in.req)
  400. or->in.residual = or->in.req->resid_len;
  401. }
  402. int osd_execute_request(struct osd_request *or)
  403. {
  404. int error = blk_execute_rq(or->request->q, NULL, or->request, 0);
  405. _set_error_resid(or, or->request, error);
  406. return error;
  407. }
  408. EXPORT_SYMBOL(osd_execute_request);
  409. static void osd_request_async_done(struct request *req, int error)
  410. {
  411. struct osd_request *or = req->end_io_data;
  412. _set_error_resid(or, req, error);
  413. if (req->next_rq) {
  414. __blk_put_request(req->q, req->next_rq);
  415. req->next_rq = NULL;
  416. }
  417. __blk_put_request(req->q, req);
  418. or->request = NULL;
  419. or->in.req = NULL;
  420. or->out.req = NULL;
  421. if (or->async_done)
  422. or->async_done(or, or->async_private);
  423. else
  424. osd_end_request(or);
  425. }
  426. int osd_execute_request_async(struct osd_request *or,
  427. osd_req_done_fn *done, void *private)
  428. {
  429. or->request->end_io_data = or;
  430. or->async_private = private;
  431. or->async_done = done;
  432. blk_execute_rq_nowait(or->request->q, NULL, or->request, 0,
  433. osd_request_async_done);
  434. return 0;
  435. }
  436. EXPORT_SYMBOL(osd_execute_request_async);
  437. u8 sg_out_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  438. u8 sg_in_pad_buffer[1 << OSDv1_OFFSET_MIN_SHIFT];
  439. static int _osd_realloc_seg(struct osd_request *or,
  440. struct _osd_req_data_segment *seg, unsigned max_bytes)
  441. {
  442. void *buff;
  443. if (seg->alloc_size >= max_bytes)
  444. return 0;
  445. buff = krealloc(seg->buff, max_bytes, or->alloc_flags);
  446. if (!buff) {
  447. OSD_ERR("Failed to Realloc %d-bytes was-%d\n", max_bytes,
  448. seg->alloc_size);
  449. return -ENOMEM;
  450. }
  451. memset(buff + seg->alloc_size, 0, max_bytes - seg->alloc_size);
  452. seg->buff = buff;
  453. seg->alloc_size = max_bytes;
  454. return 0;
  455. }
  456. static int _alloc_set_attr_list(struct osd_request *or,
  457. const struct osd_attr *oa, unsigned nelem, unsigned add_bytes)
  458. {
  459. unsigned total_bytes = add_bytes;
  460. for (; nelem; --nelem, ++oa)
  461. total_bytes += _osd_req_alist_elem_size(or, oa->len);
  462. OSD_DEBUG("total_bytes=%d\n", total_bytes);
  463. return _osd_realloc_seg(or, &or->set_attr, total_bytes);
  464. }
  465. static int _alloc_get_attr_desc(struct osd_request *or, unsigned max_bytes)
  466. {
  467. OSD_DEBUG("total_bytes=%d\n", max_bytes);
  468. return _osd_realloc_seg(or, &or->enc_get_attr, max_bytes);
  469. }
  470. static int _alloc_get_attr_list(struct osd_request *or)
  471. {
  472. OSD_DEBUG("total_bytes=%d\n", or->get_attr.total_bytes);
  473. return _osd_realloc_seg(or, &or->get_attr, or->get_attr.total_bytes);
  474. }
  475. /*
  476. * Common to all OSD commands
  477. */
  478. static void _osdv1_req_encode_common(struct osd_request *or,
  479. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  480. {
  481. struct osdv1_cdb *ocdb = &or->cdb.v1;
  482. /*
  483. * For speed, the commands
  484. * OSD_ACT_PERFORM_SCSI_COMMAND , V1 0x8F7E, V2 0x8F7C
  485. * OSD_ACT_SCSI_TASK_MANAGEMENT , V1 0x8F7F, V2 0x8F7D
  486. * are not supported here. Should pass zero and set after the call
  487. */
  488. act &= cpu_to_be16(~0x0080); /* V1 action code */
  489. OSD_DEBUG("OSDv1 execute opcode 0x%x\n", be16_to_cpu(act));
  490. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  491. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  492. ocdb->h.varlen_cdb.service_action = act;
  493. ocdb->h.partition = cpu_to_be64(obj->partition);
  494. ocdb->h.object = cpu_to_be64(obj->id);
  495. ocdb->h.v1.length = cpu_to_be64(len);
  496. ocdb->h.v1.start_address = cpu_to_be64(offset);
  497. }
  498. static void _osdv2_req_encode_common(struct osd_request *or,
  499. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  500. {
  501. struct osdv2_cdb *ocdb = &or->cdb.v2;
  502. OSD_DEBUG("OSDv2 execute opcode 0x%x\n", be16_to_cpu(act));
  503. ocdb->h.varlen_cdb.opcode = VARIABLE_LENGTH_CMD;
  504. ocdb->h.varlen_cdb.additional_cdb_length = OSD_ADDITIONAL_CDB_LENGTH;
  505. ocdb->h.varlen_cdb.service_action = act;
  506. ocdb->h.partition = cpu_to_be64(obj->partition);
  507. ocdb->h.object = cpu_to_be64(obj->id);
  508. ocdb->h.v2.length = cpu_to_be64(len);
  509. ocdb->h.v2.start_address = cpu_to_be64(offset);
  510. }
  511. static void _osd_req_encode_common(struct osd_request *or,
  512. __be16 act, const struct osd_obj_id *obj, u64 offset, u64 len)
  513. {
  514. if (osd_req_is_ver1(or))
  515. _osdv1_req_encode_common(or, act, obj, offset, len);
  516. else
  517. _osdv2_req_encode_common(or, act, obj, offset, len);
  518. }
  519. /*
  520. * Device commands
  521. */
  522. /*TODO: void osd_req_set_master_seed_xchg(struct osd_request *, ...); */
  523. /*TODO: void osd_req_set_master_key(struct osd_request *, ...); */
  524. void osd_req_format(struct osd_request *or, u64 tot_capacity)
  525. {
  526. _osd_req_encode_common(or, OSD_ACT_FORMAT_OSD, &osd_root_object, 0,
  527. tot_capacity);
  528. }
  529. EXPORT_SYMBOL(osd_req_format);
  530. int osd_req_list_dev_partitions(struct osd_request *or,
  531. osd_id initial_id, struct osd_obj_id_list *list, unsigned nelem)
  532. {
  533. return osd_req_list_partition_objects(or, 0, initial_id, list, nelem);
  534. }
  535. EXPORT_SYMBOL(osd_req_list_dev_partitions);
  536. static void _osd_req_encode_flush(struct osd_request *or,
  537. enum osd_options_flush_scope_values op)
  538. {
  539. struct osd_cdb_head *ocdb = osd_cdb_head(&or->cdb);
  540. ocdb->command_specific_options = op;
  541. }
  542. void osd_req_flush_obsd(struct osd_request *or,
  543. enum osd_options_flush_scope_values op)
  544. {
  545. _osd_req_encode_common(or, OSD_ACT_FLUSH_OSD, &osd_root_object, 0, 0);
  546. _osd_req_encode_flush(or, op);
  547. }
  548. EXPORT_SYMBOL(osd_req_flush_obsd);
  549. /*TODO: void osd_req_perform_scsi_command(struct osd_request *,
  550. const u8 *cdb, ...); */
  551. /*TODO: void osd_req_task_management(struct osd_request *, ...); */
  552. /*
  553. * Partition commands
  554. */
  555. static void _osd_req_encode_partition(struct osd_request *or,
  556. __be16 act, osd_id partition)
  557. {
  558. struct osd_obj_id par = {
  559. .partition = partition,
  560. .id = 0,
  561. };
  562. _osd_req_encode_common(or, act, &par, 0, 0);
  563. }
  564. void osd_req_create_partition(struct osd_request *or, osd_id partition)
  565. {
  566. _osd_req_encode_partition(or, OSD_ACT_CREATE_PARTITION, partition);
  567. }
  568. EXPORT_SYMBOL(osd_req_create_partition);
  569. void osd_req_remove_partition(struct osd_request *or, osd_id partition)
  570. {
  571. _osd_req_encode_partition(or, OSD_ACT_REMOVE_PARTITION, partition);
  572. }
  573. EXPORT_SYMBOL(osd_req_remove_partition);
  574. /*TODO: void osd_req_set_partition_key(struct osd_request *,
  575. osd_id partition, u8 new_key_id[OSD_CRYPTO_KEYID_SIZE],
  576. u8 seed[OSD_CRYPTO_SEED_SIZE]); */
  577. static int _osd_req_list_objects(struct osd_request *or,
  578. __be16 action, const struct osd_obj_id *obj, osd_id initial_id,
  579. struct osd_obj_id_list *list, unsigned nelem)
  580. {
  581. struct request_queue *q = osd_request_queue(or->osd_dev);
  582. u64 len = nelem * sizeof(osd_id) + sizeof(*list);
  583. struct bio *bio;
  584. _osd_req_encode_common(or, action, obj, (u64)initial_id, len);
  585. if (list->list_identifier)
  586. _osd_req_encode_olist(or, list);
  587. WARN_ON(or->in.bio);
  588. bio = bio_map_kern(q, list, len, or->alloc_flags);
  589. if (IS_ERR(bio)) {
  590. OSD_ERR("!!! Failed to allocate list_objects BIO\n");
  591. return PTR_ERR(bio);
  592. }
  593. bio->bi_rw &= ~(1 << BIO_RW);
  594. or->in.bio = bio;
  595. or->in.total_bytes = bio->bi_size;
  596. return 0;
  597. }
  598. int osd_req_list_partition_collections(struct osd_request *or,
  599. osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
  600. unsigned nelem)
  601. {
  602. struct osd_obj_id par = {
  603. .partition = partition,
  604. .id = 0,
  605. };
  606. return osd_req_list_collection_objects(or, &par, initial_id, list,
  607. nelem);
  608. }
  609. EXPORT_SYMBOL(osd_req_list_partition_collections);
  610. int osd_req_list_partition_objects(struct osd_request *or,
  611. osd_id partition, osd_id initial_id, struct osd_obj_id_list *list,
  612. unsigned nelem)
  613. {
  614. struct osd_obj_id par = {
  615. .partition = partition,
  616. .id = 0,
  617. };
  618. return _osd_req_list_objects(or, OSD_ACT_LIST, &par, initial_id, list,
  619. nelem);
  620. }
  621. EXPORT_SYMBOL(osd_req_list_partition_objects);
  622. void osd_req_flush_partition(struct osd_request *or,
  623. osd_id partition, enum osd_options_flush_scope_values op)
  624. {
  625. _osd_req_encode_partition(or, OSD_ACT_FLUSH_PARTITION, partition);
  626. _osd_req_encode_flush(or, op);
  627. }
  628. EXPORT_SYMBOL(osd_req_flush_partition);
  629. /*
  630. * Collection commands
  631. */
  632. /*TODO: void osd_req_create_collection(struct osd_request *,
  633. const struct osd_obj_id *); */
  634. /*TODO: void osd_req_remove_collection(struct osd_request *,
  635. const struct osd_obj_id *); */
  636. int osd_req_list_collection_objects(struct osd_request *or,
  637. const struct osd_obj_id *obj, osd_id initial_id,
  638. struct osd_obj_id_list *list, unsigned nelem)
  639. {
  640. return _osd_req_list_objects(or, OSD_ACT_LIST_COLLECTION, obj,
  641. initial_id, list, nelem);
  642. }
  643. EXPORT_SYMBOL(osd_req_list_collection_objects);
  644. /*TODO: void query(struct osd_request *, ...); V2 */
  645. void osd_req_flush_collection(struct osd_request *or,
  646. const struct osd_obj_id *obj, enum osd_options_flush_scope_values op)
  647. {
  648. _osd_req_encode_common(or, OSD_ACT_FLUSH_PARTITION, obj, 0, 0);
  649. _osd_req_encode_flush(or, op);
  650. }
  651. EXPORT_SYMBOL(osd_req_flush_collection);
  652. /*TODO: void get_member_attrs(struct osd_request *, ...); V2 */
  653. /*TODO: void set_member_attrs(struct osd_request *, ...); V2 */
  654. /*
  655. * Object commands
  656. */
  657. void osd_req_create_object(struct osd_request *or, struct osd_obj_id *obj)
  658. {
  659. _osd_req_encode_common(or, OSD_ACT_CREATE, obj, 0, 0);
  660. }
  661. EXPORT_SYMBOL(osd_req_create_object);
  662. void osd_req_remove_object(struct osd_request *or, struct osd_obj_id *obj)
  663. {
  664. _osd_req_encode_common(or, OSD_ACT_REMOVE, obj, 0, 0);
  665. }
  666. EXPORT_SYMBOL(osd_req_remove_object);
  667. /*TODO: void osd_req_create_multi(struct osd_request *or,
  668. struct osd_obj_id *first, struct osd_obj_id_list *list, unsigned nelem);
  669. */
  670. void osd_req_write(struct osd_request *or,
  671. const struct osd_obj_id *obj, u64 offset,
  672. struct bio *bio, u64 len)
  673. {
  674. _osd_req_encode_common(or, OSD_ACT_WRITE, obj, offset, len);
  675. WARN_ON(or->out.bio || or->out.total_bytes);
  676. WARN_ON(0 == bio_rw_flagged(bio, BIO_RW));
  677. or->out.bio = bio;
  678. or->out.total_bytes = len;
  679. }
  680. EXPORT_SYMBOL(osd_req_write);
  681. int osd_req_write_kern(struct osd_request *or,
  682. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  683. {
  684. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  685. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  686. if (IS_ERR(bio))
  687. return PTR_ERR(bio);
  688. bio->bi_rw |= (1 << BIO_RW); /* FIXME: bio_set_dir() */
  689. osd_req_write(or, obj, offset, bio, len);
  690. return 0;
  691. }
  692. EXPORT_SYMBOL(osd_req_write_kern);
  693. /*TODO: void osd_req_append(struct osd_request *,
  694. const struct osd_obj_id *, struct bio *data_out); */
  695. /*TODO: void osd_req_create_write(struct osd_request *,
  696. const struct osd_obj_id *, struct bio *data_out, u64 offset); */
  697. /*TODO: void osd_req_clear(struct osd_request *,
  698. const struct osd_obj_id *, u64 offset, u64 len); */
  699. /*TODO: void osd_req_punch(struct osd_request *,
  700. const struct osd_obj_id *, u64 offset, u64 len); V2 */
  701. void osd_req_flush_object(struct osd_request *or,
  702. const struct osd_obj_id *obj, enum osd_options_flush_scope_values op,
  703. /*V2*/ u64 offset, /*V2*/ u64 len)
  704. {
  705. if (unlikely(osd_req_is_ver1(or) && (offset || len))) {
  706. OSD_DEBUG("OSD Ver1 flush on specific range ignored\n");
  707. offset = 0;
  708. len = 0;
  709. }
  710. _osd_req_encode_common(or, OSD_ACT_FLUSH, obj, offset, len);
  711. _osd_req_encode_flush(or, op);
  712. }
  713. EXPORT_SYMBOL(osd_req_flush_object);
  714. void osd_req_read(struct osd_request *or,
  715. const struct osd_obj_id *obj, u64 offset,
  716. struct bio *bio, u64 len)
  717. {
  718. _osd_req_encode_common(or, OSD_ACT_READ, obj, offset, len);
  719. WARN_ON(or->in.bio || or->in.total_bytes);
  720. WARN_ON(1 == bio_rw_flagged(bio, BIO_RW));
  721. or->in.bio = bio;
  722. or->in.total_bytes = len;
  723. }
  724. EXPORT_SYMBOL(osd_req_read);
  725. int osd_req_read_kern(struct osd_request *or,
  726. const struct osd_obj_id *obj, u64 offset, void* buff, u64 len)
  727. {
  728. struct request_queue *req_q = osd_request_queue(or->osd_dev);
  729. struct bio *bio = bio_map_kern(req_q, buff, len, GFP_KERNEL);
  730. if (IS_ERR(bio))
  731. return PTR_ERR(bio);
  732. osd_req_read(or, obj, offset, bio, len);
  733. return 0;
  734. }
  735. EXPORT_SYMBOL(osd_req_read_kern);
  736. void osd_req_get_attributes(struct osd_request *or,
  737. const struct osd_obj_id *obj)
  738. {
  739. _osd_req_encode_common(or, OSD_ACT_GET_ATTRIBUTES, obj, 0, 0);
  740. }
  741. EXPORT_SYMBOL(osd_req_get_attributes);
  742. void osd_req_set_attributes(struct osd_request *or,
  743. const struct osd_obj_id *obj)
  744. {
  745. _osd_req_encode_common(or, OSD_ACT_SET_ATTRIBUTES, obj, 0, 0);
  746. }
  747. EXPORT_SYMBOL(osd_req_set_attributes);
  748. /*
  749. * Attributes List-mode
  750. */
  751. int osd_req_add_set_attr_list(struct osd_request *or,
  752. const struct osd_attr *oa, unsigned nelem)
  753. {
  754. unsigned total_bytes = or->set_attr.total_bytes;
  755. void *attr_last;
  756. int ret;
  757. if (or->attributes_mode &&
  758. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  759. WARN_ON(1);
  760. return -EINVAL;
  761. }
  762. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  763. if (!total_bytes) { /* first-time: allocate and put list header */
  764. total_bytes = _osd_req_sizeof_alist_header(or);
  765. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  766. if (ret)
  767. return ret;
  768. _osd_req_set_alist_type(or, or->set_attr.buff,
  769. OSD_ATTR_LIST_SET_RETRIEVE);
  770. }
  771. attr_last = or->set_attr.buff + total_bytes;
  772. for (; nelem; --nelem) {
  773. unsigned elem_size = _osd_req_alist_elem_size(or, oa->len);
  774. total_bytes += elem_size;
  775. if (unlikely(or->set_attr.alloc_size < total_bytes)) {
  776. or->set_attr.total_bytes = total_bytes - elem_size;
  777. ret = _alloc_set_attr_list(or, oa, nelem, total_bytes);
  778. if (ret)
  779. return ret;
  780. attr_last =
  781. or->set_attr.buff + or->set_attr.total_bytes;
  782. }
  783. _osd_req_alist_elem_encode(or, attr_last, oa);
  784. attr_last += elem_size;
  785. ++oa;
  786. }
  787. or->set_attr.total_bytes = total_bytes;
  788. return 0;
  789. }
  790. EXPORT_SYMBOL(osd_req_add_set_attr_list);
  791. static int _req_append_segment(struct osd_request *or,
  792. unsigned padding, struct _osd_req_data_segment *seg,
  793. struct _osd_req_data_segment *last_seg, struct _osd_io_info *io)
  794. {
  795. void *pad_buff;
  796. int ret;
  797. if (padding) {
  798. /* check if we can just add it to last buffer */
  799. if (last_seg &&
  800. (padding <= last_seg->alloc_size - last_seg->total_bytes))
  801. pad_buff = last_seg->buff + last_seg->total_bytes;
  802. else
  803. pad_buff = io->pad_buff;
  804. ret = blk_rq_map_kern(io->req->q, io->req, pad_buff, padding,
  805. or->alloc_flags);
  806. if (ret)
  807. return ret;
  808. io->total_bytes += padding;
  809. }
  810. ret = blk_rq_map_kern(io->req->q, io->req, seg->buff, seg->total_bytes,
  811. or->alloc_flags);
  812. if (ret)
  813. return ret;
  814. io->total_bytes += seg->total_bytes;
  815. OSD_DEBUG("padding=%d buff=%p total_bytes=%d\n", padding, seg->buff,
  816. seg->total_bytes);
  817. return 0;
  818. }
  819. static int _osd_req_finalize_set_attr_list(struct osd_request *or)
  820. {
  821. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  822. unsigned padding;
  823. int ret;
  824. if (!or->set_attr.total_bytes) {
  825. cdbh->attrs_list.set_attr_offset = OSD_OFFSET_UNUSED;
  826. return 0;
  827. }
  828. cdbh->attrs_list.set_attr_bytes = cpu_to_be32(or->set_attr.total_bytes);
  829. cdbh->attrs_list.set_attr_offset =
  830. osd_req_encode_offset(or, or->out.total_bytes, &padding);
  831. ret = _req_append_segment(or, padding, &or->set_attr,
  832. or->out.last_seg, &or->out);
  833. if (ret)
  834. return ret;
  835. or->out.last_seg = &or->set_attr;
  836. return 0;
  837. }
  838. int osd_req_add_get_attr_list(struct osd_request *or,
  839. const struct osd_attr *oa, unsigned nelem)
  840. {
  841. unsigned total_bytes = or->enc_get_attr.total_bytes;
  842. void *attr_last;
  843. int ret;
  844. if (or->attributes_mode &&
  845. or->attributes_mode != OSD_CDB_GET_SET_ATTR_LISTS) {
  846. WARN_ON(1);
  847. return -EINVAL;
  848. }
  849. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  850. /* first time calc data-in list header size */
  851. if (!or->get_attr.total_bytes)
  852. or->get_attr.total_bytes = _osd_req_sizeof_alist_header(or);
  853. /* calc data-out info */
  854. if (!total_bytes) { /* first-time: allocate and put list header */
  855. unsigned max_bytes;
  856. total_bytes = _osd_req_sizeof_alist_header(or);
  857. max_bytes = total_bytes +
  858. nelem * sizeof(struct osd_attributes_list_attrid);
  859. ret = _alloc_get_attr_desc(or, max_bytes);
  860. if (ret)
  861. return ret;
  862. _osd_req_set_alist_type(or, or->enc_get_attr.buff,
  863. OSD_ATTR_LIST_GET);
  864. }
  865. attr_last = or->enc_get_attr.buff + total_bytes;
  866. for (; nelem; --nelem) {
  867. struct osd_attributes_list_attrid *attrid;
  868. const unsigned cur_size = sizeof(*attrid);
  869. total_bytes += cur_size;
  870. if (unlikely(or->enc_get_attr.alloc_size < total_bytes)) {
  871. or->enc_get_attr.total_bytes = total_bytes - cur_size;
  872. ret = _alloc_get_attr_desc(or,
  873. total_bytes + nelem * sizeof(*attrid));
  874. if (ret)
  875. return ret;
  876. attr_last = or->enc_get_attr.buff +
  877. or->enc_get_attr.total_bytes;
  878. }
  879. attrid = attr_last;
  880. attrid->attr_page = cpu_to_be32(oa->attr_page);
  881. attrid->attr_id = cpu_to_be32(oa->attr_id);
  882. attr_last += cur_size;
  883. /* calc data-in size */
  884. or->get_attr.total_bytes +=
  885. _osd_req_alist_elem_size(or, oa->len);
  886. ++oa;
  887. }
  888. or->enc_get_attr.total_bytes = total_bytes;
  889. OSD_DEBUG(
  890. "get_attr.total_bytes=%u(%u) enc_get_attr.total_bytes=%u(%Zu)\n",
  891. or->get_attr.total_bytes,
  892. or->get_attr.total_bytes - _osd_req_sizeof_alist_header(or),
  893. or->enc_get_attr.total_bytes,
  894. (or->enc_get_attr.total_bytes - _osd_req_sizeof_alist_header(or))
  895. / sizeof(struct osd_attributes_list_attrid));
  896. return 0;
  897. }
  898. EXPORT_SYMBOL(osd_req_add_get_attr_list);
  899. static int _osd_req_finalize_get_attr_list(struct osd_request *or)
  900. {
  901. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  902. unsigned out_padding;
  903. unsigned in_padding;
  904. int ret;
  905. if (!or->enc_get_attr.total_bytes) {
  906. cdbh->attrs_list.get_attr_desc_offset = OSD_OFFSET_UNUSED;
  907. cdbh->attrs_list.get_attr_offset = OSD_OFFSET_UNUSED;
  908. return 0;
  909. }
  910. ret = _alloc_get_attr_list(or);
  911. if (ret)
  912. return ret;
  913. /* The out-going buffer info update */
  914. OSD_DEBUG("out-going\n");
  915. cdbh->attrs_list.get_attr_desc_bytes =
  916. cpu_to_be32(or->enc_get_attr.total_bytes);
  917. cdbh->attrs_list.get_attr_desc_offset =
  918. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  919. ret = _req_append_segment(or, out_padding, &or->enc_get_attr,
  920. or->out.last_seg, &or->out);
  921. if (ret)
  922. return ret;
  923. or->out.last_seg = &or->enc_get_attr;
  924. /* The incoming buffer info update */
  925. OSD_DEBUG("in-coming\n");
  926. cdbh->attrs_list.get_attr_alloc_length =
  927. cpu_to_be32(or->get_attr.total_bytes);
  928. cdbh->attrs_list.get_attr_offset =
  929. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  930. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  931. &or->in);
  932. if (ret)
  933. return ret;
  934. or->in.last_seg = &or->get_attr;
  935. return 0;
  936. }
  937. int osd_req_decode_get_attr_list(struct osd_request *or,
  938. struct osd_attr *oa, int *nelem, void **iterator)
  939. {
  940. unsigned cur_bytes, returned_bytes;
  941. int n;
  942. const unsigned sizeof_attr_list = _osd_req_sizeof_alist_header(or);
  943. void *cur_p;
  944. if (!_osd_req_is_alist_type(or, or->get_attr.buff,
  945. OSD_ATTR_LIST_SET_RETRIEVE)) {
  946. oa->attr_page = 0;
  947. oa->attr_id = 0;
  948. oa->val_ptr = NULL;
  949. oa->len = 0;
  950. *iterator = NULL;
  951. return 0;
  952. }
  953. if (*iterator) {
  954. BUG_ON((*iterator < or->get_attr.buff) ||
  955. (or->get_attr.buff + or->get_attr.alloc_size < *iterator));
  956. cur_p = *iterator;
  957. cur_bytes = (*iterator - or->get_attr.buff) - sizeof_attr_list;
  958. returned_bytes = or->get_attr.total_bytes;
  959. } else { /* first time decode the list header */
  960. cur_bytes = sizeof_attr_list;
  961. returned_bytes = _osd_req_alist_size(or, or->get_attr.buff) +
  962. sizeof_attr_list;
  963. cur_p = or->get_attr.buff + sizeof_attr_list;
  964. if (returned_bytes > or->get_attr.alloc_size) {
  965. OSD_DEBUG("target report: space was not big enough! "
  966. "Allocate=%u Needed=%u\n",
  967. or->get_attr.alloc_size,
  968. returned_bytes + sizeof_attr_list);
  969. returned_bytes =
  970. or->get_attr.alloc_size - sizeof_attr_list;
  971. }
  972. or->get_attr.total_bytes = returned_bytes;
  973. }
  974. for (n = 0; (n < *nelem) && (cur_bytes < returned_bytes); ++n) {
  975. int inc = _osd_req_alist_elem_decode(or, cur_p, oa,
  976. returned_bytes - cur_bytes);
  977. if (inc < 0) {
  978. OSD_ERR("BAD FOOD from target. list not valid!"
  979. "c=%d r=%d n=%d\n",
  980. cur_bytes, returned_bytes, n);
  981. oa->val_ptr = NULL;
  982. cur_bytes = returned_bytes; /* break the caller loop */
  983. break;
  984. }
  985. cur_bytes += inc;
  986. cur_p += inc;
  987. ++oa;
  988. }
  989. *iterator = (returned_bytes - cur_bytes) ? cur_p : NULL;
  990. *nelem = n;
  991. return returned_bytes - cur_bytes;
  992. }
  993. EXPORT_SYMBOL(osd_req_decode_get_attr_list);
  994. /*
  995. * Attributes Page-mode
  996. */
  997. int osd_req_add_get_attr_page(struct osd_request *or,
  998. u32 page_id, void *attar_page, unsigned max_page_len,
  999. const struct osd_attr *set_one_attr)
  1000. {
  1001. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1002. if (or->attributes_mode &&
  1003. or->attributes_mode != OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  1004. WARN_ON(1);
  1005. return -EINVAL;
  1006. }
  1007. or->attributes_mode = OSD_CDB_GET_ATTR_PAGE_SET_ONE;
  1008. or->get_attr.buff = attar_page;
  1009. or->get_attr.total_bytes = max_page_len;
  1010. or->set_attr.buff = set_one_attr->val_ptr;
  1011. or->set_attr.total_bytes = set_one_attr->len;
  1012. cdbh->attrs_page.get_attr_page = cpu_to_be32(page_id);
  1013. cdbh->attrs_page.get_attr_alloc_length = cpu_to_be32(max_page_len);
  1014. /* ocdb->attrs_page.get_attr_offset; */
  1015. cdbh->attrs_page.set_attr_page = cpu_to_be32(set_one_attr->attr_page);
  1016. cdbh->attrs_page.set_attr_id = cpu_to_be32(set_one_attr->attr_id);
  1017. cdbh->attrs_page.set_attr_length = cpu_to_be32(set_one_attr->len);
  1018. /* ocdb->attrs_page.set_attr_offset; */
  1019. return 0;
  1020. }
  1021. EXPORT_SYMBOL(osd_req_add_get_attr_page);
  1022. static int _osd_req_finalize_attr_page(struct osd_request *or)
  1023. {
  1024. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1025. unsigned in_padding, out_padding;
  1026. int ret;
  1027. /* returned page */
  1028. cdbh->attrs_page.get_attr_offset =
  1029. osd_req_encode_offset(or, or->in.total_bytes, &in_padding);
  1030. ret = _req_append_segment(or, in_padding, &or->get_attr, NULL,
  1031. &or->in);
  1032. if (ret)
  1033. return ret;
  1034. /* set one value */
  1035. cdbh->attrs_page.set_attr_offset =
  1036. osd_req_encode_offset(or, or->out.total_bytes, &out_padding);
  1037. ret = _req_append_segment(or, out_padding, &or->enc_get_attr, NULL,
  1038. &or->out);
  1039. return ret;
  1040. }
  1041. static inline void osd_sec_parms_set_out_offset(bool is_v1,
  1042. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1043. {
  1044. if (is_v1)
  1045. sec_parms->v1.data_out_integrity_check_offset = offset;
  1046. else
  1047. sec_parms->v2.data_out_integrity_check_offset = offset;
  1048. }
  1049. static inline void osd_sec_parms_set_in_offset(bool is_v1,
  1050. struct osd_security_parameters *sec_parms, osd_cdb_offset offset)
  1051. {
  1052. if (is_v1)
  1053. sec_parms->v1.data_in_integrity_check_offset = offset;
  1054. else
  1055. sec_parms->v2.data_in_integrity_check_offset = offset;
  1056. }
  1057. static int _osd_req_finalize_data_integrity(struct osd_request *or,
  1058. bool has_in, bool has_out, u64 out_data_bytes, const u8 *cap_key)
  1059. {
  1060. struct osd_security_parameters *sec_parms = _osd_req_sec_params(or);
  1061. int ret;
  1062. if (!osd_is_sec_alldata(sec_parms))
  1063. return 0;
  1064. if (has_out) {
  1065. struct _osd_req_data_segment seg = {
  1066. .buff = &or->out_data_integ,
  1067. .total_bytes = sizeof(or->out_data_integ),
  1068. };
  1069. unsigned pad;
  1070. or->out_data_integ.data_bytes = cpu_to_be64(out_data_bytes);
  1071. or->out_data_integ.set_attributes_bytes = cpu_to_be64(
  1072. or->set_attr.total_bytes);
  1073. or->out_data_integ.get_attributes_bytes = cpu_to_be64(
  1074. or->enc_get_attr.total_bytes);
  1075. osd_sec_parms_set_out_offset(osd_req_is_ver1(or), sec_parms,
  1076. osd_req_encode_offset(or, or->out.total_bytes, &pad));
  1077. ret = _req_append_segment(or, pad, &seg, or->out.last_seg,
  1078. &or->out);
  1079. if (ret)
  1080. return ret;
  1081. or->out.last_seg = NULL;
  1082. /* they are now all chained to request sign them all together */
  1083. osd_sec_sign_data(&or->out_data_integ, or->out.req->bio,
  1084. cap_key);
  1085. }
  1086. if (has_in) {
  1087. struct _osd_req_data_segment seg = {
  1088. .buff = &or->in_data_integ,
  1089. .total_bytes = sizeof(or->in_data_integ),
  1090. };
  1091. unsigned pad;
  1092. osd_sec_parms_set_in_offset(osd_req_is_ver1(or), sec_parms,
  1093. osd_req_encode_offset(or, or->in.total_bytes, &pad));
  1094. ret = _req_append_segment(or, pad, &seg, or->in.last_seg,
  1095. &or->in);
  1096. if (ret)
  1097. return ret;
  1098. or->in.last_seg = NULL;
  1099. }
  1100. return 0;
  1101. }
  1102. /*
  1103. * osd_finalize_request and helpers
  1104. */
  1105. static struct request *_make_request(struct request_queue *q, bool has_write,
  1106. struct _osd_io_info *oii, gfp_t flags)
  1107. {
  1108. if (oii->bio)
  1109. return blk_make_request(q, oii->bio, flags);
  1110. else {
  1111. struct request *req;
  1112. req = blk_get_request(q, has_write ? WRITE : READ, flags);
  1113. if (unlikely(!req))
  1114. return ERR_PTR(-ENOMEM);
  1115. return req;
  1116. }
  1117. }
  1118. static int _init_blk_request(struct osd_request *or,
  1119. bool has_in, bool has_out)
  1120. {
  1121. gfp_t flags = or->alloc_flags;
  1122. struct scsi_device *scsi_device = or->osd_dev->scsi_device;
  1123. struct request_queue *q = scsi_device->request_queue;
  1124. struct request *req;
  1125. int ret;
  1126. req = _make_request(q, has_out, has_out ? &or->out : &or->in, flags);
  1127. if (IS_ERR(req)) {
  1128. ret = PTR_ERR(req);
  1129. goto out;
  1130. }
  1131. or->request = req;
  1132. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1133. req->cmd_flags |= REQ_QUIET;
  1134. req->timeout = or->timeout;
  1135. req->retries = or->retries;
  1136. req->sense = or->sense;
  1137. req->sense_len = 0;
  1138. if (has_out) {
  1139. or->out.req = req;
  1140. if (has_in) {
  1141. /* allocate bidi request */
  1142. req = _make_request(q, false, &or->in, flags);
  1143. if (IS_ERR(req)) {
  1144. OSD_DEBUG("blk_get_request for bidi failed\n");
  1145. ret = PTR_ERR(req);
  1146. goto out;
  1147. }
  1148. req->cmd_type = REQ_TYPE_BLOCK_PC;
  1149. or->in.req = or->request->next_rq = req;
  1150. }
  1151. } else if (has_in)
  1152. or->in.req = req;
  1153. ret = 0;
  1154. out:
  1155. OSD_DEBUG("or=%p has_in=%d has_out=%d => %d, %p\n",
  1156. or, has_in, has_out, ret, or->request);
  1157. return ret;
  1158. }
  1159. int osd_finalize_request(struct osd_request *or,
  1160. u8 options, const void *cap, const u8 *cap_key)
  1161. {
  1162. struct osd_cdb_head *cdbh = osd_cdb_head(&or->cdb);
  1163. bool has_in, has_out;
  1164. u64 out_data_bytes = or->out.total_bytes;
  1165. int ret;
  1166. if (options & OSD_REQ_FUA)
  1167. cdbh->options |= OSD_CDB_FUA;
  1168. if (options & OSD_REQ_DPO)
  1169. cdbh->options |= OSD_CDB_DPO;
  1170. if (options & OSD_REQ_BYPASS_TIMESTAMPS)
  1171. cdbh->timestamp_control = OSD_CDB_BYPASS_TIMESTAMPS;
  1172. osd_set_caps(&or->cdb, cap);
  1173. has_in = or->in.bio || or->get_attr.total_bytes;
  1174. has_out = or->out.bio || or->set_attr.total_bytes ||
  1175. or->enc_get_attr.total_bytes;
  1176. ret = _init_blk_request(or, has_in, has_out);
  1177. if (ret) {
  1178. OSD_DEBUG("_init_blk_request failed\n");
  1179. return ret;
  1180. }
  1181. or->out.pad_buff = sg_out_pad_buffer;
  1182. or->in.pad_buff = sg_in_pad_buffer;
  1183. if (!or->attributes_mode)
  1184. or->attributes_mode = OSD_CDB_GET_SET_ATTR_LISTS;
  1185. cdbh->command_specific_options |= or->attributes_mode;
  1186. if (or->attributes_mode == OSD_CDB_GET_ATTR_PAGE_SET_ONE) {
  1187. ret = _osd_req_finalize_attr_page(or);
  1188. if (ret) {
  1189. OSD_DEBUG("_osd_req_finalize_attr_page failed\n");
  1190. return ret;
  1191. }
  1192. } else {
  1193. /* TODO: I think that for the GET_ATTR command these 2 should
  1194. * be reversed to keep them in execution order (for embeded
  1195. * targets with low memory footprint)
  1196. */
  1197. ret = _osd_req_finalize_set_attr_list(or);
  1198. if (ret) {
  1199. OSD_DEBUG("_osd_req_finalize_set_attr_list failed\n");
  1200. return ret;
  1201. }
  1202. ret = _osd_req_finalize_get_attr_list(or);
  1203. if (ret) {
  1204. OSD_DEBUG("_osd_req_finalize_get_attr_list failed\n");
  1205. return ret;
  1206. }
  1207. }
  1208. ret = _osd_req_finalize_data_integrity(or, has_in, has_out,
  1209. out_data_bytes, cap_key);
  1210. if (ret)
  1211. return ret;
  1212. osd_sec_sign_cdb(&or->cdb, cap_key);
  1213. or->request->cmd = or->cdb.buff;
  1214. or->request->cmd_len = _osd_req_cdb_len(or);
  1215. return 0;
  1216. }
  1217. EXPORT_SYMBOL(osd_finalize_request);
  1218. static bool _is_osd_security_code(int code)
  1219. {
  1220. return (code == osd_security_audit_value_frozen) ||
  1221. (code == osd_security_working_key_frozen) ||
  1222. (code == osd_nonce_not_unique) ||
  1223. (code == osd_nonce_timestamp_out_of_range) ||
  1224. (code == osd_invalid_dataout_buffer_integrity_check_value);
  1225. }
  1226. #define OSD_SENSE_PRINT1(fmt, a...) \
  1227. do { \
  1228. if (__cur_sense_need_output) \
  1229. OSD_ERR(fmt, ##a); \
  1230. } while (0)
  1231. #define OSD_SENSE_PRINT2(fmt, a...) OSD_SENSE_PRINT1(" " fmt, ##a)
  1232. int osd_req_decode_sense_full(struct osd_request *or,
  1233. struct osd_sense_info *osi, bool silent,
  1234. struct osd_obj_id *bad_obj_list __unused, int max_obj __unused,
  1235. struct osd_attr *bad_attr_list, int max_attr)
  1236. {
  1237. int sense_len, original_sense_len;
  1238. struct osd_sense_info local_osi;
  1239. struct scsi_sense_descriptor_based *ssdb;
  1240. void *cur_descriptor;
  1241. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 0)
  1242. const bool __cur_sense_need_output = false;
  1243. #else
  1244. bool __cur_sense_need_output = !silent;
  1245. #endif
  1246. int ret;
  1247. if (likely(!or->req_errors))
  1248. return 0;
  1249. osi = osi ? : &local_osi;
  1250. memset(osi, 0, sizeof(*osi));
  1251. ssdb = (typeof(ssdb))or->sense;
  1252. sense_len = or->sense_len;
  1253. if ((sense_len < (int)sizeof(*ssdb) || !ssdb->sense_key)) {
  1254. OSD_ERR("Block-layer returned error(0x%x) but "
  1255. "sense_len(%u) || key(%d) is empty\n",
  1256. or->req_errors, sense_len, ssdb->sense_key);
  1257. goto analyze;
  1258. }
  1259. if ((ssdb->response_code != 0x72) && (ssdb->response_code != 0x73)) {
  1260. OSD_ERR("Unrecognized scsi sense: rcode=%x length=%d\n",
  1261. ssdb->response_code, sense_len);
  1262. goto analyze;
  1263. }
  1264. osi->key = ssdb->sense_key;
  1265. osi->additional_code = be16_to_cpu(ssdb->additional_sense_code);
  1266. original_sense_len = ssdb->additional_sense_length + 8;
  1267. #if (CONFIG_SCSI_OSD_DPRINT_SENSE == 1)
  1268. if (__cur_sense_need_output)
  1269. __cur_sense_need_output = (osi->key > scsi_sk_recovered_error);
  1270. #endif
  1271. OSD_SENSE_PRINT1("Main Sense information key=0x%x length(%d, %d) "
  1272. "additional_code=0x%x async_error=%d errors=0x%x\n",
  1273. osi->key, original_sense_len, sense_len,
  1274. osi->additional_code, or->async_error,
  1275. or->req_errors);
  1276. if (original_sense_len < sense_len)
  1277. sense_len = original_sense_len;
  1278. cur_descriptor = ssdb->ssd;
  1279. sense_len -= sizeof(*ssdb);
  1280. while (sense_len > 0) {
  1281. struct scsi_sense_descriptor *ssd = cur_descriptor;
  1282. int cur_len = ssd->additional_length + 2;
  1283. sense_len -= cur_len;
  1284. if (sense_len < 0)
  1285. break; /* sense was truncated */
  1286. switch (ssd->descriptor_type) {
  1287. case scsi_sense_information:
  1288. case scsi_sense_command_specific_information:
  1289. {
  1290. struct scsi_sense_command_specific_data_descriptor
  1291. *sscd = cur_descriptor;
  1292. osi->command_info =
  1293. get_unaligned_be64(&sscd->information) ;
  1294. OSD_SENSE_PRINT2(
  1295. "command_specific_information 0x%llx \n",
  1296. _LLU(osi->command_info));
  1297. break;
  1298. }
  1299. case scsi_sense_key_specific:
  1300. {
  1301. struct scsi_sense_key_specific_data_descriptor
  1302. *ssks = cur_descriptor;
  1303. osi->sense_info = get_unaligned_be16(&ssks->value);
  1304. OSD_SENSE_PRINT2(
  1305. "sense_key_specific_information %u"
  1306. "sksv_cd_bpv_bp (0x%x)\n",
  1307. osi->sense_info, ssks->sksv_cd_bpv_bp);
  1308. break;
  1309. }
  1310. case osd_sense_object_identification:
  1311. { /*FIXME: Keep first not last, Store in array*/
  1312. struct osd_sense_identification_data_descriptor
  1313. *osidd = cur_descriptor;
  1314. osi->not_initiated_command_functions =
  1315. le32_to_cpu(osidd->not_initiated_functions);
  1316. osi->completed_command_functions =
  1317. le32_to_cpu(osidd->completed_functions);
  1318. osi->obj.partition = be64_to_cpu(osidd->partition_id);
  1319. osi->obj.id = be64_to_cpu(osidd->object_id);
  1320. OSD_SENSE_PRINT2(
  1321. "object_identification pid=0x%llx oid=0x%llx\n",
  1322. _LLU(osi->obj.partition), _LLU(osi->obj.id));
  1323. OSD_SENSE_PRINT2(
  1324. "not_initiated_bits(%x) "
  1325. "completed_command_bits(%x)\n",
  1326. osi->not_initiated_command_functions,
  1327. osi->completed_command_functions);
  1328. break;
  1329. }
  1330. case osd_sense_response_integrity_check:
  1331. {
  1332. struct osd_sense_response_integrity_check_descriptor
  1333. *osricd = cur_descriptor;
  1334. const unsigned len =
  1335. sizeof(osricd->integrity_check_value);
  1336. char key_dump[len*4 + 2]; /* 2nibbles+space+ASCII */
  1337. hex_dump_to_buffer(osricd->integrity_check_value, len,
  1338. 32, 1, key_dump, sizeof(key_dump), true);
  1339. OSD_SENSE_PRINT2("response_integrity [%s]\n", key_dump);
  1340. }
  1341. case osd_sense_attribute_identification:
  1342. {
  1343. struct osd_sense_attributes_data_descriptor
  1344. *osadd = cur_descriptor;
  1345. unsigned len = min(cur_len, sense_len);
  1346. struct osd_sense_attr *pattr = osadd->sense_attrs;
  1347. while (len >= sizeof(*pattr)) {
  1348. u32 attr_page = be32_to_cpu(pattr->attr_page);
  1349. u32 attr_id = be32_to_cpu(pattr->attr_id);
  1350. if (!osi->attr.attr_page) {
  1351. osi->attr.attr_page = attr_page;
  1352. osi->attr.attr_id = attr_id;
  1353. }
  1354. if (bad_attr_list && max_attr) {
  1355. bad_attr_list->attr_page = attr_page;
  1356. bad_attr_list->attr_id = attr_id;
  1357. bad_attr_list++;
  1358. max_attr--;
  1359. }
  1360. len -= sizeof(*pattr);
  1361. OSD_SENSE_PRINT2(
  1362. "osd_sense_attribute_identification"
  1363. "attr_page=0x%x attr_id=0x%x\n",
  1364. attr_page, attr_id);
  1365. }
  1366. }
  1367. /*These are not legal for OSD*/
  1368. case scsi_sense_field_replaceable_unit:
  1369. OSD_SENSE_PRINT2("scsi_sense_field_replaceable_unit\n");
  1370. break;
  1371. case scsi_sense_stream_commands:
  1372. OSD_SENSE_PRINT2("scsi_sense_stream_commands\n");
  1373. break;
  1374. case scsi_sense_block_commands:
  1375. OSD_SENSE_PRINT2("scsi_sense_block_commands\n");
  1376. break;
  1377. case scsi_sense_ata_return:
  1378. OSD_SENSE_PRINT2("scsi_sense_ata_return\n");
  1379. break;
  1380. default:
  1381. if (ssd->descriptor_type <= scsi_sense_Reserved_last)
  1382. OSD_SENSE_PRINT2(
  1383. "scsi_sense Reserved descriptor (0x%x)",
  1384. ssd->descriptor_type);
  1385. else
  1386. OSD_SENSE_PRINT2(
  1387. "scsi_sense Vendor descriptor (0x%x)",
  1388. ssd->descriptor_type);
  1389. }
  1390. cur_descriptor += cur_len;
  1391. }
  1392. analyze:
  1393. if (!osi->key) {
  1394. /* scsi sense is Empty, the request was never issued to target
  1395. * linux return code might tell us what happened.
  1396. */
  1397. if (or->async_error == -ENOMEM)
  1398. osi->osd_err_pri = OSD_ERR_PRI_RESOURCE;
  1399. else
  1400. osi->osd_err_pri = OSD_ERR_PRI_UNREACHABLE;
  1401. ret = or->async_error;
  1402. } else if (osi->key <= scsi_sk_recovered_error) {
  1403. osi->osd_err_pri = 0;
  1404. ret = 0;
  1405. } else if (osi->additional_code == scsi_invalid_field_in_cdb) {
  1406. if (osi->cdb_field_offset == OSD_CFO_STARTING_BYTE) {
  1407. osi->osd_err_pri = OSD_ERR_PRI_CLEAR_PAGES;
  1408. ret = -EFAULT; /* caller should recover from this */
  1409. } else if (osi->cdb_field_offset == OSD_CFO_OBJECT_ID) {
  1410. osi->osd_err_pri = OSD_ERR_PRI_NOT_FOUND;
  1411. ret = -ENOENT;
  1412. } else if (osi->cdb_field_offset == OSD_CFO_PERMISSIONS) {
  1413. osi->osd_err_pri = OSD_ERR_PRI_NO_ACCESS;
  1414. ret = -EACCES;
  1415. } else {
  1416. osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
  1417. ret = -EINVAL;
  1418. }
  1419. } else if (osi->additional_code == osd_quota_error) {
  1420. osi->osd_err_pri = OSD_ERR_PRI_NO_SPACE;
  1421. ret = -ENOSPC;
  1422. } else if (_is_osd_security_code(osi->additional_code)) {
  1423. osi->osd_err_pri = OSD_ERR_PRI_BAD_CRED;
  1424. ret = -EINVAL;
  1425. } else {
  1426. osi->osd_err_pri = OSD_ERR_PRI_EIO;
  1427. ret = -EIO;
  1428. }
  1429. if (!or->out.residual)
  1430. or->out.residual = or->out.total_bytes;
  1431. if (!or->in.residual)
  1432. or->in.residual = or->in.total_bytes;
  1433. return ret;
  1434. }
  1435. EXPORT_SYMBOL(osd_req_decode_sense_full);
  1436. /*
  1437. * Implementation of osd_sec.h API
  1438. * TODO: Move to a separate osd_sec.c file at a later stage.
  1439. */
  1440. enum { OSD_SEC_CAP_V1_ALL_CAPS =
  1441. OSD_SEC_CAP_APPEND | OSD_SEC_CAP_OBJ_MGMT | OSD_SEC_CAP_REMOVE |
  1442. OSD_SEC_CAP_CREATE | OSD_SEC_CAP_SET_ATTR | OSD_SEC_CAP_GET_ATTR |
  1443. OSD_SEC_CAP_WRITE | OSD_SEC_CAP_READ | OSD_SEC_CAP_POL_SEC |
  1444. OSD_SEC_CAP_GLOBAL | OSD_SEC_CAP_DEV_MGMT
  1445. };
  1446. enum { OSD_SEC_CAP_V2_ALL_CAPS =
  1447. OSD_SEC_CAP_V1_ALL_CAPS | OSD_SEC_CAP_QUERY | OSD_SEC_CAP_M_OBJECT
  1448. };
  1449. void osd_sec_init_nosec_doall_caps(void *caps,
  1450. const struct osd_obj_id *obj, bool is_collection, const bool is_v1)
  1451. {
  1452. struct osd_capability *cap = caps;
  1453. u8 type;
  1454. u8 descriptor_type;
  1455. if (likely(obj->id)) {
  1456. if (unlikely(is_collection)) {
  1457. type = OSD_SEC_OBJ_COLLECTION;
  1458. descriptor_type = is_v1 ? OSD_SEC_OBJ_DESC_OBJ :
  1459. OSD_SEC_OBJ_DESC_COL;
  1460. } else {
  1461. type = OSD_SEC_OBJ_USER;
  1462. descriptor_type = OSD_SEC_OBJ_DESC_OBJ;
  1463. }
  1464. WARN_ON(!obj->partition);
  1465. } else {
  1466. type = obj->partition ? OSD_SEC_OBJ_PARTITION :
  1467. OSD_SEC_OBJ_ROOT;
  1468. descriptor_type = OSD_SEC_OBJ_DESC_PAR;
  1469. }
  1470. memset(cap, 0, sizeof(*cap));
  1471. cap->h.format = OSD_SEC_CAP_FORMAT_VER1;
  1472. cap->h.integrity_algorithm__key_version = 0; /* MAKE_BYTE(0, 0); */
  1473. cap->h.security_method = OSD_SEC_NOSEC;
  1474. /* cap->expiration_time;
  1475. cap->AUDIT[30-10];
  1476. cap->discriminator[42-30];
  1477. cap->object_created_time; */
  1478. cap->h.object_type = type;
  1479. osd_sec_set_caps(&cap->h, OSD_SEC_CAP_V1_ALL_CAPS);
  1480. cap->h.object_descriptor_type = descriptor_type;
  1481. cap->od.obj_desc.policy_access_tag = 0;
  1482. cap->od.obj_desc.allowed_partition_id = cpu_to_be64(obj->partition);
  1483. cap->od.obj_desc.allowed_object_id = cpu_to_be64(obj->id);
  1484. }
  1485. EXPORT_SYMBOL(osd_sec_init_nosec_doall_caps);
  1486. /* FIXME: Extract version from caps pointer.
  1487. * Also Pete's target only supports caps from OSDv1 for now
  1488. */
  1489. void osd_set_caps(struct osd_cdb *cdb, const void *caps)
  1490. {
  1491. bool is_ver1 = true;
  1492. /* NOTE: They start at same address */
  1493. memcpy(&cdb->v1.caps, caps, is_ver1 ? OSDv1_CAP_LEN : OSD_CAP_LEN);
  1494. }
  1495. bool osd_is_sec_alldata(struct osd_security_parameters *sec_parms __unused)
  1496. {
  1497. return false;
  1498. }
  1499. void osd_sec_sign_cdb(struct osd_cdb *ocdb __unused, const u8 *cap_key __unused)
  1500. {
  1501. }
  1502. void osd_sec_sign_data(void *data_integ __unused,
  1503. struct bio *bio __unused, const u8 *cap_key __unused)
  1504. {
  1505. }
  1506. /*
  1507. * Declared in osd_protocol.h
  1508. * 4.12.5 Data-In and Data-Out buffer offsets
  1509. * byte offset = mantissa * (2^(exponent+8))
  1510. * Returns the smallest allowed encoded offset that contains given @offset
  1511. * The actual encoded offset returned is @offset + *@padding.
  1512. */
  1513. osd_cdb_offset __osd_encode_offset(
  1514. u64 offset, unsigned *padding, int min_shift, int max_shift)
  1515. {
  1516. u64 try_offset = -1, mod, align;
  1517. osd_cdb_offset be32_offset;
  1518. int shift;
  1519. *padding = 0;
  1520. if (!offset)
  1521. return 0;
  1522. for (shift = min_shift; shift < max_shift; ++shift) {
  1523. try_offset = offset >> shift;
  1524. if (try_offset < (1 << OSD_OFFSET_MAX_BITS))
  1525. break;
  1526. }
  1527. BUG_ON(shift == max_shift);
  1528. align = 1 << shift;
  1529. mod = offset & (align - 1);
  1530. if (mod) {
  1531. *padding = align - mod;
  1532. try_offset += 1;
  1533. }
  1534. try_offset |= ((shift - 8) & 0xf) << 28;
  1535. be32_offset = cpu_to_be32((u32)try_offset);
  1536. OSD_DEBUG("offset=%llu mantissa=%llu exp=%d encoded=%x pad=%d\n",
  1537. _LLU(offset), _LLU(try_offset & 0x0FFFFFFF), shift,
  1538. be32_offset, *padding);
  1539. return be32_offset;
  1540. }