blob: 48eb91aad554020ed2a7b86e828027587dea6729 [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;
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700145typedef int (*objio_done_fn)(struct objio_state *ios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300146
147struct objio_state {
148 /* Generic layer */
149 struct objlayout_io_state ol_state;
150
Boaz Harrosh96218552011-10-31 14:47:32 -0700151 struct page **pages;
152 unsigned pgbase;
153 unsigned nr_pages;
154 unsigned long count;
155 loff_t offset;
156 bool sync;
157
Boaz Harrosh04f83452011-05-22 19:52:19 +0300158 struct objio_segment *layout;
159
160 struct kref kref;
161 objio_done_fn done;
162 void *private;
163
164 unsigned long length;
165 unsigned numdevs; /* Actually used devs in this IO */
166 /* A per-device variable array of size numdevs */
167 struct _objio_per_comp {
168 struct bio *bio;
169 struct osd_request *or;
170 unsigned long length;
171 u64 offset;
172 unsigned dev;
173 } per_dev[];
174};
175
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300176/* Send and wait for a get_device_info of devices in the layout,
177 then look them up with the osd_initiator library */
178static struct objio_dev_ent *_device_lookup(struct pnfs_layout_hdr *pnfslay,
179 struct objio_segment *objio_seg, unsigned comp,
180 gfp_t gfp_flags)
181{
182 struct pnfs_osd_deviceaddr *deviceaddr;
183 struct nfs4_deviceid *d_id;
184 struct objio_dev_ent *ode;
185 struct osd_dev *od;
186 struct osd_dev_info odi;
187 int err;
188
189 d_id = &objio_seg->comps[comp].oc_object_id.oid_device_id;
190
191 ode = _dev_list_find(NFS_SERVER(pnfslay->plh_inode), d_id);
192 if (ode)
193 return ode;
194
195 err = objlayout_get_deviceinfo(pnfslay, d_id, &deviceaddr, gfp_flags);
196 if (unlikely(err)) {
197 dprintk("%s: objlayout_get_deviceinfo dev(%llx:%llx) =>%d\n",
198 __func__, _DEVID_LO(d_id), _DEVID_HI(d_id), err);
199 return ERR_PTR(err);
200 }
201
202 odi.systemid_len = deviceaddr->oda_systemid.len;
203 if (odi.systemid_len > sizeof(odi.systemid)) {
204 err = -EINVAL;
205 goto out;
206 } else if (odi.systemid_len)
207 memcpy(odi.systemid, deviceaddr->oda_systemid.data,
208 odi.systemid_len);
209 odi.osdname_len = deviceaddr->oda_osdname.len;
210 odi.osdname = (u8 *)deviceaddr->oda_osdname.data;
211
212 if (!odi.osdname_len && !odi.systemid_len) {
213 dprintk("%s: !odi.osdname_len && !odi.systemid_len\n",
214 __func__);
215 err = -ENODEV;
216 goto out;
217 }
218
219 od = osduld_info_lookup(&odi);
220 if (unlikely(IS_ERR(od))) {
221 err = PTR_ERR(od);
222 dprintk("%s: osduld_info_lookup => %d\n", __func__, err);
223 goto out;
224 }
225
226 ode = _dev_list_add(NFS_SERVER(pnfslay->plh_inode), d_id, od,
227 gfp_flags);
228
229out:
230 dprintk("%s: return=%d\n", __func__, err);
231 objlayout_put_deviceinfo(deviceaddr);
232 return err ? ERR_PTR(err) : ode;
233}
234
235static int objio_devices_lookup(struct pnfs_layout_hdr *pnfslay,
236 struct objio_segment *objio_seg,
237 gfp_t gfp_flags)
238{
239 unsigned i;
240 int err;
241
242 /* lookup all devices */
243 for (i = 0; i < objio_seg->num_comps; i++) {
244 struct objio_dev_ent *ode;
245
246 ode = _device_lookup(pnfslay, objio_seg, i, gfp_flags);
247 if (unlikely(IS_ERR(ode))) {
248 err = PTR_ERR(ode);
249 goto out;
250 }
251 objio_seg->ods[i] = ode;
252 }
253 err = 0;
254
255out:
256 dprintk("%s: return=%d\n", __func__, err);
257 return err;
258}
259
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300260static int _verify_data_map(struct pnfs_osd_layout *layout)
261{
262 struct pnfs_osd_data_map *data_map = &layout->olo_map;
263 u64 stripe_length;
264 u32 group_width;
265
266/* FIXME: Only raid0 for now. if not go through MDS */
267 if (data_map->odm_raid_algorithm != PNFS_OSD_RAID_0) {
268 printk(KERN_ERR "Only RAID_0 for now\n");
269 return -ENOTSUPP;
270 }
271 if (0 != (data_map->odm_num_comps % (data_map->odm_mirror_cnt + 1))) {
272 printk(KERN_ERR "Data Map wrong, num_comps=%u mirrors=%u\n",
273 data_map->odm_num_comps, data_map->odm_mirror_cnt);
274 return -EINVAL;
275 }
276
277 if (data_map->odm_group_width)
278 group_width = data_map->odm_group_width;
279 else
280 group_width = data_map->odm_num_comps /
281 (data_map->odm_mirror_cnt + 1);
282
283 stripe_length = (u64)data_map->odm_stripe_unit * group_width;
284 if (stripe_length >= (1ULL << 32)) {
285 printk(KERN_ERR "Total Stripe length(0x%llx)"
286 " >= 32bit is not supported\n", _LLU(stripe_length));
287 return -ENOTSUPP;
288 }
289
290 if (0 != (data_map->odm_stripe_unit & ~PAGE_MASK)) {
291 printk(KERN_ERR "Stripe Unit(0x%llx)"
292 " must be Multples of PAGE_SIZE(0x%lx)\n",
293 _LLU(data_map->odm_stripe_unit), PAGE_SIZE);
294 return -ENOTSUPP;
295 }
296
297 return 0;
298}
299
300static void copy_single_comp(struct pnfs_osd_object_cred *cur_comp,
301 struct pnfs_osd_object_cred *src_comp,
302 struct caps_buffers *caps_p)
303{
304 WARN_ON(src_comp->oc_cap_key.cred_len > sizeof(caps_p->caps_key));
305 WARN_ON(src_comp->oc_cap.cred_len > sizeof(caps_p->creds));
306
307 *cur_comp = *src_comp;
308
309 memcpy(caps_p->caps_key, src_comp->oc_cap_key.cred,
310 sizeof(caps_p->caps_key));
311 cur_comp->oc_cap_key.cred = caps_p->caps_key;
312
313 memcpy(caps_p->creds, src_comp->oc_cap.cred,
314 sizeof(caps_p->creds));
315 cur_comp->oc_cap.cred = caps_p->creds;
316}
317
318int objio_alloc_lseg(struct pnfs_layout_segment **outp,
319 struct pnfs_layout_hdr *pnfslay,
320 struct pnfs_layout_range *range,
321 struct xdr_stream *xdr,
322 gfp_t gfp_flags)
323{
324 struct objio_segment *objio_seg;
325 struct pnfs_osd_xdr_decode_layout_iter iter;
326 struct pnfs_osd_layout layout;
327 struct pnfs_osd_object_cred *cur_comp, src_comp;
328 struct caps_buffers *caps_p;
329 int err;
330
331 err = pnfs_osd_xdr_decode_layout_map(&layout, &iter, xdr);
332 if (unlikely(err))
333 return err;
334
335 err = _verify_data_map(&layout);
336 if (unlikely(err))
337 return err;
338
339 objio_seg = kzalloc(sizeof(*objio_seg) +
340 sizeof(objio_seg->ods[0]) * layout.olo_num_comps +
341 sizeof(*objio_seg->comps) * layout.olo_num_comps +
342 sizeof(struct caps_buffers) * layout.olo_num_comps,
343 gfp_flags);
344 if (!objio_seg)
345 return -ENOMEM;
346
347 objio_seg->comps = (void *)(objio_seg->ods + layout.olo_num_comps);
348 cur_comp = objio_seg->comps;
349 caps_p = (void *)(cur_comp + layout.olo_num_comps);
350 while (pnfs_osd_xdr_decode_layout_comp(&src_comp, &iter, xdr, &err))
351 copy_single_comp(cur_comp++, &src_comp, caps_p++);
352 if (unlikely(err))
353 goto err;
354
355 objio_seg->num_comps = layout.olo_num_comps;
356 objio_seg->comps_index = layout.olo_comps_index;
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300357 err = objio_devices_lookup(pnfslay, objio_seg, gfp_flags);
358 if (err)
359 goto err;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300360
361 objio_seg->mirrors_p1 = layout.olo_map.odm_mirror_cnt + 1;
362 objio_seg->stripe_unit = layout.olo_map.odm_stripe_unit;
363 if (layout.olo_map.odm_group_width) {
364 objio_seg->group_width = layout.olo_map.odm_group_width;
365 objio_seg->group_depth = layout.olo_map.odm_group_depth;
366 objio_seg->group_count = layout.olo_map.odm_num_comps /
367 objio_seg->mirrors_p1 /
368 objio_seg->group_width;
369 } else {
370 objio_seg->group_width = layout.olo_map.odm_num_comps /
371 objio_seg->mirrors_p1;
372 objio_seg->group_depth = -1;
373 objio_seg->group_count = 1;
374 }
375
Boaz Harrosh93420772011-05-25 21:25:29 +0300376 /* Cache this calculation it will hit for every page */
377 objio_seg->max_io_size = (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE -
378 objio_seg->stripe_unit) *
379 objio_seg->group_width;
380
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300381 *outp = &objio_seg->lseg;
382 return 0;
383
384err:
385 kfree(objio_seg);
386 dprintk("%s: Error: return %d\n", __func__, err);
387 *outp = NULL;
388 return err;
389}
390
391void objio_free_lseg(struct pnfs_layout_segment *lseg)
392{
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300393 int i;
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300394 struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
395
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300396 for (i = 0; i < objio_seg->num_comps; i++) {
397 if (!objio_seg->ods[i])
398 break;
399 nfs4_put_deviceid_node(&objio_seg->ods[i]->id_node);
400 }
Boaz Harrosh09f5bf42011-05-22 19:50:20 +0300401 kfree(objio_seg);
402}
403
Boaz Harrosh96218552011-10-31 14:47:32 -0700404static int
405objio_alloc_io_state(struct pnfs_layout_hdr *pnfs_layout_type,
406 struct pnfs_layout_segment *lseg, struct page **pages, unsigned pgbase,
407 loff_t offset, size_t count, void *rpcdata, gfp_t gfp_flags,
408 struct objio_state **outp)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300409{
410 struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
411 struct objio_state *ios;
Boaz Harrosh96218552011-10-31 14:47:32 -0700412 struct __alloc_objio_state {
413 struct objio_state objios;
414 struct _objio_per_comp per_dev[objio_seg->num_comps];
415 struct pnfs_osd_ioerr ioerrs[objio_seg->num_comps];
416 } *aos;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300417
Boaz Harrosh96218552011-10-31 14:47:32 -0700418 aos = kzalloc(sizeof(*aos), gfp_flags);
419 if (unlikely(!aos))
Boaz Harrosh04f83452011-05-22 19:52:19 +0300420 return -ENOMEM;
421
Boaz Harrosh96218552011-10-31 14:47:32 -0700422 ios = &aos->objios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300423
Boaz Harrosh96218552011-10-31 14:47:32 -0700424 ios->layout = objio_seg;
425 objlayout_init_ioerrs(&aos->objios.ol_state, objio_seg->num_comps,
426 aos->ioerrs, rpcdata, pnfs_layout_type);
427
428 ios->pages = pages;
429 ios->pgbase = pgbase;
430 ios->nr_pages = (pgbase + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
431 ios->offset = offset;
432 ios->count = count;
433 ios->sync = 0;
434 BUG_ON(ios->nr_pages > (pgbase + count + PAGE_SIZE - 1) >> PAGE_SHIFT);
435
436 *outp = ios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300437 return 0;
438}
439
Boaz Harrosh96218552011-10-31 14:47:32 -0700440void objio_free_result(struct objlayout_io_state *ol_state)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300441{
442 struct objio_state *ios = container_of(ol_state, struct objio_state,
443 ol_state);
444
445 kfree(ios);
446}
447
Boaz Harroshadb58532011-05-26 21:49:46 +0300448enum pnfs_osd_errno osd_pri_2_pnfs_err(enum osd_err_priority oep)
449{
450 switch (oep) {
451 case OSD_ERR_PRI_NO_ERROR:
452 return (enum pnfs_osd_errno)0;
453
454 case OSD_ERR_PRI_CLEAR_PAGES:
455 BUG_ON(1);
456 return 0;
457
458 case OSD_ERR_PRI_RESOURCE:
459 return PNFS_OSD_ERR_RESOURCE;
460 case OSD_ERR_PRI_BAD_CRED:
461 return PNFS_OSD_ERR_BAD_CRED;
462 case OSD_ERR_PRI_NO_ACCESS:
463 return PNFS_OSD_ERR_NO_ACCESS;
464 case OSD_ERR_PRI_UNREACHABLE:
465 return PNFS_OSD_ERR_UNREACHABLE;
466 case OSD_ERR_PRI_NOT_FOUND:
467 return PNFS_OSD_ERR_NOT_FOUND;
468 case OSD_ERR_PRI_NO_SPACE:
469 return PNFS_OSD_ERR_NO_SPACE;
470 default:
471 WARN_ON(1);
472 /* fallthrough */
473 case OSD_ERR_PRI_EIO:
474 return PNFS_OSD_ERR_EIO;
475 }
476}
477
Boaz Harrosh04f83452011-05-22 19:52:19 +0300478static void _clear_bio(struct bio *bio)
479{
480 struct bio_vec *bv;
481 unsigned i;
482
483 __bio_for_each_segment(bv, bio, i, 0) {
484 unsigned this_count = bv->bv_len;
485
486 if (likely(PAGE_SIZE == this_count))
487 clear_highpage(bv->bv_page);
488 else
489 zero_user(bv->bv_page, bv->bv_offset, this_count);
490 }
491}
492
493static int _io_check(struct objio_state *ios, bool is_write)
494{
495 enum osd_err_priority oep = OSD_ERR_PRI_NO_ERROR;
496 int lin_ret = 0;
497 int i;
498
499 for (i = 0; i < ios->numdevs; i++) {
500 struct osd_sense_info osi;
501 struct osd_request *or = ios->per_dev[i].or;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300502 int ret;
503
504 if (!or)
505 continue;
506
507 ret = osd_req_decode_sense(or, &osi);
508 if (likely(!ret))
509 continue;
510
511 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
512 /* start read offset passed endof file */
513 BUG_ON(is_write);
514 _clear_bio(ios->per_dev[i].bio);
515 dprintk("%s: start read offset passed end of file "
516 "offset=0x%llx, length=0x%lx\n", __func__,
517 _LLU(ios->per_dev[i].offset),
518 ios->per_dev[i].length);
519
520 continue; /* we recovered */
521 }
Boaz Harrosh9af7db32011-08-03 21:52:51 -0700522 objlayout_io_set_result(&ios->ol_state, i,
523 &ios->layout->comps[i].oc_object_id,
Boaz Harroshadb58532011-05-26 21:49:46 +0300524 osd_pri_2_pnfs_err(osi.osd_err_pri),
525 ios->per_dev[i].offset,
526 ios->per_dev[i].length,
527 is_write);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300528
529 if (osi.osd_err_pri >= oep) {
530 oep = osi.osd_err_pri;
531 lin_ret = ret;
532 }
533 }
534
535 return lin_ret;
536}
537
538/*
539 * Common IO state helpers.
540 */
541static void _io_free(struct objio_state *ios)
542{
543 unsigned i;
544
545 for (i = 0; i < ios->numdevs; i++) {
546 struct _objio_per_comp *per_dev = &ios->per_dev[i];
547
548 if (per_dev->or) {
549 osd_end_request(per_dev->or);
550 per_dev->or = NULL;
551 }
552
553 if (per_dev->bio) {
554 bio_put(per_dev->bio);
555 per_dev->bio = NULL;
556 }
557 }
558}
559
560struct osd_dev *_io_od(struct objio_state *ios, unsigned dev)
561{
562 unsigned min_dev = ios->layout->comps_index;
563 unsigned max_dev = min_dev + ios->layout->num_comps;
564
565 BUG_ON(dev < min_dev || max_dev <= dev);
566 return ios->layout->ods[dev - min_dev]->od;
567}
568
569struct _striping_info {
570 u64 obj_offset;
571 u64 group_length;
572 unsigned dev;
573 unsigned unit_off;
574};
575
576static void _calc_stripe_info(struct objio_state *ios, u64 file_offset,
577 struct _striping_info *si)
578{
579 u32 stripe_unit = ios->layout->stripe_unit;
580 u32 group_width = ios->layout->group_width;
581 u64 group_depth = ios->layout->group_depth;
582 u32 U = stripe_unit * group_width;
583
584 u64 T = U * group_depth;
585 u64 S = T * ios->layout->group_count;
586 u64 M = div64_u64(file_offset, S);
587
588 /*
589 G = (L - (M * S)) / T
590 H = (L - (M * S)) % T
591 */
592 u64 LmodU = file_offset - M * S;
593 u32 G = div64_u64(LmodU, T);
594 u64 H = LmodU - G * T;
595
596 u32 N = div_u64(H, U);
597
598 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
599 si->obj_offset = si->unit_off + (N * stripe_unit) +
600 (M * group_depth * stripe_unit);
601
602 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
603 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
604 si->dev *= ios->layout->mirrors_p1;
605
606 si->group_length = T - H;
607}
608
609static int _add_stripe_unit(struct objio_state *ios, unsigned *cur_pg,
Boaz Harrosh20618b22011-08-03 21:54:33 -0700610 unsigned pgbase, struct _objio_per_comp *per_dev, int len,
Boaz Harrosh04f83452011-05-22 19:52:19 +0300611 gfp_t gfp_flags)
612{
613 unsigned pg = *cur_pg;
Boaz Harrosh20618b22011-08-03 21:54:33 -0700614 int cur_len = len;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300615 struct request_queue *q =
616 osd_request_queue(_io_od(ios, per_dev->dev));
617
Boaz Harrosh04f83452011-05-22 19:52:19 +0300618 if (per_dev->bio == NULL) {
Boaz Harrosh20618b22011-08-03 21:54:33 -0700619 unsigned pages_in_stripe = ios->layout->group_width *
Boaz Harrosh04f83452011-05-22 19:52:19 +0300620 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh96218552011-10-31 14:47:32 -0700621 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh20618b22011-08-03 21:54:33 -0700622 ios->layout->group_width;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300623
624 if (BIO_MAX_PAGES_KMALLOC < bio_size)
625 bio_size = BIO_MAX_PAGES_KMALLOC;
626
627 per_dev->bio = bio_kmalloc(gfp_flags, bio_size);
628 if (unlikely(!per_dev->bio)) {
629 dprintk("Faild to allocate BIO size=%u\n", bio_size);
630 return -ENOMEM;
631 }
632 }
633
634 while (cur_len > 0) {
635 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
636 unsigned added_len;
637
Boaz Harrosh96218552011-10-31 14:47:32 -0700638 BUG_ON(ios->nr_pages <= pg);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300639 cur_len -= pglen;
640
641 added_len = bio_add_pc_page(q, per_dev->bio,
Boaz Harrosh96218552011-10-31 14:47:32 -0700642 ios->pages[pg], pglen, pgbase);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300643 if (unlikely(pglen != added_len))
644 return -ENOMEM;
645 pgbase = 0;
646 ++pg;
647 }
648 BUG_ON(cur_len);
649
Boaz Harrosh20618b22011-08-03 21:54:33 -0700650 per_dev->length += len;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300651 *cur_pg = pg;
652 return 0;
653}
654
655static int _prepare_one_group(struct objio_state *ios, u64 length,
656 struct _striping_info *si, unsigned *last_pg,
657 gfp_t gfp_flags)
658{
659 unsigned stripe_unit = ios->layout->stripe_unit;
660 unsigned mirrors_p1 = ios->layout->mirrors_p1;
661 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
662 unsigned dev = si->dev;
663 unsigned first_dev = dev - (dev % devs_in_group);
664 unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
665 unsigned cur_pg = *last_pg;
666 int ret = 0;
667
668 while (length) {
Boaz Harrosh9af7db32011-08-03 21:52:51 -0700669 struct _objio_per_comp *per_dev = &ios->per_dev[dev - first_dev];
Boaz Harrosh04f83452011-05-22 19:52:19 +0300670 unsigned cur_len, page_off = 0;
671
672 if (!per_dev->length) {
673 per_dev->dev = dev;
674 if (dev < si->dev) {
675 per_dev->offset = si->obj_offset + stripe_unit -
676 si->unit_off;
677 cur_len = stripe_unit;
678 } else if (dev == si->dev) {
679 per_dev->offset = si->obj_offset;
680 cur_len = stripe_unit - si->unit_off;
681 page_off = si->unit_off & ~PAGE_MASK;
682 BUG_ON(page_off &&
Boaz Harrosh96218552011-10-31 14:47:32 -0700683 (page_off != ios->pgbase));
Boaz Harrosh04f83452011-05-22 19:52:19 +0300684 } else { /* dev > si->dev */
685 per_dev->offset = si->obj_offset - si->unit_off;
686 cur_len = stripe_unit;
687 }
688
Boaz Harrosh9af7db32011-08-03 21:52:51 -0700689 if (max_comp < dev - first_dev)
690 max_comp = dev - first_dev;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300691 } else {
692 cur_len = stripe_unit;
693 }
694 if (cur_len >= length)
695 cur_len = length;
696
697 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
698 cur_len, gfp_flags);
699 if (unlikely(ret))
700 goto out;
701
702 dev += mirrors_p1;
703 dev = (dev % devs_in_group) + first_dev;
704
705 length -= cur_len;
706 ios->length += cur_len;
707 }
708out:
709 ios->numdevs = max_comp + mirrors_p1;
710 *last_pg = cur_pg;
711 return ret;
712}
713
714static int _io_rw_pagelist(struct objio_state *ios, gfp_t gfp_flags)
715{
Boaz Harrosh96218552011-10-31 14:47:32 -0700716 u64 length = ios->count;
717 u64 offset = ios->offset;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300718 struct _striping_info si;
719 unsigned last_pg = 0;
720 int ret = 0;
721
722 while (length) {
723 _calc_stripe_info(ios, offset, &si);
724
725 if (length < si.group_length)
726 si.group_length = length;
727
728 ret = _prepare_one_group(ios, si.group_length, &si, &last_pg, gfp_flags);
729 if (unlikely(ret))
730 goto out;
731
732 offset += si.group_length;
733 length -= si.group_length;
734 }
735
736out:
737 if (!ios->length)
738 return ret;
739
740 return 0;
741}
742
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700743static int _sync_done(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300744{
745 struct completion *waiting = ios->private;
746
747 complete(waiting);
748 return 0;
749}
750
751static void _last_io(struct kref *kref)
752{
753 struct objio_state *ios = container_of(kref, struct objio_state, kref);
754
755 ios->done(ios);
756}
757
758static void _done_io(struct osd_request *or, void *p)
759{
760 struct objio_state *ios = p;
761
762 kref_put(&ios->kref, _last_io);
763}
764
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700765static int _io_exec(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300766{
767 DECLARE_COMPLETION_ONSTACK(wait);
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700768 int ret = 0;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300769 unsigned i;
770 objio_done_fn saved_done_fn = ios->done;
Boaz Harrosh96218552011-10-31 14:47:32 -0700771 bool sync = ios->sync;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300772
773 if (sync) {
774 ios->done = _sync_done;
775 ios->private = &wait;
776 }
777
778 kref_init(&ios->kref);
779
780 for (i = 0; i < ios->numdevs; i++) {
781 struct osd_request *or = ios->per_dev[i].or;
782
783 if (!or)
784 continue;
785
786 kref_get(&ios->kref);
787 osd_execute_request_async(or, _done_io, ios);
788 }
789
790 kref_put(&ios->kref, _last_io);
791
792 if (sync) {
793 wait_for_completion(&wait);
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700794 ret = saved_done_fn(ios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300795 }
796
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700797 return ret;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300798}
799
800/*
801 * read
802 */
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700803static int _read_done(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300804{
805 ssize_t status;
806 int ret = _io_check(ios, false);
807
808 _io_free(ios);
809
810 if (likely(!ret))
811 status = ios->length;
812 else
813 status = ret;
814
Boaz Harrosh96218552011-10-31 14:47:32 -0700815 objlayout_read_done(&ios->ol_state, status, ios->sync);
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700816 return ret;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300817}
818
819static int _read_mirrors(struct objio_state *ios, unsigned cur_comp)
820{
821 struct osd_request *or = NULL;
822 struct _objio_per_comp *per_dev = &ios->per_dev[cur_comp];
823 unsigned dev = per_dev->dev;
824 struct pnfs_osd_object_cred *cred =
Boaz Harrosh9af7db32011-08-03 21:52:51 -0700825 &ios->layout->comps[cur_comp];
Boaz Harrosh04f83452011-05-22 19:52:19 +0300826 struct osd_obj_id obj = {
827 .partition = cred->oc_object_id.oid_partition_id,
828 .id = cred->oc_object_id.oid_object_id,
829 };
830 int ret;
831
832 or = osd_start_request(_io_od(ios, dev), GFP_KERNEL);
833 if (unlikely(!or)) {
834 ret = -ENOMEM;
835 goto err;
836 }
837 per_dev->or = or;
838
839 osd_req_read(or, &obj, per_dev->offset, per_dev->bio, per_dev->length);
840
841 ret = osd_finalize_request(or, 0, cred->oc_cap.cred, NULL);
842 if (ret) {
843 dprintk("%s: Faild to osd_finalize_request() => %d\n",
844 __func__, ret);
845 goto err;
846 }
847
848 dprintk("%s:[%d] dev=%d obj=0x%llx start=0x%llx length=0x%lx\n",
849 __func__, cur_comp, dev, obj.id, _LLU(per_dev->offset),
850 per_dev->length);
851
852err:
853 return ret;
854}
855
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700856static int _read_exec(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300857{
858 unsigned i;
859 int ret;
860
861 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
862 if (!ios->per_dev[i].length)
863 continue;
864 ret = _read_mirrors(ios, i);
865 if (unlikely(ret))
866 goto err;
867 }
868
869 ios->done = _read_done;
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700870 return _io_exec(ios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300871
872err:
873 _io_free(ios);
874 return ret;
875}
876
Boaz Harrosh96218552011-10-31 14:47:32 -0700877int objio_read_pagelist(struct nfs_read_data *rdata)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300878{
Boaz Harrosh96218552011-10-31 14:47:32 -0700879 struct objio_state *ios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300880 int ret;
881
Boaz Harrosh96218552011-10-31 14:47:32 -0700882 ret = objio_alloc_io_state(NFS_I(rdata->inode)->layout,
883 rdata->lseg, rdata->args.pages, rdata->args.pgbase,
884 rdata->args.offset, rdata->args.count, rdata,
885 GFP_KERNEL, &ios);
886 if (unlikely(ret))
887 return ret;
888
Boaz Harrosh04f83452011-05-22 19:52:19 +0300889 ret = _io_rw_pagelist(ios, GFP_KERNEL);
890 if (unlikely(ret))
891 return ret;
892
893 return _read_exec(ios);
894}
895
896/*
897 * write
898 */
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700899static int _write_done(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300900{
901 ssize_t status;
902 int ret = _io_check(ios, true);
903
904 _io_free(ios);
905
906 if (likely(!ret)) {
907 /* FIXME: should be based on the OSD's persistence model
908 * See OSD2r05 Section 4.13 Data persistence model */
909 ios->ol_state.committed = NFS_FILE_SYNC;
910 status = ios->length;
911 } else {
912 status = ret;
913 }
914
Boaz Harrosh96218552011-10-31 14:47:32 -0700915 objlayout_write_done(&ios->ol_state, status, ios->sync);
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700916 return ret;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300917}
918
919static int _write_mirrors(struct objio_state *ios, unsigned cur_comp)
920{
921 struct _objio_per_comp *master_dev = &ios->per_dev[cur_comp];
922 unsigned dev = ios->per_dev[cur_comp].dev;
923 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
924 int ret;
925
926 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
927 struct osd_request *or = NULL;
928 struct pnfs_osd_object_cred *cred =
Boaz Harrosh9af7db32011-08-03 21:52:51 -0700929 &ios->layout->comps[cur_comp];
Boaz Harrosh04f83452011-05-22 19:52:19 +0300930 struct osd_obj_id obj = {
931 .partition = cred->oc_object_id.oid_partition_id,
932 .id = cred->oc_object_id.oid_object_id,
933 };
934 struct _objio_per_comp *per_dev = &ios->per_dev[cur_comp];
935 struct bio *bio;
936
937 or = osd_start_request(_io_od(ios, dev), GFP_NOFS);
938 if (unlikely(!or)) {
939 ret = -ENOMEM;
940 goto err;
941 }
942 per_dev->or = or;
943
944 if (per_dev != master_dev) {
945 bio = bio_kmalloc(GFP_NOFS,
946 master_dev->bio->bi_max_vecs);
947 if (unlikely(!bio)) {
948 dprintk("Faild to allocate BIO size=%u\n",
949 master_dev->bio->bi_max_vecs);
950 ret = -ENOMEM;
951 goto err;
952 }
953
954 __bio_clone(bio, master_dev->bio);
955 bio->bi_bdev = NULL;
956 bio->bi_next = NULL;
957 per_dev->bio = bio;
958 per_dev->dev = dev;
959 per_dev->length = master_dev->length;
960 per_dev->offset = master_dev->offset;
961 } else {
962 bio = master_dev->bio;
963 bio->bi_rw |= REQ_WRITE;
964 }
965
966 osd_req_write(or, &obj, per_dev->offset, bio, per_dev->length);
967
968 ret = osd_finalize_request(or, 0, cred->oc_cap.cred, NULL);
969 if (ret) {
970 dprintk("%s: Faild to osd_finalize_request() => %d\n",
971 __func__, ret);
972 goto err;
973 }
974
975 dprintk("%s:[%d] dev=%d obj=0x%llx start=0x%llx length=0x%lx\n",
976 __func__, cur_comp, dev, obj.id, _LLU(per_dev->offset),
977 per_dev->length);
978 }
979
980err:
981 return ret;
982}
983
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700984static int _write_exec(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300985{
986 unsigned i;
987 int ret;
988
989 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
990 if (!ios->per_dev[i].length)
991 continue;
992 ret = _write_mirrors(ios, i);
993 if (unlikely(ret))
994 goto err;
995 }
996
997 ios->done = _write_done;
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700998 return _io_exec(ios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300999
1000err:
1001 _io_free(ios);
1002 return ret;
1003}
1004
Boaz Harrosh96218552011-10-31 14:47:32 -07001005int objio_write_pagelist(struct nfs_write_data *wdata, int how)
Boaz Harrosh04f83452011-05-22 19:52:19 +03001006{
Boaz Harrosh96218552011-10-31 14:47:32 -07001007 struct objio_state *ios;
Boaz Harrosh04f83452011-05-22 19:52:19 +03001008 int ret;
1009
Boaz Harrosh96218552011-10-31 14:47:32 -07001010 ret = objio_alloc_io_state(NFS_I(wdata->inode)->layout,
1011 wdata->lseg, wdata->args.pages, wdata->args.pgbase,
1012 wdata->args.offset, wdata->args.count, wdata, GFP_NOFS,
1013 &ios);
1014 if (unlikely(ret))
1015 return ret;
1016
1017 ios->sync = 0 != (how & FLUSH_SYNC);
1018
Boaz Harrosh04f83452011-05-22 19:52:19 +03001019 /* TODO: ios->stable = stable; */
1020 ret = _io_rw_pagelist(ios, GFP_NOFS);
1021 if (unlikely(ret))
1022 return ret;
1023
1024 return _write_exec(ios);
1025}
1026
Boaz Harrosh93420772011-05-25 21:25:29 +03001027static bool objio_pg_test(struct nfs_pageio_descriptor *pgio,
1028 struct nfs_page *prev, struct nfs_page *req)
1029{
1030 if (!pnfs_generic_pg_test(pgio, prev, req))
1031 return false;
1032
1033 return pgio->pg_count + req->wb_bytes <=
1034 OBJIO_LSEG(pgio->pg_lseg)->max_io_size;
1035}
1036
Trond Myklebust1751c362011-06-10 13:30:23 -04001037static const struct nfs_pageio_ops objio_pg_read_ops = {
Trond Myklebustd8007d42011-06-10 13:30:23 -04001038 .pg_init = pnfs_generic_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -04001039 .pg_test = objio_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -04001040 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -04001041};
1042
1043static const struct nfs_pageio_ops objio_pg_write_ops = {
Trond Myklebustd8007d42011-06-10 13:30:23 -04001044 .pg_init = pnfs_generic_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -04001045 .pg_test = objio_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -04001046 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -04001047};
1048
Benny Halevyc93407d2011-05-22 19:49:06 +03001049static struct pnfs_layoutdriver_type objlayout_type = {
1050 .id = LAYOUT_OSD2_OBJECTS,
1051 .name = "LAYOUT_OSD2_OBJECTS",
Benny Halevy8a1636c2010-07-14 15:43:57 -04001052 .flags = PNFS_LAYOUTRET_ON_SETATTR,
Boaz Harrosh09f5bf42011-05-22 19:50:20 +03001053
Benny Halevye51b8412011-05-22 19:51:48 +03001054 .alloc_layout_hdr = objlayout_alloc_layout_hdr,
1055 .free_layout_hdr = objlayout_free_layout_hdr,
1056
Boaz Harrosh09f5bf42011-05-22 19:50:20 +03001057 .alloc_lseg = objlayout_alloc_lseg,
1058 .free_lseg = objlayout_free_lseg,
Boaz Harroshb6c05f12011-05-26 21:45:34 +03001059
Boaz Harrosh04f83452011-05-22 19:52:19 +03001060 .read_pagelist = objlayout_read_pagelist,
1061 .write_pagelist = objlayout_write_pagelist,
Trond Myklebust1751c362011-06-10 13:30:23 -04001062 .pg_read_ops = &objio_pg_read_ops,
1063 .pg_write_ops = &objio_pg_write_ops,
Boaz Harrosh04f83452011-05-22 19:52:19 +03001064
Boaz Harroshb6c05f12011-05-26 21:45:34 +03001065 .free_deviceid_node = objio_free_deviceid_node,
Boaz Harroshadb58532011-05-26 21:49:46 +03001066
Boaz Harrosha0fe8bf2011-05-22 19:54:13 +03001067 .encode_layoutcommit = objlayout_encode_layoutcommit,
Boaz Harroshadb58532011-05-26 21:49:46 +03001068 .encode_layoutreturn = objlayout_encode_layoutreturn,
Benny Halevyc93407d2011-05-22 19:49:06 +03001069};
1070
1071MODULE_DESCRIPTION("pNFS Layout Driver for OSD2 objects");
1072MODULE_AUTHOR("Benny Halevy <bhalevy@panasas.com>");
1073MODULE_LICENSE("GPL");
1074
1075static int __init
1076objlayout_init(void)
1077{
1078 int ret = pnfs_register_layoutdriver(&objlayout_type);
1079
1080 if (ret)
1081 printk(KERN_INFO
1082 "%s: Registering OSD pNFS Layout Driver failed: error=%d\n",
1083 __func__, ret);
1084 else
1085 printk(KERN_INFO "%s: Registered OSD pNFS Layout Driver\n",
1086 __func__);
1087 return ret;
1088}
1089
1090static void __exit
1091objlayout_exit(void)
1092{
1093 pnfs_unregister_layoutdriver(&objlayout_type);
1094 printk(KERN_INFO "%s: Unregistered OSD pNFS Layout Driver\n",
1095 __func__);
1096}
1097
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001098MODULE_ALIAS("nfs-layouttype4-2");
1099
Benny Halevyc93407d2011-05-22 19:49:06 +03001100module_init(objlayout_init);
1101module_exit(objlayout_exit);