osd_initiator.c 46 KB

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