pnfs_osd_xdr_cli.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Object-Based pNFS Layout XDR layer
  3. *
  4. * Copyright (C) 2007 Panasas Inc. [year of first publication]
  5. * All rights reserved.
  6. *
  7. * Benny Halevy <bhalevy@panasas.com>
  8. * Boaz Harrosh <ooo@electrozaur.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * See the file COPYING included with this distribution for more details.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 3. Neither the name of the Panasas company nor the names of its
  24. * contributors may be used to endorse or promote products derived
  25. * from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  28. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  29. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  34. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #include <linux/pnfs_osd_xdr.h>
  40. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  41. /*
  42. * The following implementation is based on RFC5664
  43. */
  44. /*
  45. * struct pnfs_osd_objid {
  46. * struct nfs4_deviceid oid_device_id;
  47. * u64 oid_partition_id;
  48. * u64 oid_object_id;
  49. * }; // xdr size 32 bytes
  50. */
  51. static __be32 *
  52. _osd_xdr_decode_objid(__be32 *p, struct pnfs_osd_objid *objid)
  53. {
  54. p = xdr_decode_opaque_fixed(p, objid->oid_device_id.data,
  55. sizeof(objid->oid_device_id.data));
  56. p = xdr_decode_hyper(p, &objid->oid_partition_id);
  57. p = xdr_decode_hyper(p, &objid->oid_object_id);
  58. return p;
  59. }
  60. /*
  61. * struct pnfs_osd_opaque_cred {
  62. * u32 cred_len;
  63. * void *cred;
  64. * }; // xdr size [variable]
  65. * The return pointers are from the xdr buffer
  66. */
  67. static int
  68. _osd_xdr_decode_opaque_cred(struct pnfs_osd_opaque_cred *opaque_cred,
  69. struct xdr_stream *xdr)
  70. {
  71. __be32 *p = xdr_inline_decode(xdr, 1);
  72. if (!p)
  73. return -EINVAL;
  74. opaque_cred->cred_len = be32_to_cpu(*p++);
  75. p = xdr_inline_decode(xdr, opaque_cred->cred_len);
  76. if (!p)
  77. return -EINVAL;
  78. opaque_cred->cred = p;
  79. return 0;
  80. }
  81. /*
  82. * struct pnfs_osd_object_cred {
  83. * struct pnfs_osd_objid oc_object_id;
  84. * u32 oc_osd_version;
  85. * u32 oc_cap_key_sec;
  86. * struct pnfs_osd_opaque_cred oc_cap_key
  87. * struct pnfs_osd_opaque_cred oc_cap;
  88. * }; // xdr size 32 + 4 + 4 + [variable] + [variable]
  89. */
  90. static int
  91. _osd_xdr_decode_object_cred(struct pnfs_osd_object_cred *comp,
  92. struct xdr_stream *xdr)
  93. {
  94. __be32 *p = xdr_inline_decode(xdr, 32 + 4 + 4);
  95. int ret;
  96. if (!p)
  97. return -EIO;
  98. p = _osd_xdr_decode_objid(p, &comp->oc_object_id);
  99. comp->oc_osd_version = be32_to_cpup(p++);
  100. comp->oc_cap_key_sec = be32_to_cpup(p);
  101. ret = _osd_xdr_decode_opaque_cred(&comp->oc_cap_key, xdr);
  102. if (unlikely(ret))
  103. return ret;
  104. ret = _osd_xdr_decode_opaque_cred(&comp->oc_cap, xdr);
  105. return ret;
  106. }
  107. /*
  108. * struct pnfs_osd_data_map {
  109. * u32 odm_num_comps;
  110. * u64 odm_stripe_unit;
  111. * u32 odm_group_width;
  112. * u32 odm_group_depth;
  113. * u32 odm_mirror_cnt;
  114. * u32 odm_raid_algorithm;
  115. * }; // xdr size 4 + 8 + 4 + 4 + 4 + 4
  116. */
  117. static inline int
  118. _osd_data_map_xdr_sz(void)
  119. {
  120. return 4 + 8 + 4 + 4 + 4 + 4;
  121. }
  122. static __be32 *
  123. _osd_xdr_decode_data_map(__be32 *p, struct pnfs_osd_data_map *data_map)
  124. {
  125. data_map->odm_num_comps = be32_to_cpup(p++);
  126. p = xdr_decode_hyper(p, &data_map->odm_stripe_unit);
  127. data_map->odm_group_width = be32_to_cpup(p++);
  128. data_map->odm_group_depth = be32_to_cpup(p++);
  129. data_map->odm_mirror_cnt = be32_to_cpup(p++);
  130. data_map->odm_raid_algorithm = be32_to_cpup(p++);
  131. dprintk("%s: odm_num_comps=%u odm_stripe_unit=%llu odm_group_width=%u "
  132. "odm_group_depth=%u odm_mirror_cnt=%u odm_raid_algorithm=%u\n",
  133. __func__,
  134. data_map->odm_num_comps,
  135. (unsigned long long)data_map->odm_stripe_unit,
  136. data_map->odm_group_width,
  137. data_map->odm_group_depth,
  138. data_map->odm_mirror_cnt,
  139. data_map->odm_raid_algorithm);
  140. return p;
  141. }
  142. int pnfs_osd_xdr_decode_layout_map(struct pnfs_osd_layout *layout,
  143. struct pnfs_osd_xdr_decode_layout_iter *iter, struct xdr_stream *xdr)
  144. {
  145. __be32 *p;
  146. memset(iter, 0, sizeof(*iter));
  147. p = xdr_inline_decode(xdr, _osd_data_map_xdr_sz() + 4 + 4);
  148. if (unlikely(!p))
  149. return -EINVAL;
  150. p = _osd_xdr_decode_data_map(p, &layout->olo_map);
  151. layout->olo_comps_index = be32_to_cpup(p++);
  152. layout->olo_num_comps = be32_to_cpup(p++);
  153. dprintk("%s: olo_comps_index=%d olo_num_comps=%d\n", __func__,
  154. layout->olo_comps_index, layout->olo_num_comps);
  155. iter->total_comps = layout->olo_num_comps;
  156. return 0;
  157. }
  158. bool pnfs_osd_xdr_decode_layout_comp(struct pnfs_osd_object_cred *comp,
  159. struct pnfs_osd_xdr_decode_layout_iter *iter, struct xdr_stream *xdr,
  160. int *err)
  161. {
  162. BUG_ON(iter->decoded_comps > iter->total_comps);
  163. if (iter->decoded_comps == iter->total_comps)
  164. return false;
  165. *err = _osd_xdr_decode_object_cred(comp, xdr);
  166. if (unlikely(*err)) {
  167. dprintk("%s: _osd_xdr_decode_object_cred=>%d decoded_comps=%d "
  168. "total_comps=%d\n", __func__, *err,
  169. iter->decoded_comps, iter->total_comps);
  170. return false; /* stop the loop */
  171. }
  172. dprintk("%s: dev(%llx:%llx) par=0x%llx obj=0x%llx "
  173. "key_len=%u cap_len=%u\n",
  174. __func__,
  175. _DEVID_LO(&comp->oc_object_id.oid_device_id),
  176. _DEVID_HI(&comp->oc_object_id.oid_device_id),
  177. comp->oc_object_id.oid_partition_id,
  178. comp->oc_object_id.oid_object_id,
  179. comp->oc_cap_key.cred_len, comp->oc_cap.cred_len);
  180. iter->decoded_comps++;
  181. return true;
  182. }
  183. /*
  184. * Get Device Information Decoding
  185. *
  186. * Note: since Device Information is currently done synchronously, all
  187. * variable strings fields are left inside the rpc buffer and are only
  188. * pointed to by the pnfs_osd_deviceaddr members. So the read buffer
  189. * should not be freed while the returned information is in use.
  190. */
  191. /*
  192. *struct nfs4_string {
  193. * unsigned int len;
  194. * char *data;
  195. *}; // size [variable]
  196. * NOTE: Returned string points to inside the XDR buffer
  197. */
  198. static __be32 *
  199. __read_u8_opaque(__be32 *p, struct nfs4_string *str)
  200. {
  201. str->len = be32_to_cpup(p++);
  202. str->data = (char *)p;
  203. p += XDR_QUADLEN(str->len);
  204. return p;
  205. }
  206. /*
  207. * struct pnfs_osd_targetid {
  208. * u32 oti_type;
  209. * struct nfs4_string oti_scsi_device_id;
  210. * };// size 4 + [variable]
  211. */
  212. static __be32 *
  213. __read_targetid(__be32 *p, struct pnfs_osd_targetid* targetid)
  214. {
  215. u32 oti_type;
  216. oti_type = be32_to_cpup(p++);
  217. targetid->oti_type = oti_type;
  218. switch (oti_type) {
  219. case OBJ_TARGET_SCSI_NAME:
  220. case OBJ_TARGET_SCSI_DEVICE_ID:
  221. p = __read_u8_opaque(p, &targetid->oti_scsi_device_id);
  222. }
  223. return p;
  224. }
  225. /*
  226. * struct pnfs_osd_net_addr {
  227. * struct nfs4_string r_netid;
  228. * struct nfs4_string r_addr;
  229. * };
  230. */
  231. static __be32 *
  232. __read_net_addr(__be32 *p, struct pnfs_osd_net_addr* netaddr)
  233. {
  234. p = __read_u8_opaque(p, &netaddr->r_netid);
  235. p = __read_u8_opaque(p, &netaddr->r_addr);
  236. return p;
  237. }
  238. /*
  239. * struct pnfs_osd_targetaddr {
  240. * u32 ota_available;
  241. * struct pnfs_osd_net_addr ota_netaddr;
  242. * };
  243. */
  244. static __be32 *
  245. __read_targetaddr(__be32 *p, struct pnfs_osd_targetaddr *targetaddr)
  246. {
  247. u32 ota_available;
  248. ota_available = be32_to_cpup(p++);
  249. targetaddr->ota_available = ota_available;
  250. if (ota_available)
  251. p = __read_net_addr(p, &targetaddr->ota_netaddr);
  252. return p;
  253. }
  254. /*
  255. * struct pnfs_osd_deviceaddr {
  256. * struct pnfs_osd_targetid oda_targetid;
  257. * struct pnfs_osd_targetaddr oda_targetaddr;
  258. * u8 oda_lun[8];
  259. * struct nfs4_string oda_systemid;
  260. * struct pnfs_osd_object_cred oda_root_obj_cred;
  261. * struct nfs4_string oda_osdname;
  262. * };
  263. */
  264. /* We need this version for the pnfs_osd_xdr_decode_deviceaddr which does
  265. * not have an xdr_stream
  266. */
  267. static __be32 *
  268. __read_opaque_cred(__be32 *p,
  269. struct pnfs_osd_opaque_cred *opaque_cred)
  270. {
  271. opaque_cred->cred_len = be32_to_cpu(*p++);
  272. opaque_cred->cred = p;
  273. return p + XDR_QUADLEN(opaque_cred->cred_len);
  274. }
  275. static __be32 *
  276. __read_object_cred(__be32 *p, struct pnfs_osd_object_cred *comp)
  277. {
  278. p = _osd_xdr_decode_objid(p, &comp->oc_object_id);
  279. comp->oc_osd_version = be32_to_cpup(p++);
  280. comp->oc_cap_key_sec = be32_to_cpup(p++);
  281. p = __read_opaque_cred(p, &comp->oc_cap_key);
  282. p = __read_opaque_cred(p, &comp->oc_cap);
  283. return p;
  284. }
  285. void pnfs_osd_xdr_decode_deviceaddr(
  286. struct pnfs_osd_deviceaddr *deviceaddr, __be32 *p)
  287. {
  288. p = __read_targetid(p, &deviceaddr->oda_targetid);
  289. p = __read_targetaddr(p, &deviceaddr->oda_targetaddr);
  290. p = xdr_decode_opaque_fixed(p, deviceaddr->oda_lun,
  291. sizeof(deviceaddr->oda_lun));
  292. p = __read_u8_opaque(p, &deviceaddr->oda_systemid);
  293. p = __read_object_cred(p, &deviceaddr->oda_root_obj_cred);
  294. p = __read_u8_opaque(p, &deviceaddr->oda_osdname);
  295. /* libosd likes this terminated in dbg. It's last, so no problems */
  296. deviceaddr->oda_osdname.data[deviceaddr->oda_osdname.len] = 0;
  297. }
  298. /*
  299. * struct pnfs_osd_layoutupdate {
  300. * u32 dsu_valid;
  301. * s64 dsu_delta;
  302. * u32 olu_ioerr_flag;
  303. * }; xdr size 4 + 8 + 4
  304. */
  305. int
  306. pnfs_osd_xdr_encode_layoutupdate(struct xdr_stream *xdr,
  307. struct pnfs_osd_layoutupdate *lou)
  308. {
  309. __be32 *p = xdr_reserve_space(xdr, 4 + 8 + 4);
  310. if (!p)
  311. return -E2BIG;
  312. *p++ = cpu_to_be32(lou->dsu_valid);
  313. if (lou->dsu_valid)
  314. p = xdr_encode_hyper(p, lou->dsu_delta);
  315. *p++ = cpu_to_be32(lou->olu_ioerr_flag);
  316. return 0;
  317. }
  318. /*
  319. * struct pnfs_osd_objid {
  320. * struct nfs4_deviceid oid_device_id;
  321. * u64 oid_partition_id;
  322. * u64 oid_object_id;
  323. * }; // xdr size 32 bytes
  324. */
  325. static inline __be32 *
  326. pnfs_osd_xdr_encode_objid(__be32 *p, struct pnfs_osd_objid *object_id)
  327. {
  328. p = xdr_encode_opaque_fixed(p, &object_id->oid_device_id.data,
  329. sizeof(object_id->oid_device_id.data));
  330. p = xdr_encode_hyper(p, object_id->oid_partition_id);
  331. p = xdr_encode_hyper(p, object_id->oid_object_id);
  332. return p;
  333. }
  334. /*
  335. * struct pnfs_osd_ioerr {
  336. * struct pnfs_osd_objid oer_component;
  337. * u64 oer_comp_offset;
  338. * u64 oer_comp_length;
  339. * u32 oer_iswrite;
  340. * u32 oer_errno;
  341. * }; // xdr size 32 + 24 bytes
  342. */
  343. void pnfs_osd_xdr_encode_ioerr(__be32 *p, struct pnfs_osd_ioerr *ioerr)
  344. {
  345. p = pnfs_osd_xdr_encode_objid(p, &ioerr->oer_component);
  346. p = xdr_encode_hyper(p, ioerr->oer_comp_offset);
  347. p = xdr_encode_hyper(p, ioerr->oer_comp_length);
  348. *p++ = cpu_to_be32(ioerr->oer_iswrite);
  349. *p = cpu_to_be32(ioerr->oer_errno);
  350. }
  351. __be32 *pnfs_osd_xdr_ioerr_reserve_space(struct xdr_stream *xdr)
  352. {
  353. __be32 *p;
  354. p = xdr_reserve_space(xdr, 32 + 24);
  355. if (unlikely(!p))
  356. dprintk("%s: out of xdr space\n", __func__);
  357. return p;
  358. }