blob: 10e5fca3b7fb8937870487dcaa4fc6fe8a07cd11 [file] [log] [blame]
Boaz Harrosh09f5bf42011-05-22 19:50:20 +03001/*
2 * pNFS Objects layout driver high level definitions
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 <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
40#include <scsi/osd_initiator.h>
41#include "objlayout.h"
42
43#define NFSDBG_FACILITY NFSDBG_PNFS_LD
44/*
45 * Unmarshall layout and store it in pnfslay.
46 */
47struct pnfs_layout_segment *
48objlayout_alloc_lseg(struct pnfs_layout_hdr *pnfslay,
49 struct nfs4_layoutget_res *lgr,
50 gfp_t gfp_flags)
51{
52 int status = -ENOMEM;
53 struct xdr_stream stream;
54 struct xdr_buf buf = {
55 .pages = lgr->layoutp->pages,
56 .page_len = lgr->layoutp->len,
57 .buflen = lgr->layoutp->len,
58 .len = lgr->layoutp->len,
59 };
60 struct page *scratch;
61 struct pnfs_layout_segment *lseg;
62
63 dprintk("%s: Begin pnfslay %p\n", __func__, pnfslay);
64
65 scratch = alloc_page(gfp_flags);
66 if (!scratch)
67 goto err_nofree;
68
69 xdr_init_decode(&stream, &buf, NULL);
70 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
71
72 status = objio_alloc_lseg(&lseg, pnfslay, &lgr->range, &stream, gfp_flags);
73 if (unlikely(status)) {
74 dprintk("%s: objio_alloc_lseg Return err %d\n", __func__,
75 status);
76 goto err;
77 }
78
79 __free_page(scratch);
80
81 dprintk("%s: Return %p\n", __func__, lseg);
82 return lseg;
83
84err:
85 __free_page(scratch);
86err_nofree:
87 dprintk("%s: Err Return=>%d\n", __func__, status);
88 return ERR_PTR(status);
89}
90
91/*
92 * Free a layout segement
93 */
94void
95objlayout_free_lseg(struct pnfs_layout_segment *lseg)
96{
97 dprintk("%s: freeing layout segment %p\n", __func__, lseg);
98
99 if (unlikely(!lseg))
100 return;
101
102 objio_free_lseg(lseg);
103}
104
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300105/*
106 * Get Device Info API for io engines
107 */
108struct objlayout_deviceinfo {
109 struct page *page;
110 struct pnfs_osd_deviceaddr da; /* This must be last */
111};
112
113/* Initialize and call nfs_getdeviceinfo, then decode and return a
114 * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo()
115 * should be called.
116 */
117int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay,
118 struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr,
119 gfp_t gfp_flags)
120{
121 struct objlayout_deviceinfo *odi;
122 struct pnfs_device pd;
123 struct super_block *sb;
124 struct page *page, **pages;
125 u32 *p;
126 int err;
127
128 page = alloc_page(gfp_flags);
129 if (!page)
130 return -ENOMEM;
131
132 pages = &page;
133 pd.pages = pages;
134
135 memcpy(&pd.dev_id, d_id, sizeof(*d_id));
136 pd.layout_type = LAYOUT_OSD2_OBJECTS;
137 pd.pages = &page;
138 pd.pgbase = 0;
139 pd.pglen = PAGE_SIZE;
140 pd.mincount = 0;
141
142 sb = pnfslay->plh_inode->i_sb;
143 err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd);
144 dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err);
145 if (err)
146 goto err_out;
147
148 p = page_address(page);
149 odi = kzalloc(sizeof(*odi), gfp_flags);
150 if (!odi) {
151 err = -ENOMEM;
152 goto err_out;
153 }
154 pnfs_osd_xdr_decode_deviceaddr(&odi->da, p);
155 odi->page = page;
156 *deviceaddr = &odi->da;
157 return 0;
158
159err_out:
160 __free_page(page);
161 return err;
162}
163
164void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr)
165{
166 struct objlayout_deviceinfo *odi = container_of(deviceaddr,
167 struct objlayout_deviceinfo,
168 da);
169
170 __free_page(odi->page);
171 kfree(odi);
172}