blob: aa8663a8938fa3e6030bb29e0e368902dde23a6a [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 Harrosh09f5bf42011-05-22 19:50:20 +030041#include <scsi/osd_initiator.h>
42
43#include "objlayout.h"
44
45#define NFSDBG_FACILITY NFSDBG_PNFS_LD
46
47#define _LLU(x) ((unsigned long long)x)
48
Boaz Harrosh04f83452011-05-22 19:52:19 +030049enum { BIO_MAX_PAGES_KMALLOC =
50 (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),
51};
52
Boaz Harroshb6c05f12011-05-26 21:45:34 +030053struct objio_dev_ent {
54 struct nfs4_deviceid_node id_node;
55 struct osd_dev *od;
56};
57
58static void
59objio_free_deviceid_node(struct nfs4_deviceid_node *d)
60{
61 struct objio_dev_ent *de = container_of(d, struct objio_dev_ent, id_node);
62
63 dprintk("%s: free od=%p\n", __func__, de->od);
64 osduld_put_device(de->od);
65 kfree(de);
66}
67
68static struct objio_dev_ent *_dev_list_find(const struct nfs_server *nfss,
69 const struct nfs4_deviceid *d_id)
70{
71 struct nfs4_deviceid_node *d;
72 struct objio_dev_ent *de;
73
74 d = nfs4_find_get_deviceid(nfss->pnfs_curr_ld, nfss->nfs_client, d_id);
75 if (!d)
76 return NULL;
77
78 de = container_of(d, struct objio_dev_ent, id_node);
79 return de;
80}
81
82static struct objio_dev_ent *
83_dev_list_add(const struct nfs_server *nfss,
84 const struct nfs4_deviceid *d_id, struct osd_dev *od,
85 gfp_t gfp_flags)
86{
87 struct nfs4_deviceid_node *d;
88 struct objio_dev_ent *de = kzalloc(sizeof(*de), gfp_flags);
89 struct objio_dev_ent *n;
90
91 if (!de) {
92 dprintk("%s: -ENOMEM od=%p\n", __func__, od);
93 return NULL;
94 }
95
96 dprintk("%s: Adding od=%p\n", __func__, od);
97 nfs4_init_deviceid_node(&de->id_node,
98 nfss->pnfs_curr_ld,
99 nfss->nfs_client,
100 d_id);
101 de->od = od;
102
103 d = nfs4_insert_deviceid_node(&de->id_node);
104 n = container_of(d, struct objio_dev_ent, id_node);
105 if (n != de) {
106 dprintk("%s: Race with other n->od=%p\n", __func__, n->od);
107 objio_free_deviceid_node(&de->id_node);
108 de = n;
109 }
110
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300111 return de;
112}
113
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300114struct caps_buffers {
115 u8 caps_key[OSD_CRYPTO_KEYID_SIZE];
116 u8 creds[OSD_CAP_LEN];
117};
118
119struct objio_segment {
120 struct pnfs_layout_segment lseg;
121
122 struct pnfs_osd_object_cred *comps;
123
124 unsigned mirrors_p1;
125 unsigned stripe_unit;
126 unsigned group_width; /* Data stripe_units without integrity comps */
127 u64 group_depth;
128 unsigned group_count;
129
Boaz Harrosh93420772011-05-25 21:25:29 +0300130 unsigned max_io_size;
131
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300132 unsigned comps_index;
133 unsigned num_comps;
134 /* variable length */
135 struct objio_dev_ent *ods[];
136};
137
138static inline struct objio_segment *
139OBJIO_LSEG(struct pnfs_layout_segment *lseg)
140{
141 return container_of(lseg, struct objio_segment, lseg);
142}
143
Boaz Harrosh04f83452011-05-22 19:52:19 +0300144struct objio_state;
145typedef ssize_t (*objio_done_fn)(struct objio_state *ios);
146
147struct objio_state {
148 /* Generic layer */
149 struct objlayout_io_state ol_state;
150
151 struct objio_segment *layout;
152
153 struct kref kref;
154 objio_done_fn done;
155 void *private;
156
157 unsigned long length;
158 unsigned numdevs; /* Actually used devs in this IO */
159 /* A per-device variable array of size numdevs */
160 struct _objio_per_comp {
161 struct bio *bio;
162 struct osd_request *or;
163 unsigned long length;
164 u64 offset;
165 unsigned dev;
166 } per_dev[];
167};
168
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300169/* Send and wait for a get_device_info of devices in the layout,
170 then look them up with the osd_initiator library */
171static struct objio_dev_ent *_device_lookup(struct pnfs_layout_hdr *pnfslay,
172 struct objio_segment *objio_seg, unsigned comp,
173 gfp_t gfp_flags)
174{
175 struct pnfs_osd_deviceaddr *deviceaddr;
176 struct nfs4_deviceid *d_id;
177 struct objio_dev_ent *ode;
178 struct osd_dev *od;
179 struct osd_dev_info odi;
180 int err;
181
182 d_id = &objio_seg->comps[comp].oc_object_id.oid_device_id;
183
184 ode = _dev_list_find(NFS_SERVER(pnfslay->plh_inode), d_id);
185 if (ode)
186 return ode;
187
188 err = objlayout_get_deviceinfo(pnfslay, d_id, &deviceaddr, gfp_flags);
189 if (unlikely(err)) {
190 dprintk("%s: objlayout_get_deviceinfo dev(%llx:%llx) =>%d\n",
191 __func__, _DEVID_LO(d_id), _DEVID_HI(d_id), err);
192 return ERR_PTR(err);
193 }
194
195 odi.systemid_len = deviceaddr->oda_systemid.len;
196 if (odi.systemid_len > sizeof(odi.systemid)) {
197 err = -EINVAL;
198 goto out;
199 } else if (odi.systemid_len)
200 memcpy(odi.systemid, deviceaddr->oda_systemid.data,
201 odi.systemid_len);
202 odi.osdname_len = deviceaddr->oda_osdname.len;
203 odi.osdname = (u8 *)deviceaddr->oda_osdname.data;
204
205 if (!odi.osdname_len && !odi.systemid_len) {
206 dprintk("%s: !odi.osdname_len && !odi.systemid_len\n",
207 __func__);
208 err = -ENODEV;
209 goto out;
210 }
211
212 od = osduld_info_lookup(&odi);
213 if (unlikely(IS_ERR(od))) {
214 err = PTR_ERR(od);
215 dprintk("%s: osduld_info_lookup => %d\n", __func__, err);
216 goto out;
217 }
218
219 ode = _dev_list_add(NFS_SERVER(pnfslay->plh_inode), d_id, od,
220 gfp_flags);
221
222out:
223 dprintk("%s: return=%d\n", __func__, err);
224 objlayout_put_deviceinfo(deviceaddr);
225 return err ? ERR_PTR(err) : ode;
226}
227
228static int objio_devices_lookup(struct pnfs_layout_hdr *pnfslay,
229 struct objio_segment *objio_seg,
230 gfp_t gfp_flags)
231{
232 unsigned i;
233 int err;
234
235 /* lookup all devices */
236 for (i = 0; i < objio_seg->num_comps; i++) {
237 struct objio_dev_ent *ode;
238
239 ode = _device_lookup(pnfslay, objio_seg, i, gfp_flags);
240 if (unlikely(IS_ERR(ode))) {
241 err = PTR_ERR(ode);
242 goto out;
243 }
244 objio_seg->ods[i] = ode;
245 }
246 err = 0;
247
248out:
249 dprintk("%s: return=%d\n", __func__, err);
250 return err;
251}
252
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300253static int _verify_data_map(struct pnfs_osd_layout *layout)
254{
255 struct pnfs_osd_data_map *data_map = &layout->olo_map;
256 u64 stripe_length;
257 u32 group_width;
258
259/* FIXME: Only raid0 for now. if not go through MDS */
260 if (data_map->odm_raid_algorithm != PNFS_OSD_RAID_0) {
261 printk(KERN_ERR "Only RAID_0 for now\n");
262 return -ENOTSUPP;
263 }
264 if (0 != (data_map->odm_num_comps % (data_map->odm_mirror_cnt + 1))) {
265 printk(KERN_ERR "Data Map wrong, num_comps=%u mirrors=%u\n",
266 data_map->odm_num_comps, data_map->odm_mirror_cnt);
267 return -EINVAL;
268 }
269
270 if (data_map->odm_group_width)
271 group_width = data_map->odm_group_width;
272 else
273 group_width = data_map->odm_num_comps /
274 (data_map->odm_mirror_cnt + 1);
275
276 stripe_length = (u64)data_map->odm_stripe_unit * group_width;
277 if (stripe_length >= (1ULL << 32)) {
278 printk(KERN_ERR "Total Stripe length(0x%llx)"
279 " >= 32bit is not supported\n", _LLU(stripe_length));
280 return -ENOTSUPP;
281 }
282
283 if (0 != (data_map->odm_stripe_unit & ~PAGE_MASK)) {
284 printk(KERN_ERR "Stripe Unit(0x%llx)"
285 " must be Multples of PAGE_SIZE(0x%lx)\n",
286 _LLU(data_map->odm_stripe_unit), PAGE_SIZE);
287 return -ENOTSUPP;
288 }
289
290 return 0;
291}
292
293static void copy_single_comp(struct pnfs_osd_object_cred *cur_comp,
294 struct pnfs_osd_object_cred *src_comp,
295 struct caps_buffers *caps_p)
296{
297 WARN_ON(src_comp->oc_cap_key.cred_len > sizeof(caps_p->caps_key));
298 WARN_ON(src_comp->oc_cap.cred_len > sizeof(caps_p->creds));
299
300 *cur_comp = *src_comp;
301
302 memcpy(caps_p->caps_key, src_comp->oc_cap_key.cred,
303 sizeof(caps_p->caps_key));
304 cur_comp->oc_cap_key.cred = caps_p->caps_key;
305
306 memcpy(caps_p->creds, src_comp->oc_cap.cred,
307 sizeof(caps_p->creds));
308 cur_comp->oc_cap.cred = caps_p->creds;
309}
310
311int objio_alloc_lseg(struct pnfs_layout_segment **outp,
312 struct pnfs_layout_hdr *pnfslay,
313 struct pnfs_layout_range *range,
314 struct xdr_stream *xdr,
315 gfp_t gfp_flags)
316{
317 struct objio_segment *objio_seg;
318 struct pnfs_osd_xdr_decode_layout_iter iter;
319 struct pnfs_osd_layout layout;
320 struct pnfs_osd_object_cred *cur_comp, src_comp;
321 struct caps_buffers *caps_p;
322 int err;
323
324 err = pnfs_osd_xdr_decode_layout_map(&layout, &iter, xdr);
325 if (unlikely(err))
326 return err;
327
328 err = _verify_data_map(&layout);
329 if (unlikely(err))
330 return err;
331
332 objio_seg = kzalloc(sizeof(*objio_seg) +
333 sizeof(objio_seg->ods[0]) * layout.olo_num_comps +
334 sizeof(*objio_seg->comps) * layout.olo_num_comps +
335 sizeof(struct caps_buffers) * layout.olo_num_comps,
336 gfp_flags);
337 if (!objio_seg)
338 return -ENOMEM;
339
340 objio_seg->comps = (void *)(objio_seg->ods + layout.olo_num_comps);
341 cur_comp = objio_seg->comps;
342 caps_p = (void *)(cur_comp + layout.olo_num_comps);
343 while (pnfs_osd_xdr_decode_layout_comp(&src_comp, &iter, xdr, &err))
344 copy_single_comp(cur_comp++, &src_comp, caps_p++);
345 if (unlikely(err))
346 goto err;
347
348 objio_seg->num_comps = layout.olo_num_comps;
349 objio_seg->comps_index = layout.olo_comps_index;
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300350 err = objio_devices_lookup(pnfslay, objio_seg, gfp_flags);
351 if (err)
352 goto err;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300353
354 objio_seg->mirrors_p1 = layout.olo_map.odm_mirror_cnt + 1;
355 objio_seg->stripe_unit = layout.olo_map.odm_stripe_unit;
356 if (layout.olo_map.odm_group_width) {
357 objio_seg->group_width = layout.olo_map.odm_group_width;
358 objio_seg->group_depth = layout.olo_map.odm_group_depth;
359 objio_seg->group_count = layout.olo_map.odm_num_comps /
360 objio_seg->mirrors_p1 /
361 objio_seg->group_width;
362 } else {
363 objio_seg->group_width = layout.olo_map.odm_num_comps /
364 objio_seg->mirrors_p1;
365 objio_seg->group_depth = -1;
366 objio_seg->group_count = 1;
367 }
368
Boaz Harrosh93420772011-05-25 21:25:29 +0300369 /* Cache this calculation it will hit for every page */
370 objio_seg->max_io_size = (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE -
371 objio_seg->stripe_unit) *
372 objio_seg->group_width;
373
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300374 *outp = &objio_seg->lseg;
375 return 0;
376
377err:
378 kfree(objio_seg);
379 dprintk("%s: Error: return %d\n", __func__, err);
380 *outp = NULL;
381 return err;
382}
383
384void objio_free_lseg(struct pnfs_layout_segment *lseg)
385{
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300386 int i;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300387 struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
388
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300389 for (i = 0; i < objio_seg->num_comps; i++) {
390 if (!objio_seg->ods[i])
391 break;
392 nfs4_put_deviceid_node(&objio_seg->ods[i]->id_node);
393 }
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300394 kfree(objio_seg);
395}
396
Boaz Harrosh04f83452011-05-22 19:52:19 +0300397int objio_alloc_io_state(struct pnfs_layout_segment *lseg,
398 struct objlayout_io_state **outp,
399 gfp_t gfp_flags)
400{
401 struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
402 struct objio_state *ios;
403 const unsigned first_size = sizeof(*ios) +
404 objio_seg->num_comps * sizeof(ios->per_dev[0]);
Boaz Harroshadb58532011-05-26 21:49:46 +0300405 const unsigned sec_size = objio_seg->num_comps *
406 sizeof(ios->ol_state.ioerrs[0]);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300407
Boaz Harroshadb58532011-05-26 21:49:46 +0300408 ios = kzalloc(first_size + sec_size, gfp_flags);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300409 if (unlikely(!ios))
410 return -ENOMEM;
411
412 ios->layout = objio_seg;
Boaz Harroshadb58532011-05-26 21:49:46 +0300413 ios->ol_state.ioerrs = ((void *)ios) + first_size;
414 ios->ol_state.num_comps = objio_seg->num_comps;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300415
416 *outp = &ios->ol_state;
417 return 0;
418}
419
420void objio_free_io_state(struct objlayout_io_state *ol_state)
421{
422 struct objio_state *ios = container_of(ol_state, struct objio_state,
423 ol_state);
424
425 kfree(ios);
426}
427
Boaz Harroshadb58532011-05-26 21:49:46 +0300428enum pnfs_osd_errno osd_pri_2_pnfs_err(enum osd_err_priority oep)
429{
430 switch (oep) {
431 case OSD_ERR_PRI_NO_ERROR:
432 return (enum pnfs_osd_errno)0;
433
434 case OSD_ERR_PRI_CLEAR_PAGES:
435 BUG_ON(1);
436 return 0;
437
438 case OSD_ERR_PRI_RESOURCE:
439 return PNFS_OSD_ERR_RESOURCE;
440 case OSD_ERR_PRI_BAD_CRED:
441 return PNFS_OSD_ERR_BAD_CRED;
442 case OSD_ERR_PRI_NO_ACCESS:
443 return PNFS_OSD_ERR_NO_ACCESS;
444 case OSD_ERR_PRI_UNREACHABLE:
445 return PNFS_OSD_ERR_UNREACHABLE;
446 case OSD_ERR_PRI_NOT_FOUND:
447 return PNFS_OSD_ERR_NOT_FOUND;
448 case OSD_ERR_PRI_NO_SPACE:
449 return PNFS_OSD_ERR_NO_SPACE;
450 default:
451 WARN_ON(1);
452 /* fallthrough */
453 case OSD_ERR_PRI_EIO:
454 return PNFS_OSD_ERR_EIO;
455 }
456}
457
Boaz Harrosh04f83452011-05-22 19:52:19 +0300458static void _clear_bio(struct bio *bio)
459{
460 struct bio_vec *bv;
461 unsigned i;
462
463 __bio_for_each_segment(bv, bio, i, 0) {
464 unsigned this_count = bv->bv_len;
465
466 if (likely(PAGE_SIZE == this_count))
467 clear_highpage(bv->bv_page);
468 else
469 zero_user(bv->bv_page, bv->bv_offset, this_count);
470 }
471}
472
473static int _io_check(struct objio_state *ios, bool is_write)
474{
475 enum osd_err_priority oep = OSD_ERR_PRI_NO_ERROR;
476 int lin_ret = 0;
477 int i;
478
479 for (i = 0; i < ios->numdevs; i++) {
480 struct osd_sense_info osi;
481 struct osd_request *or = ios->per_dev[i].or;
482 unsigned dev;
483 int ret;
484
485 if (!or)
486 continue;
487
488 ret = osd_req_decode_sense(or, &osi);
489 if (likely(!ret))
490 continue;
491
492 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
493 /* start read offset passed endof file */
494 BUG_ON(is_write);
495 _clear_bio(ios->per_dev[i].bio);
496 dprintk("%s: start read offset passed end of file "
497 "offset=0x%llx, length=0x%lx\n", __func__,
498 _LLU(ios->per_dev[i].offset),
499 ios->per_dev[i].length);
500
501 continue; /* we recovered */
502 }
503 dev = ios->per_dev[i].dev;
Boaz Harroshadb58532011-05-26 21:49:46 +0300504 objlayout_io_set_result(&ios->ol_state, dev,
505 &ios->layout->comps[dev].oc_object_id,
506 osd_pri_2_pnfs_err(osi.osd_err_pri),
507 ios->per_dev[i].offset,
508 ios->per_dev[i].length,
509 is_write);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300510
511 if (osi.osd_err_pri >= oep) {
512 oep = osi.osd_err_pri;
513 lin_ret = ret;
514 }
515 }
516
517 return lin_ret;
518}
519
520/*
521 * Common IO state helpers.
522 */
523static void _io_free(struct objio_state *ios)
524{
525 unsigned i;
526
527 for (i = 0; i < ios->numdevs; i++) {
528 struct _objio_per_comp *per_dev = &ios->per_dev[i];
529
530 if (per_dev->or) {
531 osd_end_request(per_dev->or);
532 per_dev->or = NULL;
533 }
534
535 if (per_dev->bio) {
536 bio_put(per_dev->bio);
537 per_dev->bio = NULL;
538 }
539 }
540}
541
542struct osd_dev *_io_od(struct objio_state *ios, unsigned dev)
543{
544 unsigned min_dev = ios->layout->comps_index;
545 unsigned max_dev = min_dev + ios->layout->num_comps;
546
547 BUG_ON(dev < min_dev || max_dev <= dev);
548 return ios->layout->ods[dev - min_dev]->od;
549}
550
551struct _striping_info {
552 u64 obj_offset;
553 u64 group_length;
554 unsigned dev;
555 unsigned unit_off;
556};
557
558static void _calc_stripe_info(struct objio_state *ios, u64 file_offset,
559 struct _striping_info *si)
560{
561 u32 stripe_unit = ios->layout->stripe_unit;
562 u32 group_width = ios->layout->group_width;
563 u64 group_depth = ios->layout->group_depth;
564 u32 U = stripe_unit * group_width;
565
566 u64 T = U * group_depth;
567 u64 S = T * ios->layout->group_count;
568 u64 M = div64_u64(file_offset, S);
569
570 /*
571 G = (L - (M * S)) / T
572 H = (L - (M * S)) % T
573 */
574 u64 LmodU = file_offset - M * S;
575 u32 G = div64_u64(LmodU, T);
576 u64 H = LmodU - G * T;
577
578 u32 N = div_u64(H, U);
579
580 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
581 si->obj_offset = si->unit_off + (N * stripe_unit) +
582 (M * group_depth * stripe_unit);
583
584 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
585 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
586 si->dev *= ios->layout->mirrors_p1;
587
588 si->group_length = T - H;
589}
590
591static int _add_stripe_unit(struct objio_state *ios, unsigned *cur_pg,
Boaz Harrosh20618b22011-08-03 21:54:33 -0700592 unsigned pgbase, struct _objio_per_comp *per_dev, int len,
Boaz Harrosh04f83452011-05-22 19:52:19 +0300593 gfp_t gfp_flags)
594{
595 unsigned pg = *cur_pg;
Boaz Harrosh20618b22011-08-03 21:54:33 -0700596 int cur_len = len;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300597 struct request_queue *q =
598 osd_request_queue(_io_od(ios, per_dev->dev));
599
Boaz Harrosh04f83452011-05-22 19:52:19 +0300600 if (per_dev->bio == NULL) {
Boaz Harrosh20618b22011-08-03 21:54:33 -0700601 unsigned pages_in_stripe = ios->layout->group_width *
Boaz Harrosh04f83452011-05-22 19:52:19 +0300602 (ios->layout->stripe_unit / PAGE_SIZE);
603 unsigned bio_size = (ios->ol_state.nr_pages + pages_in_stripe) /
Boaz Harrosh20618b22011-08-03 21:54:33 -0700604 ios->layout->group_width;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300605
606 if (BIO_MAX_PAGES_KMALLOC < bio_size)
607 bio_size = BIO_MAX_PAGES_KMALLOC;
608
609 per_dev->bio = bio_kmalloc(gfp_flags, bio_size);
610 if (unlikely(!per_dev->bio)) {
611 dprintk("Faild to allocate BIO size=%u\n", bio_size);
612 return -ENOMEM;
613 }
614 }
615
616 while (cur_len > 0) {
617 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
618 unsigned added_len;
619
620 BUG_ON(ios->ol_state.nr_pages <= pg);
621 cur_len -= pglen;
622
623 added_len = bio_add_pc_page(q, per_dev->bio,
624 ios->ol_state.pages[pg], pglen, pgbase);
625 if (unlikely(pglen != added_len))
626 return -ENOMEM;
627 pgbase = 0;
628 ++pg;
629 }
630 BUG_ON(cur_len);
631
Boaz Harrosh20618b22011-08-03 21:54:33 -0700632 per_dev->length += len;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300633 *cur_pg = pg;
634 return 0;
635}
636
637static int _prepare_one_group(struct objio_state *ios, u64 length,
638 struct _striping_info *si, unsigned *last_pg,
639 gfp_t gfp_flags)
640{
641 unsigned stripe_unit = ios->layout->stripe_unit;
642 unsigned mirrors_p1 = ios->layout->mirrors_p1;
643 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
644 unsigned dev = si->dev;
645 unsigned first_dev = dev - (dev % devs_in_group);
646 unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
647 unsigned cur_pg = *last_pg;
648 int ret = 0;
649
650 while (length) {
651 struct _objio_per_comp *per_dev = &ios->per_dev[dev];
652 unsigned cur_len, page_off = 0;
653
654 if (!per_dev->length) {
655 per_dev->dev = dev;
656 if (dev < si->dev) {
657 per_dev->offset = si->obj_offset + stripe_unit -
658 si->unit_off;
659 cur_len = stripe_unit;
660 } else if (dev == si->dev) {
661 per_dev->offset = si->obj_offset;
662 cur_len = stripe_unit - si->unit_off;
663 page_off = si->unit_off & ~PAGE_MASK;
664 BUG_ON(page_off &&
665 (page_off != ios->ol_state.pgbase));
666 } else { /* dev > si->dev */
667 per_dev->offset = si->obj_offset - si->unit_off;
668 cur_len = stripe_unit;
669 }
670
671 if (max_comp < dev)
672 max_comp = dev;
673 } else {
674 cur_len = stripe_unit;
675 }
676 if (cur_len >= length)
677 cur_len = length;
678
679 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
680 cur_len, gfp_flags);
681 if (unlikely(ret))
682 goto out;
683
684 dev += mirrors_p1;
685 dev = (dev % devs_in_group) + first_dev;
686
687 length -= cur_len;
688 ios->length += cur_len;
689 }
690out:
691 ios->numdevs = max_comp + mirrors_p1;
692 *last_pg = cur_pg;
693 return ret;
694}
695
696static int _io_rw_pagelist(struct objio_state *ios, gfp_t gfp_flags)
697{
698 u64 length = ios->ol_state.count;
699 u64 offset = ios->ol_state.offset;
700 struct _striping_info si;
701 unsigned last_pg = 0;
702 int ret = 0;
703
704 while (length) {
705 _calc_stripe_info(ios, offset, &si);
706
707 if (length < si.group_length)
708 si.group_length = length;
709
710 ret = _prepare_one_group(ios, si.group_length, &si, &last_pg, gfp_flags);
711 if (unlikely(ret))
712 goto out;
713
714 offset += si.group_length;
715 length -= si.group_length;
716 }
717
718out:
719 if (!ios->length)
720 return ret;
721
722 return 0;
723}
724
725static ssize_t _sync_done(struct objio_state *ios)
726{
727 struct completion *waiting = ios->private;
728
729 complete(waiting);
730 return 0;
731}
732
733static void _last_io(struct kref *kref)
734{
735 struct objio_state *ios = container_of(kref, struct objio_state, kref);
736
737 ios->done(ios);
738}
739
740static void _done_io(struct osd_request *or, void *p)
741{
742 struct objio_state *ios = p;
743
744 kref_put(&ios->kref, _last_io);
745}
746
747static ssize_t _io_exec(struct objio_state *ios)
748{
749 DECLARE_COMPLETION_ONSTACK(wait);
750 ssize_t status = 0; /* sync status */
751 unsigned i;
752 objio_done_fn saved_done_fn = ios->done;
753 bool sync = ios->ol_state.sync;
754
755 if (sync) {
756 ios->done = _sync_done;
757 ios->private = &wait;
758 }
759
760 kref_init(&ios->kref);
761
762 for (i = 0; i < ios->numdevs; i++) {
763 struct osd_request *or = ios->per_dev[i].or;
764
765 if (!or)
766 continue;
767
768 kref_get(&ios->kref);
769 osd_execute_request_async(or, _done_io, ios);
770 }
771
772 kref_put(&ios->kref, _last_io);
773
774 if (sync) {
775 wait_for_completion(&wait);
776 status = saved_done_fn(ios);
777 }
778
779 return status;
780}
781
782/*
783 * read
784 */
785static ssize_t _read_done(struct objio_state *ios)
786{
787 ssize_t status;
788 int ret = _io_check(ios, false);
789
790 _io_free(ios);
791
792 if (likely(!ret))
793 status = ios->length;
794 else
795 status = ret;
796
797 objlayout_read_done(&ios->ol_state, status, ios->ol_state.sync);
798 return status;
799}
800
801static int _read_mirrors(struct objio_state *ios, unsigned cur_comp)
802{
803 struct osd_request *or = NULL;
804 struct _objio_per_comp *per_dev = &ios->per_dev[cur_comp];
805 unsigned dev = per_dev->dev;
806 struct pnfs_osd_object_cred *cred =
807 &ios->layout->comps[dev];
808 struct osd_obj_id obj = {
809 .partition = cred->oc_object_id.oid_partition_id,
810 .id = cred->oc_object_id.oid_object_id,
811 };
812 int ret;
813
814 or = osd_start_request(_io_od(ios, dev), GFP_KERNEL);
815 if (unlikely(!or)) {
816 ret = -ENOMEM;
817 goto err;
818 }
819 per_dev->or = or;
820
821 osd_req_read(or, &obj, per_dev->offset, per_dev->bio, per_dev->length);
822
823 ret = osd_finalize_request(or, 0, cred->oc_cap.cred, NULL);
824 if (ret) {
825 dprintk("%s: Faild to osd_finalize_request() => %d\n",
826 __func__, ret);
827 goto err;
828 }
829
830 dprintk("%s:[%d] dev=%d obj=0x%llx start=0x%llx length=0x%lx\n",
831 __func__, cur_comp, dev, obj.id, _LLU(per_dev->offset),
832 per_dev->length);
833
834err:
835 return ret;
836}
837
838static ssize_t _read_exec(struct objio_state *ios)
839{
840 unsigned i;
841 int ret;
842
843 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
844 if (!ios->per_dev[i].length)
845 continue;
846 ret = _read_mirrors(ios, i);
847 if (unlikely(ret))
848 goto err;
849 }
850
851 ios->done = _read_done;
852 return _io_exec(ios); /* In sync mode exec returns the io status */
853
854err:
855 _io_free(ios);
856 return ret;
857}
858
859ssize_t objio_read_pagelist(struct objlayout_io_state *ol_state)
860{
861 struct objio_state *ios = container_of(ol_state, struct objio_state,
862 ol_state);
863 int ret;
864
865 ret = _io_rw_pagelist(ios, GFP_KERNEL);
866 if (unlikely(ret))
867 return ret;
868
869 return _read_exec(ios);
870}
871
872/*
873 * write
874 */
875static ssize_t _write_done(struct objio_state *ios)
876{
877 ssize_t status;
878 int ret = _io_check(ios, true);
879
880 _io_free(ios);
881
882 if (likely(!ret)) {
883 /* FIXME: should be based on the OSD's persistence model
884 * See OSD2r05 Section 4.13 Data persistence model */
885 ios->ol_state.committed = NFS_FILE_SYNC;
886 status = ios->length;
887 } else {
888 status = ret;
889 }
890
891 objlayout_write_done(&ios->ol_state, status, ios->ol_state.sync);
892 return status;
893}
894
895static int _write_mirrors(struct objio_state *ios, unsigned cur_comp)
896{
897 struct _objio_per_comp *master_dev = &ios->per_dev[cur_comp];
898 unsigned dev = ios->per_dev[cur_comp].dev;
899 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
900 int ret;
901
902 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
903 struct osd_request *or = NULL;
904 struct pnfs_osd_object_cred *cred =
905 &ios->layout->comps[dev];
906 struct osd_obj_id obj = {
907 .partition = cred->oc_object_id.oid_partition_id,
908 .id = cred->oc_object_id.oid_object_id,
909 };
910 struct _objio_per_comp *per_dev = &ios->per_dev[cur_comp];
911 struct bio *bio;
912
913 or = osd_start_request(_io_od(ios, dev), GFP_NOFS);
914 if (unlikely(!or)) {
915 ret = -ENOMEM;
916 goto err;
917 }
918 per_dev->or = or;
919
920 if (per_dev != master_dev) {
921 bio = bio_kmalloc(GFP_NOFS,
922 master_dev->bio->bi_max_vecs);
923 if (unlikely(!bio)) {
924 dprintk("Faild to allocate BIO size=%u\n",
925 master_dev->bio->bi_max_vecs);
926 ret = -ENOMEM;
927 goto err;
928 }
929
930 __bio_clone(bio, master_dev->bio);
931 bio->bi_bdev = NULL;
932 bio->bi_next = NULL;
933 per_dev->bio = bio;
934 per_dev->dev = dev;
935 per_dev->length = master_dev->length;
936 per_dev->offset = master_dev->offset;
937 } else {
938 bio = master_dev->bio;
939 bio->bi_rw |= REQ_WRITE;
940 }
941
942 osd_req_write(or, &obj, per_dev->offset, bio, per_dev->length);
943
944 ret = osd_finalize_request(or, 0, cred->oc_cap.cred, NULL);
945 if (ret) {
946 dprintk("%s: Faild to osd_finalize_request() => %d\n",
947 __func__, ret);
948 goto err;
949 }
950
951 dprintk("%s:[%d] dev=%d obj=0x%llx start=0x%llx length=0x%lx\n",
952 __func__, cur_comp, dev, obj.id, _LLU(per_dev->offset),
953 per_dev->length);
954 }
955
956err:
957 return ret;
958}
959
960static ssize_t _write_exec(struct objio_state *ios)
961{
962 unsigned i;
963 int ret;
964
965 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
966 if (!ios->per_dev[i].length)
967 continue;
968 ret = _write_mirrors(ios, i);
969 if (unlikely(ret))
970 goto err;
971 }
972
973 ios->done = _write_done;
974 return _io_exec(ios); /* In sync mode exec returns the io->status */
975
976err:
977 _io_free(ios);
978 return ret;
979}
980
981ssize_t objio_write_pagelist(struct objlayout_io_state *ol_state, bool stable)
982{
983 struct objio_state *ios = container_of(ol_state, struct objio_state,
984 ol_state);
985 int ret;
986
987 /* TODO: ios->stable = stable; */
988 ret = _io_rw_pagelist(ios, GFP_NOFS);
989 if (unlikely(ret))
990 return ret;
991
992 return _write_exec(ios);
993}
994
Boaz Harrosh93420772011-05-25 21:25:29 +0300995static bool objio_pg_test(struct nfs_pageio_descriptor *pgio,
996 struct nfs_page *prev, struct nfs_page *req)
997{
998 if (!pnfs_generic_pg_test(pgio, prev, req))
999 return false;
1000
1001 return pgio->pg_count + req->wb_bytes <=
1002 OBJIO_LSEG(pgio->pg_lseg)->max_io_size;
1003}
1004
Trond Myklebust1751c362011-06-10 13:30:23 -04001005static const struct nfs_pageio_ops objio_pg_read_ops = {
Trond Myklebustd8007d42011-06-10 13:30:23 -04001006 .pg_init = pnfs_generic_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -04001007 .pg_test = objio_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -04001008 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -04001009};
1010
1011static const struct nfs_pageio_ops objio_pg_write_ops = {
Trond Myklebustd8007d42011-06-10 13:30:23 -04001012 .pg_init = pnfs_generic_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -04001013 .pg_test = objio_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -04001014 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -04001015};
1016
Benny Halevyc93407d2011-05-22 19:49:06 +03001017static struct pnfs_layoutdriver_type objlayout_type = {
1018 .id = LAYOUT_OSD2_OBJECTS,
1019 .name = "LAYOUT_OSD2_OBJECTS",
Benny Halevy8a1636c2010-07-14 15:43:57 -04001020 .flags = PNFS_LAYOUTRET_ON_SETATTR,
Boaz Harrosh09f5bf42011-05-22 19:50:20 +03001021
Benny Halevye51b8412011-05-22 19:51:48 +03001022 .alloc_layout_hdr = objlayout_alloc_layout_hdr,
1023 .free_layout_hdr = objlayout_free_layout_hdr,
1024
Boaz Harrosh09f5bf42011-05-22 19:50:20 +03001025 .alloc_lseg = objlayout_alloc_lseg,
1026 .free_lseg = objlayout_free_lseg,
Boaz Harroshb6c05f12011-05-26 21:45:34 +03001027
Boaz Harrosh04f83452011-05-22 19:52:19 +03001028 .read_pagelist = objlayout_read_pagelist,
1029 .write_pagelist = objlayout_write_pagelist,
Trond Myklebust1751c362011-06-10 13:30:23 -04001030 .pg_read_ops = &objio_pg_read_ops,
1031 .pg_write_ops = &objio_pg_write_ops,
Boaz Harrosh04f83452011-05-22 19:52:19 +03001032
Boaz Harroshb6c05f12011-05-26 21:45:34 +03001033 .free_deviceid_node = objio_free_deviceid_node,
Boaz Harroshadb58532011-05-26 21:49:46 +03001034
Boaz Harrosha0fe8bf2011-05-22 19:54:13 +03001035 .encode_layoutcommit = objlayout_encode_layoutcommit,
Boaz Harroshadb58532011-05-26 21:49:46 +03001036 .encode_layoutreturn = objlayout_encode_layoutreturn,
Benny Halevyc93407d2011-05-22 19:49:06 +03001037};
1038
1039MODULE_DESCRIPTION("pNFS Layout Driver for OSD2 objects");
1040MODULE_AUTHOR("Benny Halevy <bhalevy@panasas.com>");
1041MODULE_LICENSE("GPL");
1042
1043static int __init
1044objlayout_init(void)
1045{
1046 int ret = pnfs_register_layoutdriver(&objlayout_type);
1047
1048 if (ret)
1049 printk(KERN_INFO
1050 "%s: Registering OSD pNFS Layout Driver failed: error=%d\n",
1051 __func__, ret);
1052 else
1053 printk(KERN_INFO "%s: Registered OSD pNFS Layout Driver\n",
1054 __func__);
1055 return ret;
1056}
1057
1058static void __exit
1059objlayout_exit(void)
1060{
1061 pnfs_unregister_layoutdriver(&objlayout_type);
1062 printk(KERN_INFO "%s: Unregistered OSD pNFS Layout Driver\n",
1063 __func__);
1064}
1065
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001066MODULE_ALIAS("nfs-layouttype4-2");
1067
Benny Halevyc93407d2011-05-22 19:49:06 +03001068module_init(objlayout_init);
1069module_exit(objlayout_exit);