blob: 3a621a2fd321f22b5595b628f026904905bd4d1a [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"
44
45#define NFSDBG_FACILITY NFSDBG_PNFS_LD
46
Boaz Harroshb6c05f12011-05-26 21:45:34 +030047struct objio_dev_ent {
48 struct nfs4_deviceid_node id_node;
Boaz Harroshaf4f5b52011-10-31 15:04:19 -070049 struct ore_dev od;
Boaz Harroshb6c05f12011-05-26 21:45:34 +030050};
51
52static void
53objio_free_deviceid_node(struct nfs4_deviceid_node *d)
54{
55 struct objio_dev_ent *de = container_of(d, struct objio_dev_ent, id_node);
56
Boaz Harroshaf4f5b52011-10-31 15:04:19 -070057 dprintk("%s: free od=%p\n", __func__, de->od.od);
58 osduld_put_device(de->od.od);
Boaz Harroshb6c05f12011-05-26 21:45:34 +030059 kfree(de);
60}
61
62static struct objio_dev_ent *_dev_list_find(const struct nfs_server *nfss,
63 const struct nfs4_deviceid *d_id)
64{
65 struct nfs4_deviceid_node *d;
66 struct objio_dev_ent *de;
67
68 d = nfs4_find_get_deviceid(nfss->pnfs_curr_ld, nfss->nfs_client, d_id);
69 if (!d)
70 return NULL;
71
72 de = container_of(d, struct objio_dev_ent, id_node);
73 return de;
74}
75
76static struct objio_dev_ent *
77_dev_list_add(const struct nfs_server *nfss,
78 const struct nfs4_deviceid *d_id, struct osd_dev *od,
79 gfp_t gfp_flags)
80{
81 struct nfs4_deviceid_node *d;
82 struct objio_dev_ent *de = kzalloc(sizeof(*de), gfp_flags);
83 struct objio_dev_ent *n;
84
85 if (!de) {
86 dprintk("%s: -ENOMEM od=%p\n", __func__, od);
87 return NULL;
88 }
89
90 dprintk("%s: Adding od=%p\n", __func__, od);
91 nfs4_init_deviceid_node(&de->id_node,
92 nfss->pnfs_curr_ld,
93 nfss->nfs_client,
94 d_id);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -070095 de->od.od = od;
Boaz Harroshb6c05f12011-05-26 21:45:34 +030096
97 d = nfs4_insert_deviceid_node(&de->id_node);
98 n = container_of(d, struct objio_dev_ent, id_node);
99 if (n != de) {
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700100 dprintk("%s: Race with other n->od=%p\n", __func__, n->od.od);
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300101 objio_free_deviceid_node(&de->id_node);
102 de = n;
103 }
104
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300105 return de;
106}
107
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300108struct objio_segment {
109 struct pnfs_layout_segment lseg;
110
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700111 struct ore_layout layout;
112 struct ore_components oc;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300113};
114
115static inline struct objio_segment *
116OBJIO_LSEG(struct pnfs_layout_segment *lseg)
117{
118 return container_of(lseg, struct objio_segment, lseg);
119}
120
Boaz Harrosh04f83452011-05-22 19:52:19 +0300121struct objio_state {
122 /* Generic layer */
Boaz Harroshe2e04352011-10-31 15:03:35 -0700123 struct objlayout_io_res oir;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300124
Boaz Harrosh96218552011-10-31 14:47:32 -0700125 bool sync;
Boaz Harrosheecfc632011-10-31 15:15:38 -0700126 /*FIXME: Support for extra_bytes at ore_get_rw_state() */
127 struct ore_io_state *ios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300128};
129
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300130/* Send and wait for a get_device_info of devices in the layout,
131 then look them up with the osd_initiator library */
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700132static int objio_devices_lookup(struct pnfs_layout_hdr *pnfslay,
133 struct objio_segment *objio_seg, unsigned c, struct nfs4_deviceid *d_id,
134 gfp_t gfp_flags)
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300135{
136 struct pnfs_osd_deviceaddr *deviceaddr;
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300137 struct objio_dev_ent *ode;
138 struct osd_dev *od;
139 struct osd_dev_info odi;
140 int err;
141
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300142 ode = _dev_list_find(NFS_SERVER(pnfslay->plh_inode), d_id);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700143 if (ode) {
144 objio_seg->oc.ods[c] = &ode->od; /* must use container_of */
145 return 0;
146 }
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300147
148 err = objlayout_get_deviceinfo(pnfslay, d_id, &deviceaddr, gfp_flags);
149 if (unlikely(err)) {
150 dprintk("%s: objlayout_get_deviceinfo dev(%llx:%llx) =>%d\n",
151 __func__, _DEVID_LO(d_id), _DEVID_HI(d_id), err);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700152 return err;
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300153 }
154
155 odi.systemid_len = deviceaddr->oda_systemid.len;
156 if (odi.systemid_len > sizeof(odi.systemid)) {
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700157 dprintk("%s: odi.systemid_len > sizeof(systemid=%zd)\n",
158 __func__, sizeof(odi.systemid));
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300159 err = -EINVAL;
160 goto out;
161 } else if (odi.systemid_len)
162 memcpy(odi.systemid, deviceaddr->oda_systemid.data,
163 odi.systemid_len);
164 odi.osdname_len = deviceaddr->oda_osdname.len;
165 odi.osdname = (u8 *)deviceaddr->oda_osdname.data;
166
167 if (!odi.osdname_len && !odi.systemid_len) {
168 dprintk("%s: !odi.osdname_len && !odi.systemid_len\n",
169 __func__);
170 err = -ENODEV;
171 goto out;
172 }
173
174 od = osduld_info_lookup(&odi);
175 if (unlikely(IS_ERR(od))) {
176 err = PTR_ERR(od);
177 dprintk("%s: osduld_info_lookup => %d\n", __func__, err);
178 goto out;
179 }
180
181 ode = _dev_list_add(NFS_SERVER(pnfslay->plh_inode), d_id, od,
182 gfp_flags);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700183 objio_seg->oc.ods[c] = &ode->od; /* must use container_of */
184 dprintk("Adding new dev_id(%llx:%llx)\n",
185 _DEVID_LO(d_id), _DEVID_HI(d_id));
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300186out:
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300187 objlayout_put_deviceinfo(deviceaddr);
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300188 return err;
189}
190
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700191static void copy_single_comp(struct ore_components *oc, unsigned c,
192 struct pnfs_osd_object_cred *src_comp)
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300193{
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700194 struct ore_comp *ocomp = &oc->comps[c];
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300195
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700196 WARN_ON(src_comp->oc_cap_key.cred_len > 0); /* libosd is NO_SEC only */
197 WARN_ON(src_comp->oc_cap.cred_len > sizeof(ocomp->cred));
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300198
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700199 ocomp->obj.partition = src_comp->oc_object_id.oid_partition_id;
200 ocomp->obj.id = src_comp->oc_object_id.oid_object_id;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300201
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700202 memcpy(ocomp->cred, src_comp->oc_cap.cred, sizeof(ocomp->cred));
203}
204
205int __alloc_objio_seg(unsigned numdevs, gfp_t gfp_flags,
206 struct objio_segment **pseg)
207{
Boaz Harrosh5318a292012-03-13 20:44:26 -0700208/* This is the in memory structure of the objio_segment
209 *
210 * struct __alloc_objio_segment {
211 * struct objio_segment olseg;
212 * struct ore_dev *ods[numdevs];
213 * struct ore_comp comps[numdevs];
214 * } *aolseg;
215 * NOTE: The code as above compiles and runs perfectly. It is elegant,
216 * type safe and compact. At some Past time Linus has decided he does not
217 * like variable length arrays, For the sake of this principal we uglify
218 * the code as below.
219 */
220 struct objio_segment *lseg;
221 size_t lseg_size = sizeof(*lseg) +
222 numdevs * sizeof(lseg->oc.ods[0]) +
223 numdevs * sizeof(*lseg->oc.comps);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700224
Boaz Harrosh5318a292012-03-13 20:44:26 -0700225 lseg = kzalloc(lseg_size, gfp_flags);
226 if (unlikely(!lseg)) {
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700227 dprintk("%s: Faild allocation numdevs=%d size=%zd\n", __func__,
Boaz Harrosh5318a292012-03-13 20:44:26 -0700228 numdevs, lseg_size);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700229 return -ENOMEM;
230 }
231
Boaz Harrosh5318a292012-03-13 20:44:26 -0700232 lseg->oc.numdevs = numdevs;
233 lseg->oc.single_comp = EC_MULTPLE_COMPS;
234 lseg->oc.ods = (void *)(lseg + 1);
235 lseg->oc.comps = (void *)(lseg->oc.ods + numdevs);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700236
Boaz Harrosh5318a292012-03-13 20:44:26 -0700237 *pseg = lseg;
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700238 return 0;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300239}
240
241int objio_alloc_lseg(struct pnfs_layout_segment **outp,
242 struct pnfs_layout_hdr *pnfslay,
243 struct pnfs_layout_range *range,
244 struct xdr_stream *xdr,
245 gfp_t gfp_flags)
246{
247 struct objio_segment *objio_seg;
248 struct pnfs_osd_xdr_decode_layout_iter iter;
249 struct pnfs_osd_layout layout;
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700250 struct pnfs_osd_object_cred src_comp;
251 unsigned cur_comp;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300252 int err;
253
254 err = pnfs_osd_xdr_decode_layout_map(&layout, &iter, xdr);
255 if (unlikely(err))
256 return err;
257
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700258 err = __alloc_objio_seg(layout.olo_num_comps, gfp_flags, &objio_seg);
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300259 if (unlikely(err))
260 return err;
261
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700262 objio_seg->layout.stripe_unit = layout.olo_map.odm_stripe_unit;
263 objio_seg->layout.group_width = layout.olo_map.odm_group_width;
264 objio_seg->layout.group_depth = layout.olo_map.odm_group_depth;
265 objio_seg->layout.mirrors_p1 = layout.olo_map.odm_mirror_cnt + 1;
266 objio_seg->layout.raid_algorithm = layout.olo_map.odm_raid_algorithm;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300267
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700268 err = ore_verify_layout(layout.olo_map.odm_num_comps,
269 &objio_seg->layout);
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300270 if (unlikely(err))
271 goto err;
272
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700273 objio_seg->oc.first_dev = layout.olo_comps_index;
274 cur_comp = 0;
275 while (pnfs_osd_xdr_decode_layout_comp(&src_comp, &iter, xdr, &err)) {
276 copy_single_comp(&objio_seg->oc, cur_comp, &src_comp);
277 err = objio_devices_lookup(pnfslay, objio_seg, cur_comp,
278 &src_comp.oc_object_id.oid_device_id,
279 gfp_flags);
280 if (err)
281 goto err;
282 ++cur_comp;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300283 }
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700284 /* pnfs_osd_xdr_decode_layout_comp returns false on error */
285 if (unlikely(err))
286 goto err;
Boaz Harrosh93420772011-05-25 21:25:29 +0300287
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300288 *outp = &objio_seg->lseg;
289 return 0;
290
291err:
292 kfree(objio_seg);
293 dprintk("%s: Error: return %d\n", __func__, err);
294 *outp = NULL;
295 return err;
296}
297
298void objio_free_lseg(struct pnfs_layout_segment *lseg)
299{
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300300 int i;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300301 struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
302
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700303 for (i = 0; i < objio_seg->oc.numdevs; i++) {
304 struct ore_dev *od = objio_seg->oc.ods[i];
305 struct objio_dev_ent *ode;
306
307 if (!od)
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300308 break;
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700309 ode = container_of(od, typeof(*ode), od);
310 nfs4_put_deviceid_node(&ode->id_node);
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300311 }
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300312 kfree(objio_seg);
313}
314
Boaz Harrosh96218552011-10-31 14:47:32 -0700315static int
Boaz Harrosheecfc632011-10-31 15:15:38 -0700316objio_alloc_io_state(struct pnfs_layout_hdr *pnfs_layout_type, bool is_reading,
Boaz Harrosh96218552011-10-31 14:47:32 -0700317 struct pnfs_layout_segment *lseg, struct page **pages, unsigned pgbase,
318 loff_t offset, size_t count, void *rpcdata, gfp_t gfp_flags,
319 struct objio_state **outp)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300320{
321 struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
Boaz Harrosheecfc632011-10-31 15:15:38 -0700322 struct ore_io_state *ios;
323 int ret;
Boaz Harrosh96218552011-10-31 14:47:32 -0700324 struct __alloc_objio_state {
325 struct objio_state objios;
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700326 struct pnfs_osd_ioerr ioerrs[objio_seg->oc.numdevs];
Boaz Harrosh96218552011-10-31 14:47:32 -0700327 } *aos;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300328
Boaz Harrosh96218552011-10-31 14:47:32 -0700329 aos = kzalloc(sizeof(*aos), gfp_flags);
330 if (unlikely(!aos))
Boaz Harrosh04f83452011-05-22 19:52:19 +0300331 return -ENOMEM;
332
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700333 objlayout_init_ioerrs(&aos->objios.oir, objio_seg->oc.numdevs,
Boaz Harrosh96218552011-10-31 14:47:32 -0700334 aos->ioerrs, rpcdata, pnfs_layout_type);
335
Boaz Harrosheecfc632011-10-31 15:15:38 -0700336 ret = ore_get_rw_state(&objio_seg->layout, &objio_seg->oc, is_reading,
337 offset, count, &ios);
338 if (unlikely(ret)) {
339 kfree(aos);
340 return ret;
341 }
342
Boaz Harrosh96218552011-10-31 14:47:32 -0700343 ios->pages = pages;
344 ios->pgbase = pgbase;
Boaz Harrosheecfc632011-10-31 15:15:38 -0700345 ios->private = aos;
Boaz Harrosh96218552011-10-31 14:47:32 -0700346 BUG_ON(ios->nr_pages > (pgbase + count + PAGE_SIZE - 1) >> PAGE_SHIFT);
347
Boaz Harrosheecfc632011-10-31 15:15:38 -0700348 aos->objios.sync = 0;
349 aos->objios.ios = ios;
350 *outp = &aos->objios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300351 return 0;
352}
353
Boaz Harroshe2e04352011-10-31 15:03:35 -0700354void objio_free_result(struct objlayout_io_res *oir)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300355{
Boaz Harrosheecfc632011-10-31 15:15:38 -0700356 struct objio_state *objios = container_of(oir, struct objio_state, oir);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300357
Boaz Harrosheecfc632011-10-31 15:15:38 -0700358 ore_put_io_state(objios->ios);
359 kfree(objios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300360}
361
Boaz Harroshadb58532011-05-26 21:49:46 +0300362enum pnfs_osd_errno osd_pri_2_pnfs_err(enum osd_err_priority oep)
363{
364 switch (oep) {
365 case OSD_ERR_PRI_NO_ERROR:
366 return (enum pnfs_osd_errno)0;
367
368 case OSD_ERR_PRI_CLEAR_PAGES:
369 BUG_ON(1);
370 return 0;
371
372 case OSD_ERR_PRI_RESOURCE:
373 return PNFS_OSD_ERR_RESOURCE;
374 case OSD_ERR_PRI_BAD_CRED:
375 return PNFS_OSD_ERR_BAD_CRED;
376 case OSD_ERR_PRI_NO_ACCESS:
377 return PNFS_OSD_ERR_NO_ACCESS;
378 case OSD_ERR_PRI_UNREACHABLE:
379 return PNFS_OSD_ERR_UNREACHABLE;
380 case OSD_ERR_PRI_NOT_FOUND:
381 return PNFS_OSD_ERR_NOT_FOUND;
382 case OSD_ERR_PRI_NO_SPACE:
383 return PNFS_OSD_ERR_NO_SPACE;
384 default:
385 WARN_ON(1);
386 /* fallthrough */
387 case OSD_ERR_PRI_EIO:
388 return PNFS_OSD_ERR_EIO;
389 }
390}
391
Boaz Harrosheecfc632011-10-31 15:15:38 -0700392static void __on_dev_error(struct ore_io_state *ios,
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700393 struct ore_dev *od, unsigned dev_index, enum osd_err_priority oep,
394 u64 dev_offset, u64 dev_len)
395{
396 struct objio_state *objios = ios->private;
397 struct pnfs_osd_objid pooid;
398 struct objio_dev_ent *ode = container_of(od, typeof(*ode), od);
399 /* FIXME: what to do with more-then-one-group layouts. We need to
400 * translate from ore_io_state index to oc->comps index
401 */
402 unsigned comp = dev_index;
403
404 pooid.oid_device_id = ode->id_node.deviceid;
405 pooid.oid_partition_id = ios->oc->comps[comp].obj.partition;
406 pooid.oid_object_id = ios->oc->comps[comp].obj.id;
407
408 objlayout_io_set_result(&objios->oir, comp,
409 &pooid, osd_pri_2_pnfs_err(oep),
Boaz Harrosheecfc632011-10-31 15:15:38 -0700410 dev_offset, dev_len, !ios->reading);
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700411}
412
Boaz Harrosh04f83452011-05-22 19:52:19 +0300413/*
414 * read
415 */
Boaz Harrosheecfc632011-10-31 15:15:38 -0700416static void _read_done(struct ore_io_state *ios, void *private)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300417{
Boaz Harrosheecfc632011-10-31 15:15:38 -0700418 struct objio_state *objios = private;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300419 ssize_t status;
Boaz Harrosheecfc632011-10-31 15:15:38 -0700420 int ret = ore_check_io(ios, &__on_dev_error);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300421
Boaz Harrosheecfc632011-10-31 15:15:38 -0700422 /* FIXME: _io_free(ios) can we dealocate the libosd resources; */
Boaz Harrosh04f83452011-05-22 19:52:19 +0300423
424 if (likely(!ret))
425 status = ios->length;
426 else
427 status = ret;
428
Boaz Harrosheecfc632011-10-31 15:15:38 -0700429 objlayout_read_done(&objios->oir, status, objios->sync);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300430}
431
Boaz Harrosh96218552011-10-31 14:47:32 -0700432int objio_read_pagelist(struct nfs_read_data *rdata)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300433{
Boaz Harrosheecfc632011-10-31 15:15:38 -0700434 struct objio_state *objios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300435 int ret;
436
Boaz Harrosheecfc632011-10-31 15:15:38 -0700437 ret = objio_alloc_io_state(NFS_I(rdata->inode)->layout, true,
Boaz Harrosh96218552011-10-31 14:47:32 -0700438 rdata->lseg, rdata->args.pages, rdata->args.pgbase,
439 rdata->args.offset, rdata->args.count, rdata,
Boaz Harrosheecfc632011-10-31 15:15:38 -0700440 GFP_KERNEL, &objios);
Boaz Harrosh96218552011-10-31 14:47:32 -0700441 if (unlikely(ret))
442 return ret;
443
Boaz Harrosheecfc632011-10-31 15:15:38 -0700444 objios->ios->done = _read_done;
445 dprintk("%s: offset=0x%llx length=0x%x\n", __func__,
446 rdata->args.offset, rdata->args.count);
447 return ore_read(objios->ios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300448}
449
450/*
451 * write
452 */
Boaz Harrosheecfc632011-10-31 15:15:38 -0700453static void _write_done(struct ore_io_state *ios, void *private)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300454{
Boaz Harrosheecfc632011-10-31 15:15:38 -0700455 struct objio_state *objios = private;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300456 ssize_t status;
Boaz Harrosheecfc632011-10-31 15:15:38 -0700457 int ret = ore_check_io(ios, &__on_dev_error);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300458
Boaz Harrosheecfc632011-10-31 15:15:38 -0700459 /* FIXME: _io_free(ios) can we dealocate the libosd resources; */
Boaz Harrosh04f83452011-05-22 19:52:19 +0300460
461 if (likely(!ret)) {
462 /* FIXME: should be based on the OSD's persistence model
463 * See OSD2r05 Section 4.13 Data persistence model */
Boaz Harrosheecfc632011-10-31 15:15:38 -0700464 objios->oir.committed = NFS_FILE_SYNC;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300465 status = ios->length;
466 } else {
467 status = ret;
468 }
469
Boaz Harrosheecfc632011-10-31 15:15:38 -0700470 objlayout_write_done(&objios->oir, status, objios->sync);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300471}
472
Boaz Harrosh278c0232011-10-31 15:16:54 -0700473static struct page *__r4w_get_page(void *priv, u64 offset, bool *uptodate)
474{
475 struct objio_state *objios = priv;
476 struct nfs_write_data *wdata = objios->oir.rpcdata;
477 pgoff_t index = offset / PAGE_SIZE;
478 struct page *page = find_get_page(wdata->inode->i_mapping, index);
479
480 if (!page) {
481 page = find_or_create_page(wdata->inode->i_mapping,
482 index, GFP_NOFS);
483 if (unlikely(!page)) {
484 dprintk("%s: grab_cache_page Failed index=0x%lx\n",
485 __func__, index);
486 return NULL;
487 }
488 unlock_page(page);
489 }
490 if (PageDirty(page) || PageWriteback(page))
491 *uptodate = true;
492 else
493 *uptodate = PageUptodate(page);
494 dprintk("%s: index=0x%lx uptodate=%d\n", __func__, index, *uptodate);
495 return page;
496}
497
498static void __r4w_put_page(void *priv, struct page *page)
499{
500 dprintk("%s: index=0x%lx\n", __func__, page->index);
501 page_cache_release(page);
502 return;
503}
504
505static const struct _ore_r4w_op _r4w_op = {
506 .get_page = &__r4w_get_page,
507 .put_page = &__r4w_put_page,
508};
509
Boaz Harrosh96218552011-10-31 14:47:32 -0700510int objio_write_pagelist(struct nfs_write_data *wdata, int how)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300511{
Boaz Harrosheecfc632011-10-31 15:15:38 -0700512 struct objio_state *objios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300513 int ret;
514
Boaz Harrosheecfc632011-10-31 15:15:38 -0700515 ret = objio_alloc_io_state(NFS_I(wdata->inode)->layout, false,
Boaz Harrosh96218552011-10-31 14:47:32 -0700516 wdata->lseg, wdata->args.pages, wdata->args.pgbase,
517 wdata->args.offset, wdata->args.count, wdata, GFP_NOFS,
Boaz Harrosheecfc632011-10-31 15:15:38 -0700518 &objios);
Boaz Harrosh96218552011-10-31 14:47:32 -0700519 if (unlikely(ret))
520 return ret;
521
Boaz Harrosheecfc632011-10-31 15:15:38 -0700522 objios->sync = 0 != (how & FLUSH_SYNC);
Boaz Harrosh278c0232011-10-31 15:16:54 -0700523 objios->ios->r4w = &_r4w_op;
Boaz Harrosh96218552011-10-31 14:47:32 -0700524
Boaz Harrosheecfc632011-10-31 15:15:38 -0700525 if (!objios->sync)
526 objios->ios->done = _write_done;
527
528 dprintk("%s: offset=0x%llx length=0x%x\n", __func__,
529 wdata->args.offset, wdata->args.count);
530 ret = ore_write(objios->ios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300531 if (unlikely(ret))
532 return ret;
533
Boaz Harrosheecfc632011-10-31 15:15:38 -0700534 if (objios->sync)
535 _write_done(objios->ios, objios);
536
537 return 0;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300538}
539
Boaz Harrosh93420772011-05-25 21:25:29 +0300540static bool objio_pg_test(struct nfs_pageio_descriptor *pgio,
541 struct nfs_page *prev, struct nfs_page *req)
542{
543 if (!pnfs_generic_pg_test(pgio, prev, req))
544 return false;
545
546 return pgio->pg_count + req->wb_bytes <=
Boaz Harroshaf4f5b52011-10-31 15:04:19 -0700547 OBJIO_LSEG(pgio->pg_lseg)->layout.max_io_length;
Boaz Harrosh93420772011-05-25 21:25:29 +0300548}
549
Trond Myklebust1751c362011-06-10 13:30:23 -0400550static const struct nfs_pageio_ops objio_pg_read_ops = {
Trond Myklebustd8007d42011-06-10 13:30:23 -0400551 .pg_init = pnfs_generic_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -0400552 .pg_test = objio_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -0400553 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400554};
555
556static const struct nfs_pageio_ops objio_pg_write_ops = {
Trond Myklebustd8007d42011-06-10 13:30:23 -0400557 .pg_init = pnfs_generic_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -0400558 .pg_test = objio_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -0400559 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -0400560};
561
Benny Halevyc93407d2011-05-22 19:49:06 +0300562static struct pnfs_layoutdriver_type objlayout_type = {
563 .id = LAYOUT_OSD2_OBJECTS,
564 .name = "LAYOUT_OSD2_OBJECTS",
Boaz Harroshfe0fe832012-01-06 09:31:20 +0200565 .flags = PNFS_LAYOUTRET_ON_SETATTR |
566 PNFS_LAYOUTRET_ON_ERROR,
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300567
Benny Halevye51b8412011-05-22 19:51:48 +0300568 .alloc_layout_hdr = objlayout_alloc_layout_hdr,
569 .free_layout_hdr = objlayout_free_layout_hdr,
570
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300571 .alloc_lseg = objlayout_alloc_lseg,
572 .free_lseg = objlayout_free_lseg,
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300573
Boaz Harrosh04f83452011-05-22 19:52:19 +0300574 .read_pagelist = objlayout_read_pagelist,
575 .write_pagelist = objlayout_write_pagelist,
Trond Myklebust1751c362011-06-10 13:30:23 -0400576 .pg_read_ops = &objio_pg_read_ops,
577 .pg_write_ops = &objio_pg_write_ops,
Boaz Harrosh04f83452011-05-22 19:52:19 +0300578
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300579 .free_deviceid_node = objio_free_deviceid_node,
Boaz Harroshadb58532011-05-26 21:49:46 +0300580
Boaz Harrosha0fe8bf2011-05-22 19:54:13 +0300581 .encode_layoutcommit = objlayout_encode_layoutcommit,
Boaz Harroshadb58532011-05-26 21:49:46 +0300582 .encode_layoutreturn = objlayout_encode_layoutreturn,
Benny Halevyc93407d2011-05-22 19:49:06 +0300583};
584
585MODULE_DESCRIPTION("pNFS Layout Driver for OSD2 objects");
586MODULE_AUTHOR("Benny Halevy <bhalevy@panasas.com>");
587MODULE_LICENSE("GPL");
588
589static int __init
590objlayout_init(void)
591{
592 int ret = pnfs_register_layoutdriver(&objlayout_type);
593
594 if (ret)
595 printk(KERN_INFO
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500596 "NFS: %s: Registering OSD pNFS Layout Driver failed: error=%d\n",
Benny Halevyc93407d2011-05-22 19:49:06 +0300597 __func__, ret);
598 else
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500599 printk(KERN_INFO "NFS: %s: Registered OSD pNFS Layout Driver\n",
Benny Halevyc93407d2011-05-22 19:49:06 +0300600 __func__);
601 return ret;
602}
603
604static void __exit
605objlayout_exit(void)
606{
607 pnfs_unregister_layoutdriver(&objlayout_type);
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500608 printk(KERN_INFO "NFS: %s: Unregistered OSD pNFS Layout Driver\n",
Benny Halevyc93407d2011-05-22 19:49:06 +0300609 __func__);
610}
611
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -0400612MODULE_ALIAS("nfs-layouttype4-2");
613
Benny Halevyc93407d2011-05-22 19:49:06 +0300614module_init(objlayout_init);
615module_exit(objlayout_exit);