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