blob: 5457745dd4f1f993a7d37243d7c2d17bf67bade6 [file] [log] [blame]
Benny Halevyc93407d2011-05-22 19:49:06 +03001/*
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
40#include <linux/module.h>
Boaz Harroshaf4f5b52011-10-31 15:04:19 -070041#include <scsi/osd_ore.h>
Boaz Harrosh09f5bf42011-05-22 19:50:20 +030042
43#include "objlayout.h"
Peng Tao62965562012-09-25 14:55:57 +080044#include "../internal.h"
Boaz Harrosh09f5bf42011-05-22 19:50:20 +030045
46#define NFSDBG_FACILITY NFSDBG_PNFS_LD
47
Boaz Harroshb6c05f12011-05-26 21:45:34 +030048struct objio_dev_ent {
49 struct nfs4_deviceid_node id_node;
Boaz Harroshaf4f5b52011-10-31 15:04:19 -070050 struct ore_dev od;
Boaz Harroshb6c05f12011-05-26 21:45:34 +030051};
52
53static void
54objio_free_deviceid_node(struct nfs4_deviceid_node *d)
55{
56 struct objio_dev_ent *de = container_of(d, struct objio_dev_ent, id_node);
57
Boaz Harroshaf4f5b52011-10-31 15:04:19 -070058 dprintk("%s: free od=%p\n", __func__, de->od.od);
59 osduld_put_device(de->od.od);
Boaz Harroshb6c05f12011-05-26 21:45:34 +030060 kfree(de);
61}
62
63static struct objio_dev_ent *_dev_list_find(const struct nfs_server *nfss,
64 const struct nfs4_deviceid *d_id)
65{
66 struct nfs4_deviceid_node *d;
67 struct objio_dev_ent *de;
68
69 d = nfs4_find_get_deviceid(nfss->pnfs_curr_ld, nfss->nfs_client, d_id);
70 if (!d)
71 return NULL;
72
73 de = container_of(d, struct objio_dev_ent, id_node);
74 return de;
75}
76
77static struct objio_dev_ent *
78_dev_list_add(const struct nfs_server *nfss,
79 const struct nfs4_deviceid *d_id, struct osd_dev *od,
80 gfp_t gfp_flags)
81{
82 struct nfs4_deviceid_node *d;
83 struct objio_dev_ent *de = kzalloc(sizeof(*de), gfp_flags);
84 struct objio_dev_ent *n;
85
86 if (!de) {
87 dprintk("%s: -ENOMEM od=%p\n", __func__, od);
88 return NULL;
89 }
90
91 dprintk("%s: Adding od=%p\n", __func__, od);
92 nfs4_init_deviceid_node(&de->id_node,
93 nfss->pnfs_curr_ld,
94 nfss->nfs_client,
95 d_id);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -070096 de->od.od = od;
Boaz Harroshb6c05f12011-05-26 21:45:34 +030097
98 d = nfs4_insert_deviceid_node(&de->id_node);
99 n = container_of(d, struct objio_dev_ent, id_node);
100 if (n != de) {
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700101 dprintk("%s: Race with other n->od=%p\n", __func__, n->od.od);
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300102 objio_free_deviceid_node(&de->id_node);
103 de = n;
104 }
105
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300106 return de;
107}
108
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300109struct objio_segment {
110 struct pnfs_layout_segment lseg;
111
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700112 struct ore_layout layout;
113 struct ore_components oc;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300114};
115
116static inline struct objio_segment *
117OBJIO_LSEG(struct pnfs_layout_segment *lseg)
118{
119 return container_of(lseg, struct objio_segment, lseg);
120}
121
Boaz Harrosh04f83452011-05-22 19:52:19 +0300122struct objio_state {
123 /* Generic layer */
Boaz Harroshe2e04352011-10-31 15:03:35 -0700124 struct objlayout_io_res oir;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300125
Boaz Harrosh96218552011-10-31 14:47:32 -0700126 bool sync;
Boaz Harrosheecfc632011-10-31 15:15:38 -0700127 /*FIXME: Support for extra_bytes at ore_get_rw_state() */
128 struct ore_io_state *ios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300129};
130
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300131/* Send and wait for a get_device_info of devices in the layout,
132 then look them up with the osd_initiator library */
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700133static int objio_devices_lookup(struct pnfs_layout_hdr *pnfslay,
134 struct objio_segment *objio_seg, unsigned c, struct nfs4_deviceid *d_id,
135 gfp_t gfp_flags)
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300136{
137 struct pnfs_osd_deviceaddr *deviceaddr;
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300138 struct objio_dev_ent *ode;
139 struct osd_dev *od;
140 struct osd_dev_info odi;
Sachin Bhamare18d98f62012-03-19 20:47:58 -0700141 bool retry_flag = true;
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300142 int err;
143
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300144 ode = _dev_list_find(NFS_SERVER(pnfslay->plh_inode), d_id);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700145 if (ode) {
146 objio_seg->oc.ods[c] = &ode->od; /* must use container_of */
147 return 0;
148 }
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300149
150 err = objlayout_get_deviceinfo(pnfslay, d_id, &deviceaddr, gfp_flags);
151 if (unlikely(err)) {
152 dprintk("%s: objlayout_get_deviceinfo dev(%llx:%llx) =>%d\n",
153 __func__, _DEVID_LO(d_id), _DEVID_HI(d_id), err);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700154 return err;
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300155 }
156
157 odi.systemid_len = deviceaddr->oda_systemid.len;
158 if (odi.systemid_len > sizeof(odi.systemid)) {
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700159 dprintk("%s: odi.systemid_len > sizeof(systemid=%zd)\n",
160 __func__, sizeof(odi.systemid));
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300161 err = -EINVAL;
162 goto out;
163 } else if (odi.systemid_len)
164 memcpy(odi.systemid, deviceaddr->oda_systemid.data,
165 odi.systemid_len);
166 odi.osdname_len = deviceaddr->oda_osdname.len;
167 odi.osdname = (u8 *)deviceaddr->oda_osdname.data;
168
169 if (!odi.osdname_len && !odi.systemid_len) {
170 dprintk("%s: !odi.osdname_len && !odi.systemid_len\n",
171 __func__);
172 err = -ENODEV;
173 goto out;
174 }
175
Sachin Bhamare18d98f62012-03-19 20:47:58 -0700176retry_lookup:
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300177 od = osduld_info_lookup(&odi);
178 if (unlikely(IS_ERR(od))) {
179 err = PTR_ERR(od);
180 dprintk("%s: osduld_info_lookup => %d\n", __func__, err);
Sachin Bhamare18d98f62012-03-19 20:47:58 -0700181 if (err == -ENODEV && retry_flag) {
182 err = objlayout_autologin(deviceaddr);
183 if (likely(!err)) {
184 retry_flag = false;
185 goto retry_lookup;
186 }
187 }
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300188 goto out;
189 }
190
191 ode = _dev_list_add(NFS_SERVER(pnfslay->plh_inode), d_id, od,
192 gfp_flags);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700193 objio_seg->oc.ods[c] = &ode->od; /* must use container_of */
194 dprintk("Adding new dev_id(%llx:%llx)\n",
195 _DEVID_LO(d_id), _DEVID_HI(d_id));
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300196out:
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300197 objlayout_put_deviceinfo(deviceaddr);
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300198 return err;
199}
200
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700201static void copy_single_comp(struct ore_components *oc, unsigned c,
202 struct pnfs_osd_object_cred *src_comp)
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300203{
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700204 struct ore_comp *ocomp = &oc->comps[c];
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300205
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700206 WARN_ON(src_comp->oc_cap_key.cred_len > 0); /* libosd is NO_SEC only */
207 WARN_ON(src_comp->oc_cap.cred_len > sizeof(ocomp->cred));
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300208
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700209 ocomp->obj.partition = src_comp->oc_object_id.oid_partition_id;
210 ocomp->obj.id = src_comp->oc_object_id.oid_object_id;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300211
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700212 memcpy(ocomp->cred, src_comp->oc_cap.cred, sizeof(ocomp->cred));
213}
214
Trond Myklebust1385b812012-05-04 13:54:24 -0400215static int __alloc_objio_seg(unsigned numdevs, gfp_t gfp_flags,
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700216 struct objio_segment **pseg)
217{
Boaz Harrosh5318a29c2012-03-13 20:44:26 -0700218/* This is the in memory structure of the objio_segment
219 *
220 * struct __alloc_objio_segment {
221 * struct objio_segment olseg;
222 * struct ore_dev *ods[numdevs];
223 * struct ore_comp comps[numdevs];
224 * } *aolseg;
225 * NOTE: The code as above compiles and runs perfectly. It is elegant,
226 * type safe and compact. At some Past time Linus has decided he does not
227 * like variable length arrays, For the sake of this principal we uglify
228 * the code as below.
229 */
230 struct objio_segment *lseg;
231 size_t lseg_size = sizeof(*lseg) +
232 numdevs * sizeof(lseg->oc.ods[0]) +
233 numdevs * sizeof(*lseg->oc.comps);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700234
Boaz Harrosh5318a29c2012-03-13 20:44:26 -0700235 lseg = kzalloc(lseg_size, gfp_flags);
236 if (unlikely(!lseg)) {
Masanari Iidaa895d572013-04-09 02:06:50 +0900237 dprintk("%s: Failed allocation numdevs=%d size=%zd\n", __func__,
Boaz Harrosh5318a29c2012-03-13 20:44:26 -0700238 numdevs, lseg_size);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700239 return -ENOMEM;
240 }
241
Boaz Harrosh5318a29c2012-03-13 20:44:26 -0700242 lseg->oc.numdevs = numdevs;
243 lseg->oc.single_comp = EC_MULTPLE_COMPS;
244 lseg->oc.ods = (void *)(lseg + 1);
245 lseg->oc.comps = (void *)(lseg->oc.ods + numdevs);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700246
Boaz Harrosh5318a29c2012-03-13 20:44:26 -0700247 *pseg = lseg;
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700248 return 0;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300249}
250
251int objio_alloc_lseg(struct pnfs_layout_segment **outp,
252 struct pnfs_layout_hdr *pnfslay,
253 struct pnfs_layout_range *range,
254 struct xdr_stream *xdr,
255 gfp_t gfp_flags)
256{
257 struct objio_segment *objio_seg;
258 struct pnfs_osd_xdr_decode_layout_iter iter;
259 struct pnfs_osd_layout layout;
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700260 struct pnfs_osd_object_cred src_comp;
261 unsigned cur_comp;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300262 int err;
263
264 err = pnfs_osd_xdr_decode_layout_map(&layout, &iter, xdr);
265 if (unlikely(err))
266 return err;
267
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700268 err = __alloc_objio_seg(layout.olo_num_comps, gfp_flags, &objio_seg);
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300269 if (unlikely(err))
270 return err;
271
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700272 objio_seg->layout.stripe_unit = layout.olo_map.odm_stripe_unit;
273 objio_seg->layout.group_width = layout.olo_map.odm_group_width;
274 objio_seg->layout.group_depth = layout.olo_map.odm_group_depth;
275 objio_seg->layout.mirrors_p1 = layout.olo_map.odm_mirror_cnt + 1;
276 objio_seg->layout.raid_algorithm = layout.olo_map.odm_raid_algorithm;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300277
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700278 err = ore_verify_layout(layout.olo_map.odm_num_comps,
279 &objio_seg->layout);
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300280 if (unlikely(err))
281 goto err;
282
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700283 objio_seg->oc.first_dev = layout.olo_comps_index;
284 cur_comp = 0;
285 while (pnfs_osd_xdr_decode_layout_comp(&src_comp, &iter, xdr, &err)) {
286 copy_single_comp(&objio_seg->oc, cur_comp, &src_comp);
287 err = objio_devices_lookup(pnfslay, objio_seg, cur_comp,
288 &src_comp.oc_object_id.oid_device_id,
289 gfp_flags);
290 if (err)
291 goto err;
292 ++cur_comp;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300293 }
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700294 /* pnfs_osd_xdr_decode_layout_comp returns false on error */
295 if (unlikely(err))
296 goto err;
Boaz Harrosh93420772011-05-25 21:25:29 +0300297
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300298 *outp = &objio_seg->lseg;
299 return 0;
300
301err:
302 kfree(objio_seg);
303 dprintk("%s: Error: return %d\n", __func__, err);
304 *outp = NULL;
305 return err;
306}
307
308void objio_free_lseg(struct pnfs_layout_segment *lseg)
309{
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300310 int i;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300311 struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
312
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700313 for (i = 0; i < objio_seg->oc.numdevs; i++) {
314 struct ore_dev *od = objio_seg->oc.ods[i];
315 struct objio_dev_ent *ode;
316
317 if (!od)
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300318 break;
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700319 ode = container_of(od, typeof(*ode), od);
320 nfs4_put_deviceid_node(&ode->id_node);
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300321 }
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300322 kfree(objio_seg);
323}
324
Boaz Harrosh96218552011-10-31 14:47:32 -0700325static int
Boaz Harrosheecfc632011-10-31 15:15:38 -0700326objio_alloc_io_state(struct pnfs_layout_hdr *pnfs_layout_type, bool is_reading,
Boaz Harrosh96218552011-10-31 14:47:32 -0700327 struct pnfs_layout_segment *lseg, struct page **pages, unsigned pgbase,
328 loff_t offset, size_t count, void *rpcdata, gfp_t gfp_flags,
329 struct objio_state **outp)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300330{
331 struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
Boaz Harrosheecfc632011-10-31 15:15:38 -0700332 struct ore_io_state *ios;
333 int ret;
Boaz Harrosh96218552011-10-31 14:47:32 -0700334 struct __alloc_objio_state {
335 struct objio_state objios;
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700336 struct pnfs_osd_ioerr ioerrs[objio_seg->oc.numdevs];
Boaz Harrosh96218552011-10-31 14:47:32 -0700337 } *aos;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300338
Boaz Harrosh96218552011-10-31 14:47:32 -0700339 aos = kzalloc(sizeof(*aos), gfp_flags);
340 if (unlikely(!aos))
Boaz Harrosh04f83452011-05-22 19:52:19 +0300341 return -ENOMEM;
342
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700343 objlayout_init_ioerrs(&aos->objios.oir, objio_seg->oc.numdevs,
Boaz Harrosh96218552011-10-31 14:47:32 -0700344 aos->ioerrs, rpcdata, pnfs_layout_type);
345
Boaz Harrosheecfc632011-10-31 15:15:38 -0700346 ret = ore_get_rw_state(&objio_seg->layout, &objio_seg->oc, is_reading,
347 offset, count, &ios);
348 if (unlikely(ret)) {
349 kfree(aos);
350 return ret;
351 }
352
Boaz Harrosh96218552011-10-31 14:47:32 -0700353 ios->pages = pages;
354 ios->pgbase = pgbase;
Boaz Harrosheecfc632011-10-31 15:15:38 -0700355 ios->private = aos;
Boaz Harrosh96218552011-10-31 14:47:32 -0700356 BUG_ON(ios->nr_pages > (pgbase + count + PAGE_SIZE - 1) >> PAGE_SHIFT);
357
Boaz Harrosheecfc632011-10-31 15:15:38 -0700358 aos->objios.sync = 0;
359 aos->objios.ios = ios;
360 *outp = &aos->objios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300361 return 0;
362}
363
Boaz Harroshe2e04352011-10-31 15:03:35 -0700364void objio_free_result(struct objlayout_io_res *oir)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300365{
Boaz Harrosheecfc632011-10-31 15:15:38 -0700366 struct objio_state *objios = container_of(oir, struct objio_state, oir);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300367
Boaz Harrosheecfc632011-10-31 15:15:38 -0700368 ore_put_io_state(objios->ios);
369 kfree(objios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300370}
371
Trond Myklebust2e928e42012-10-16 12:34:56 -0400372static enum pnfs_osd_errno osd_pri_2_pnfs_err(enum osd_err_priority oep)
Boaz Harroshadb58532011-05-26 21:49:46 +0300373{
374 switch (oep) {
375 case OSD_ERR_PRI_NO_ERROR:
376 return (enum pnfs_osd_errno)0;
377
378 case OSD_ERR_PRI_CLEAR_PAGES:
379 BUG_ON(1);
380 return 0;
381
382 case OSD_ERR_PRI_RESOURCE:
383 return PNFS_OSD_ERR_RESOURCE;
384 case OSD_ERR_PRI_BAD_CRED:
385 return PNFS_OSD_ERR_BAD_CRED;
386 case OSD_ERR_PRI_NO_ACCESS:
387 return PNFS_OSD_ERR_NO_ACCESS;
388 case OSD_ERR_PRI_UNREACHABLE:
389 return PNFS_OSD_ERR_UNREACHABLE;
390 case OSD_ERR_PRI_NOT_FOUND:
391 return PNFS_OSD_ERR_NOT_FOUND;
392 case OSD_ERR_PRI_NO_SPACE:
393 return PNFS_OSD_ERR_NO_SPACE;
394 default:
395 WARN_ON(1);
396 /* fallthrough */
397 case OSD_ERR_PRI_EIO:
398 return PNFS_OSD_ERR_EIO;
399 }
400}
401
Boaz Harrosheecfc632011-10-31 15:15:38 -0700402static void __on_dev_error(struct ore_io_state *ios,
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700403 struct ore_dev *od, unsigned dev_index, enum osd_err_priority oep,
404 u64 dev_offset, u64 dev_len)
405{
406 struct objio_state *objios = ios->private;
407 struct pnfs_osd_objid pooid;
408 struct objio_dev_ent *ode = container_of(od, typeof(*ode), od);
409 /* FIXME: what to do with more-then-one-group layouts. We need to
410 * translate from ore_io_state index to oc->comps index
411 */
412 unsigned comp = dev_index;
413
414 pooid.oid_device_id = ode->id_node.deviceid;
415 pooid.oid_partition_id = ios->oc->comps[comp].obj.partition;
416 pooid.oid_object_id = ios->oc->comps[comp].obj.id;
417
418 objlayout_io_set_result(&objios->oir, comp,
419 &pooid, osd_pri_2_pnfs_err(oep),
Boaz Harrosheecfc632011-10-31 15:15:38 -0700420 dev_offset, dev_len, !ios->reading);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700421}
422
Boaz Harrosh04f83452011-05-22 19:52:19 +0300423/*
424 * read
425 */
Boaz Harrosheecfc632011-10-31 15:15:38 -0700426static void _read_done(struct ore_io_state *ios, void *private)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300427{
Boaz Harrosheecfc632011-10-31 15:15:38 -0700428 struct objio_state *objios = private;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300429 ssize_t status;
Boaz Harrosheecfc632011-10-31 15:15:38 -0700430 int ret = ore_check_io(ios, &__on_dev_error);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300431
Boaz Harrosheecfc632011-10-31 15:15:38 -0700432 /* FIXME: _io_free(ios) can we dealocate the libosd resources; */
Boaz Harrosh04f83452011-05-22 19:52:19 +0300433
434 if (likely(!ret))
435 status = ios->length;
436 else
437 status = ret;
438
Boaz Harrosheecfc632011-10-31 15:15:38 -0700439 objlayout_read_done(&objios->oir, status, objios->sync);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300440}
441
Boaz Harrosh96218552011-10-31 14:47:32 -0700442int objio_read_pagelist(struct nfs_read_data *rdata)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300443{
Fred Isamancd841602012-04-20 14:47:44 -0400444 struct nfs_pgio_header *hdr = rdata->header;
Boaz Harrosheecfc632011-10-31 15:15:38 -0700445 struct objio_state *objios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300446 int ret;
447
Fred Isamancd841602012-04-20 14:47:44 -0400448 ret = objio_alloc_io_state(NFS_I(hdr->inode)->layout, true,
449 hdr->lseg, rdata->args.pages, rdata->args.pgbase,
Boaz Harrosh96218552011-10-31 14:47:32 -0700450 rdata->args.offset, rdata->args.count, rdata,
Boaz Harrosheecfc632011-10-31 15:15:38 -0700451 GFP_KERNEL, &objios);
Boaz Harrosh96218552011-10-31 14:47:32 -0700452 if (unlikely(ret))
453 return ret;
454
Boaz Harrosheecfc632011-10-31 15:15:38 -0700455 objios->ios->done = _read_done;
456 dprintk("%s: offset=0x%llx length=0x%x\n", __func__,
457 rdata->args.offset, rdata->args.count);
Boaz Harrosh9909d452012-06-08 05:29:40 +0300458 ret = ore_read(objios->ios);
459 if (unlikely(ret))
460 objio_free_result(&objios->oir);
461 return ret;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300462}
463
464/*
465 * write
466 */
Boaz Harrosheecfc632011-10-31 15:15:38 -0700467static void _write_done(struct ore_io_state *ios, void *private)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300468{
Boaz Harrosheecfc632011-10-31 15:15:38 -0700469 struct objio_state *objios = private;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300470 ssize_t status;
Boaz Harrosheecfc632011-10-31 15:15:38 -0700471 int ret = ore_check_io(ios, &__on_dev_error);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300472
Boaz Harrosheecfc632011-10-31 15:15:38 -0700473 /* FIXME: _io_free(ios) can we dealocate the libosd resources; */
Boaz Harrosh04f83452011-05-22 19:52:19 +0300474
475 if (likely(!ret)) {
476 /* FIXME: should be based on the OSD's persistence model
477 * See OSD2r05 Section 4.13 Data persistence model */
Boaz Harrosheecfc632011-10-31 15:15:38 -0700478 objios->oir.committed = NFS_FILE_SYNC;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300479 status = ios->length;
480 } else {
481 status = ret;
482 }
483
Boaz Harrosheecfc632011-10-31 15:15:38 -0700484 objlayout_write_done(&objios->oir, status, objios->sync);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300485}
486
Boaz Harrosh278c0232011-10-31 15:16:54 -0700487static struct page *__r4w_get_page(void *priv, u64 offset, bool *uptodate)
488{
489 struct objio_state *objios = priv;
490 struct nfs_write_data *wdata = objios->oir.rpcdata;
Fred Isamancd841602012-04-20 14:47:44 -0400491 struct address_space *mapping = wdata->header->inode->i_mapping;
Boaz Harrosh278c0232011-10-31 15:16:54 -0700492 pgoff_t index = offset / PAGE_SIZE;
Boaz Harroshc999ff62012-06-08 02:02:30 +0300493 struct page *page;
494 loff_t i_size = i_size_read(wdata->header->inode);
Boaz Harrosh278c0232011-10-31 15:16:54 -0700495
Boaz Harroshc999ff62012-06-08 02:02:30 +0300496 if (offset >= i_size) {
497 *uptodate = true;
498 dprintk("%s: g_zero_page index=0x%lx\n", __func__, index);
499 return ZERO_PAGE(0);
500 }
501
502 page = find_get_page(mapping, index);
Boaz Harrosh278c0232011-10-31 15:16:54 -0700503 if (!page) {
Fred Isamancd841602012-04-20 14:47:44 -0400504 page = find_or_create_page(mapping, index, GFP_NOFS);
Boaz Harrosh278c0232011-10-31 15:16:54 -0700505 if (unlikely(!page)) {
506 dprintk("%s: grab_cache_page Failed index=0x%lx\n",
507 __func__, index);
508 return NULL;
509 }
510 unlock_page(page);
511 }
512 if (PageDirty(page) || PageWriteback(page))
513 *uptodate = true;
514 else
515 *uptodate = PageUptodate(page);
516 dprintk("%s: index=0x%lx uptodate=%d\n", __func__, index, *uptodate);
517 return page;
518}
519
520static void __r4w_put_page(void *priv, struct page *page)
521{
Boaz Harroshc999ff62012-06-08 02:02:30 +0300522 dprintk("%s: index=0x%lx\n", __func__,
523 (page == ZERO_PAGE(0)) ? -1UL : page->index);
524 if (ZERO_PAGE(0) != page)
525 page_cache_release(page);
Boaz Harrosh278c0232011-10-31 15:16:54 -0700526 return;
527}
528
529static const struct _ore_r4w_op _r4w_op = {
530 .get_page = &__r4w_get_page,
531 .put_page = &__r4w_put_page,
532};
533
Boaz Harrosh96218552011-10-31 14:47:32 -0700534int objio_write_pagelist(struct nfs_write_data *wdata, int how)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300535{
Fred Isamancd841602012-04-20 14:47:44 -0400536 struct nfs_pgio_header *hdr = wdata->header;
Boaz Harrosheecfc632011-10-31 15:15:38 -0700537 struct objio_state *objios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300538 int ret;
539
Fred Isamancd841602012-04-20 14:47:44 -0400540 ret = objio_alloc_io_state(NFS_I(hdr->inode)->layout, false,
541 hdr->lseg, wdata->args.pages, wdata->args.pgbase,
Boaz Harrosh96218552011-10-31 14:47:32 -0700542 wdata->args.offset, wdata->args.count, wdata, GFP_NOFS,
Boaz Harrosheecfc632011-10-31 15:15:38 -0700543 &objios);
Boaz Harrosh96218552011-10-31 14:47:32 -0700544 if (unlikely(ret))
545 return ret;
546
Boaz Harrosheecfc632011-10-31 15:15:38 -0700547 objios->sync = 0 != (how & FLUSH_SYNC);
Boaz Harrosh278c0232011-10-31 15:16:54 -0700548 objios->ios->r4w = &_r4w_op;
Boaz Harrosh96218552011-10-31 14:47:32 -0700549
Boaz Harrosheecfc632011-10-31 15:15:38 -0700550 if (!objios->sync)
551 objios->ios->done = _write_done;
552
553 dprintk("%s: offset=0x%llx length=0x%x\n", __func__,
554 wdata->args.offset, wdata->args.count);
555 ret = ore_write(objios->ios);
Boaz Harrosh9909d452012-06-08 05:29:40 +0300556 if (unlikely(ret)) {
557 objio_free_result(&objios->oir);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300558 return ret;
Boaz Harrosh9909d452012-06-08 05:29:40 +0300559 }
Boaz Harrosh04f83452011-05-22 19:52:19 +0300560
Boaz Harrosheecfc632011-10-31 15:15:38 -0700561 if (objios->sync)
562 _write_done(objios->ios, objios);
563
564 return 0;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300565}
566
Boaz Harrosh93420772011-05-25 21:25:29 +0300567static bool objio_pg_test(struct nfs_pageio_descriptor *pgio,
568 struct nfs_page *prev, struct nfs_page *req)
569{
570 if (!pnfs_generic_pg_test(pgio, prev, req))
571 return false;
572
573 return pgio->pg_count + req->wb_bytes <=
Boaz Harrosh7de6e282012-08-02 15:38:23 +0300574 (unsigned long)pgio->pg_layout_private;
575}
576
Trond Myklebust2e928e42012-10-16 12:34:56 -0400577static void objio_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
Boaz Harrosh7de6e282012-08-02 15:38:23 +0300578{
579 pnfs_generic_pg_init_read(pgio, req);
580 if (unlikely(pgio->pg_lseg == NULL))
581 return; /* Not pNFS */
582
583 pgio->pg_layout_private = (void *)
584 OBJIO_LSEG(pgio->pg_lseg)->layout.max_io_length;
585}
586
587static bool aligned_on_raid_stripe(u64 offset, struct ore_layout *layout,
588 unsigned long *stripe_end)
589{
590 u32 stripe_off;
591 unsigned stripe_size;
592
593 if (layout->raid_algorithm == PNFS_OSD_RAID_0)
594 return true;
595
596 stripe_size = layout->stripe_unit *
597 (layout->group_width - layout->parity);
598
599 div_u64_rem(offset, stripe_size, &stripe_off);
600 if (!stripe_off)
601 return true;
602
603 *stripe_end = stripe_size - stripe_off;
604 return false;
605}
606
Trond Myklebust2e928e42012-10-16 12:34:56 -0400607static void objio_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
Boaz Harrosh7de6e282012-08-02 15:38:23 +0300608{
609 unsigned long stripe_end = 0;
Peng Tao62965562012-09-25 14:55:57 +0800610 u64 wb_size;
Boaz Harrosh7de6e282012-08-02 15:38:23 +0300611
Peng Tao62965562012-09-25 14:55:57 +0800612 if (pgio->pg_dreq == NULL)
613 wb_size = i_size_read(pgio->pg_inode) - req_offset(req);
614 else
615 wb_size = nfs_dreq_bytes_left(pgio->pg_dreq);
616
617 pnfs_generic_pg_init_write(pgio, req, wb_size);
Boaz Harrosh7de6e282012-08-02 15:38:23 +0300618 if (unlikely(pgio->pg_lseg == NULL))
619 return; /* Not pNFS */
620
621 if (req->wb_offset ||
622 !aligned_on_raid_stripe(req->wb_index * PAGE_SIZE,
623 &OBJIO_LSEG(pgio->pg_lseg)->layout,
624 &stripe_end)) {
625 pgio->pg_layout_private = (void *)stripe_end;
626 } else {
627 pgio->pg_layout_private = (void *)
628 OBJIO_LSEG(pgio->pg_lseg)->layout.max_io_length;
629 }
Boaz Harrosh93420772011-05-25 21:25:29 +0300630}
631
Trond Myklebust1751c362011-06-10 13:30:23 -0400632static const struct nfs_pageio_ops objio_pg_read_ops = {
Boaz Harrosh7de6e282012-08-02 15:38:23 +0300633 .pg_init = objio_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400634 .pg_test = objio_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400635 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400636};
637
638static const struct nfs_pageio_ops objio_pg_write_ops = {
Boaz Harrosh7de6e282012-08-02 15:38:23 +0300639 .pg_init = objio_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400640 .pg_test = objio_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400641 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400642};
643
Benny Halevyc93407d2011-05-22 19:49:06 +0300644static struct pnfs_layoutdriver_type objlayout_type = {
645 .id = LAYOUT_OSD2_OBJECTS,
646 .name = "LAYOUT_OSD2_OBJECTS",
Boaz Harroshfe0fe832012-01-06 09:31:20 +0200647 .flags = PNFS_LAYOUTRET_ON_SETATTR |
648 PNFS_LAYOUTRET_ON_ERROR,
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300649
fanchaoting5a12cca2013-02-04 21:15:02 +0800650 .owner = THIS_MODULE,
Benny Halevye51b8412011-05-22 19:51:48 +0300651 .alloc_layout_hdr = objlayout_alloc_layout_hdr,
652 .free_layout_hdr = objlayout_free_layout_hdr,
653
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300654 .alloc_lseg = objlayout_alloc_lseg,
655 .free_lseg = objlayout_free_lseg,
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300656
Boaz Harrosh04f83452011-05-22 19:52:19 +0300657 .read_pagelist = objlayout_read_pagelist,
658 .write_pagelist = objlayout_write_pagelist,
Trond Myklebust1751c362011-06-10 13:30:23 -0400659 .pg_read_ops = &objio_pg_read_ops,
660 .pg_write_ops = &objio_pg_write_ops,
Boaz Harrosh04f83452011-05-22 19:52:19 +0300661
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300662 .free_deviceid_node = objio_free_deviceid_node,
Boaz Harroshadb58532011-05-26 21:49:46 +0300663
Boaz Harrosha0fe8bf2011-05-22 19:54:13 +0300664 .encode_layoutcommit = objlayout_encode_layoutcommit,
Boaz Harroshadb58532011-05-26 21:49:46 +0300665 .encode_layoutreturn = objlayout_encode_layoutreturn,
Benny Halevyc93407d2011-05-22 19:49:06 +0300666};
667
668MODULE_DESCRIPTION("pNFS Layout Driver for OSD2 objects");
669MODULE_AUTHOR("Benny Halevy <bhalevy@panasas.com>");
670MODULE_LICENSE("GPL");
671
672static int __init
673objlayout_init(void)
674{
675 int ret = pnfs_register_layoutdriver(&objlayout_type);
676
677 if (ret)
678 printk(KERN_INFO
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500679 "NFS: %s: Registering OSD pNFS Layout Driver failed: error=%d\n",
Benny Halevyc93407d2011-05-22 19:49:06 +0300680 __func__, ret);
681 else
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500682 printk(KERN_INFO "NFS: %s: Registered OSD pNFS Layout Driver\n",
Benny Halevyc93407d2011-05-22 19:49:06 +0300683 __func__);
684 return ret;
685}
686
687static void __exit
688objlayout_exit(void)
689{
690 pnfs_unregister_layoutdriver(&objlayout_type);
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500691 printk(KERN_INFO "NFS: %s: Unregistered OSD pNFS Layout Driver\n",
Benny Halevyc93407d2011-05-22 19:49:06 +0300692 __func__);
693}
694
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -0400695MODULE_ALIAS("nfs-layouttype4-2");
696
Benny Halevyc93407d2011-05-22 19:49:06 +0300697module_init(objlayout_init);
698module_exit(objlayout_exit);