objio_osd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * pNFS Objects layout implementation over open-osd initiator library
  3. *
  4. * Copyright (C) 2009 Panasas Inc. [year of first publication]
  5. * All rights reserved.
  6. *
  7. * Benny Halevy <bhalevy@panasas.com>
  8. * Boaz Harrosh <bharrosh@panasas.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/module.h>
  40. #include <scsi/osd_initiator.h>
  41. #include "objlayout.h"
  42. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  43. #define _LLU(x) ((unsigned long long)x)
  44. struct objio_dev_ent {
  45. struct nfs4_deviceid_node id_node;
  46. struct osd_dev *od;
  47. };
  48. static void
  49. objio_free_deviceid_node(struct nfs4_deviceid_node *d)
  50. {
  51. struct objio_dev_ent *de = container_of(d, struct objio_dev_ent, id_node);
  52. dprintk("%s: free od=%p\n", __func__, de->od);
  53. osduld_put_device(de->od);
  54. kfree(de);
  55. }
  56. static struct objio_dev_ent *_dev_list_find(const struct nfs_server *nfss,
  57. const struct nfs4_deviceid *d_id)
  58. {
  59. struct nfs4_deviceid_node *d;
  60. struct objio_dev_ent *de;
  61. d = nfs4_find_get_deviceid(nfss->pnfs_curr_ld, nfss->nfs_client, d_id);
  62. if (!d)
  63. return NULL;
  64. de = container_of(d, struct objio_dev_ent, id_node);
  65. return de;
  66. }
  67. static struct objio_dev_ent *
  68. _dev_list_add(const struct nfs_server *nfss,
  69. const struct nfs4_deviceid *d_id, struct osd_dev *od,
  70. gfp_t gfp_flags)
  71. {
  72. struct nfs4_deviceid_node *d;
  73. struct objio_dev_ent *de = kzalloc(sizeof(*de), gfp_flags);
  74. struct objio_dev_ent *n;
  75. if (!de) {
  76. dprintk("%s: -ENOMEM od=%p\n", __func__, od);
  77. return NULL;
  78. }
  79. dprintk("%s: Adding od=%p\n", __func__, od);
  80. nfs4_init_deviceid_node(&de->id_node,
  81. nfss->pnfs_curr_ld,
  82. nfss->nfs_client,
  83. d_id);
  84. de->od = od;
  85. d = nfs4_insert_deviceid_node(&de->id_node);
  86. n = container_of(d, struct objio_dev_ent, id_node);
  87. if (n != de) {
  88. dprintk("%s: Race with other n->od=%p\n", __func__, n->od);
  89. objio_free_deviceid_node(&de->id_node);
  90. de = n;
  91. }
  92. atomic_inc(&de->id_node.ref);
  93. return de;
  94. }
  95. struct caps_buffers {
  96. u8 caps_key[OSD_CRYPTO_KEYID_SIZE];
  97. u8 creds[OSD_CAP_LEN];
  98. };
  99. struct objio_segment {
  100. struct pnfs_layout_segment lseg;
  101. struct pnfs_osd_object_cred *comps;
  102. unsigned mirrors_p1;
  103. unsigned stripe_unit;
  104. unsigned group_width; /* Data stripe_units without integrity comps */
  105. u64 group_depth;
  106. unsigned group_count;
  107. unsigned comps_index;
  108. unsigned num_comps;
  109. /* variable length */
  110. struct objio_dev_ent *ods[];
  111. };
  112. static inline struct objio_segment *
  113. OBJIO_LSEG(struct pnfs_layout_segment *lseg)
  114. {
  115. return container_of(lseg, struct objio_segment, lseg);
  116. }
  117. /* Send and wait for a get_device_info of devices in the layout,
  118. then look them up with the osd_initiator library */
  119. static struct objio_dev_ent *_device_lookup(struct pnfs_layout_hdr *pnfslay,
  120. struct objio_segment *objio_seg, unsigned comp,
  121. gfp_t gfp_flags)
  122. {
  123. struct pnfs_osd_deviceaddr *deviceaddr;
  124. struct nfs4_deviceid *d_id;
  125. struct objio_dev_ent *ode;
  126. struct osd_dev *od;
  127. struct osd_dev_info odi;
  128. int err;
  129. d_id = &objio_seg->comps[comp].oc_object_id.oid_device_id;
  130. ode = _dev_list_find(NFS_SERVER(pnfslay->plh_inode), d_id);
  131. if (ode)
  132. return ode;
  133. err = objlayout_get_deviceinfo(pnfslay, d_id, &deviceaddr, gfp_flags);
  134. if (unlikely(err)) {
  135. dprintk("%s: objlayout_get_deviceinfo dev(%llx:%llx) =>%d\n",
  136. __func__, _DEVID_LO(d_id), _DEVID_HI(d_id), err);
  137. return ERR_PTR(err);
  138. }
  139. odi.systemid_len = deviceaddr->oda_systemid.len;
  140. if (odi.systemid_len > sizeof(odi.systemid)) {
  141. err = -EINVAL;
  142. goto out;
  143. } else if (odi.systemid_len)
  144. memcpy(odi.systemid, deviceaddr->oda_systemid.data,
  145. odi.systemid_len);
  146. odi.osdname_len = deviceaddr->oda_osdname.len;
  147. odi.osdname = (u8 *)deviceaddr->oda_osdname.data;
  148. if (!odi.osdname_len && !odi.systemid_len) {
  149. dprintk("%s: !odi.osdname_len && !odi.systemid_len\n",
  150. __func__);
  151. err = -ENODEV;
  152. goto out;
  153. }
  154. od = osduld_info_lookup(&odi);
  155. if (unlikely(IS_ERR(od))) {
  156. err = PTR_ERR(od);
  157. dprintk("%s: osduld_info_lookup => %d\n", __func__, err);
  158. goto out;
  159. }
  160. ode = _dev_list_add(NFS_SERVER(pnfslay->plh_inode), d_id, od,
  161. gfp_flags);
  162. out:
  163. dprintk("%s: return=%d\n", __func__, err);
  164. objlayout_put_deviceinfo(deviceaddr);
  165. return err ? ERR_PTR(err) : ode;
  166. }
  167. static int objio_devices_lookup(struct pnfs_layout_hdr *pnfslay,
  168. struct objio_segment *objio_seg,
  169. gfp_t gfp_flags)
  170. {
  171. unsigned i;
  172. int err;
  173. /* lookup all devices */
  174. for (i = 0; i < objio_seg->num_comps; i++) {
  175. struct objio_dev_ent *ode;
  176. ode = _device_lookup(pnfslay, objio_seg, i, gfp_flags);
  177. if (unlikely(IS_ERR(ode))) {
  178. err = PTR_ERR(ode);
  179. goto out;
  180. }
  181. objio_seg->ods[i] = ode;
  182. }
  183. err = 0;
  184. out:
  185. dprintk("%s: return=%d\n", __func__, err);
  186. return err;
  187. }
  188. static int _verify_data_map(struct pnfs_osd_layout *layout)
  189. {
  190. struct pnfs_osd_data_map *data_map = &layout->olo_map;
  191. u64 stripe_length;
  192. u32 group_width;
  193. /* FIXME: Only raid0 for now. if not go through MDS */
  194. if (data_map->odm_raid_algorithm != PNFS_OSD_RAID_0) {
  195. printk(KERN_ERR "Only RAID_0 for now\n");
  196. return -ENOTSUPP;
  197. }
  198. if (0 != (data_map->odm_num_comps % (data_map->odm_mirror_cnt + 1))) {
  199. printk(KERN_ERR "Data Map wrong, num_comps=%u mirrors=%u\n",
  200. data_map->odm_num_comps, data_map->odm_mirror_cnt);
  201. return -EINVAL;
  202. }
  203. if (data_map->odm_group_width)
  204. group_width = data_map->odm_group_width;
  205. else
  206. group_width = data_map->odm_num_comps /
  207. (data_map->odm_mirror_cnt + 1);
  208. stripe_length = (u64)data_map->odm_stripe_unit * group_width;
  209. if (stripe_length >= (1ULL << 32)) {
  210. printk(KERN_ERR "Total Stripe length(0x%llx)"
  211. " >= 32bit is not supported\n", _LLU(stripe_length));
  212. return -ENOTSUPP;
  213. }
  214. if (0 != (data_map->odm_stripe_unit & ~PAGE_MASK)) {
  215. printk(KERN_ERR "Stripe Unit(0x%llx)"
  216. " must be Multples of PAGE_SIZE(0x%lx)\n",
  217. _LLU(data_map->odm_stripe_unit), PAGE_SIZE);
  218. return -ENOTSUPP;
  219. }
  220. return 0;
  221. }
  222. static void copy_single_comp(struct pnfs_osd_object_cred *cur_comp,
  223. struct pnfs_osd_object_cred *src_comp,
  224. struct caps_buffers *caps_p)
  225. {
  226. WARN_ON(src_comp->oc_cap_key.cred_len > sizeof(caps_p->caps_key));
  227. WARN_ON(src_comp->oc_cap.cred_len > sizeof(caps_p->creds));
  228. *cur_comp = *src_comp;
  229. memcpy(caps_p->caps_key, src_comp->oc_cap_key.cred,
  230. sizeof(caps_p->caps_key));
  231. cur_comp->oc_cap_key.cred = caps_p->caps_key;
  232. memcpy(caps_p->creds, src_comp->oc_cap.cred,
  233. sizeof(caps_p->creds));
  234. cur_comp->oc_cap.cred = caps_p->creds;
  235. }
  236. int objio_alloc_lseg(struct pnfs_layout_segment **outp,
  237. struct pnfs_layout_hdr *pnfslay,
  238. struct pnfs_layout_range *range,
  239. struct xdr_stream *xdr,
  240. gfp_t gfp_flags)
  241. {
  242. struct objio_segment *objio_seg;
  243. struct pnfs_osd_xdr_decode_layout_iter iter;
  244. struct pnfs_osd_layout layout;
  245. struct pnfs_osd_object_cred *cur_comp, src_comp;
  246. struct caps_buffers *caps_p;
  247. int err;
  248. err = pnfs_osd_xdr_decode_layout_map(&layout, &iter, xdr);
  249. if (unlikely(err))
  250. return err;
  251. err = _verify_data_map(&layout);
  252. if (unlikely(err))
  253. return err;
  254. objio_seg = kzalloc(sizeof(*objio_seg) +
  255. sizeof(objio_seg->ods[0]) * layout.olo_num_comps +
  256. sizeof(*objio_seg->comps) * layout.olo_num_comps +
  257. sizeof(struct caps_buffers) * layout.olo_num_comps,
  258. gfp_flags);
  259. if (!objio_seg)
  260. return -ENOMEM;
  261. objio_seg->comps = (void *)(objio_seg->ods + layout.olo_num_comps);
  262. cur_comp = objio_seg->comps;
  263. caps_p = (void *)(cur_comp + layout.olo_num_comps);
  264. while (pnfs_osd_xdr_decode_layout_comp(&src_comp, &iter, xdr, &err))
  265. copy_single_comp(cur_comp++, &src_comp, caps_p++);
  266. if (unlikely(err))
  267. goto err;
  268. objio_seg->num_comps = layout.olo_num_comps;
  269. objio_seg->comps_index = layout.olo_comps_index;
  270. err = objio_devices_lookup(pnfslay, objio_seg, gfp_flags);
  271. if (err)
  272. goto err;
  273. objio_seg->mirrors_p1 = layout.olo_map.odm_mirror_cnt + 1;
  274. objio_seg->stripe_unit = layout.olo_map.odm_stripe_unit;
  275. if (layout.olo_map.odm_group_width) {
  276. objio_seg->group_width = layout.olo_map.odm_group_width;
  277. objio_seg->group_depth = layout.olo_map.odm_group_depth;
  278. objio_seg->group_count = layout.olo_map.odm_num_comps /
  279. objio_seg->mirrors_p1 /
  280. objio_seg->group_width;
  281. } else {
  282. objio_seg->group_width = layout.olo_map.odm_num_comps /
  283. objio_seg->mirrors_p1;
  284. objio_seg->group_depth = -1;
  285. objio_seg->group_count = 1;
  286. }
  287. *outp = &objio_seg->lseg;
  288. return 0;
  289. err:
  290. kfree(objio_seg);
  291. dprintk("%s: Error: return %d\n", __func__, err);
  292. *outp = NULL;
  293. return err;
  294. }
  295. void objio_free_lseg(struct pnfs_layout_segment *lseg)
  296. {
  297. int i;
  298. struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
  299. for (i = 0; i < objio_seg->num_comps; i++) {
  300. if (!objio_seg->ods[i])
  301. break;
  302. nfs4_put_deviceid_node(&objio_seg->ods[i]->id_node);
  303. }
  304. kfree(objio_seg);
  305. }
  306. static struct pnfs_layoutdriver_type objlayout_type = {
  307. .id = LAYOUT_OSD2_OBJECTS,
  308. .name = "LAYOUT_OSD2_OBJECTS",
  309. .alloc_layout_hdr = objlayout_alloc_layout_hdr,
  310. .free_layout_hdr = objlayout_free_layout_hdr,
  311. .alloc_lseg = objlayout_alloc_lseg,
  312. .free_lseg = objlayout_free_lseg,
  313. .free_deviceid_node = objio_free_deviceid_node,
  314. };
  315. MODULE_DESCRIPTION("pNFS Layout Driver for OSD2 objects");
  316. MODULE_AUTHOR("Benny Halevy <bhalevy@panasas.com>");
  317. MODULE_LICENSE("GPL");
  318. static int __init
  319. objlayout_init(void)
  320. {
  321. int ret = pnfs_register_layoutdriver(&objlayout_type);
  322. if (ret)
  323. printk(KERN_INFO
  324. "%s: Registering OSD pNFS Layout Driver failed: error=%d\n",
  325. __func__, ret);
  326. else
  327. printk(KERN_INFO "%s: Registered OSD pNFS Layout Driver\n",
  328. __func__);
  329. return ret;
  330. }
  331. static void __exit
  332. objlayout_exit(void)
  333. {
  334. pnfs_unregister_layoutdriver(&objlayout_type);
  335. printk(KERN_INFO "%s: Unregistered OSD pNFS Layout Driver\n",
  336. __func__);
  337. }
  338. module_init(objlayout_init);
  339. module_exit(objlayout_exit);