blob: 2347e0ac63e65d7a86fce4e5df9abab06e63f7ae [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 */
Boaz Harroshe2e04352011-10-31 15:03:35 -0700149 struct objlayout_io_res oir;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300150
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;
Boaz Harroshe2e04352011-10-31 15:03:35 -0700425 objlayout_init_ioerrs(&aos->objios.oir, objio_seg->num_comps,
Boaz Harrosh96218552011-10-31 14:47:32 -0700426 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 Harroshe2e04352011-10-31 15:03:35 -0700440void objio_free_result(struct objlayout_io_res *oir)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300441{
Boaz Harroshe2e04352011-10-31 15:03:35 -0700442 struct objio_state *ios = container_of(oir, struct objio_state, oir);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300443
444 kfree(ios);
445}
446
Boaz Harroshadb58532011-05-26 21:49:46 +0300447enum pnfs_osd_errno osd_pri_2_pnfs_err(enum osd_err_priority oep)
448{
449 switch (oep) {
450 case OSD_ERR_PRI_NO_ERROR:
451 return (enum pnfs_osd_errno)0;
452
453 case OSD_ERR_PRI_CLEAR_PAGES:
454 BUG_ON(1);
455 return 0;
456
457 case OSD_ERR_PRI_RESOURCE:
458 return PNFS_OSD_ERR_RESOURCE;
459 case OSD_ERR_PRI_BAD_CRED:
460 return PNFS_OSD_ERR_BAD_CRED;
461 case OSD_ERR_PRI_NO_ACCESS:
462 return PNFS_OSD_ERR_NO_ACCESS;
463 case OSD_ERR_PRI_UNREACHABLE:
464 return PNFS_OSD_ERR_UNREACHABLE;
465 case OSD_ERR_PRI_NOT_FOUND:
466 return PNFS_OSD_ERR_NOT_FOUND;
467 case OSD_ERR_PRI_NO_SPACE:
468 return PNFS_OSD_ERR_NO_SPACE;
469 default:
470 WARN_ON(1);
471 /* fallthrough */
472 case OSD_ERR_PRI_EIO:
473 return PNFS_OSD_ERR_EIO;
474 }
475}
476
Boaz Harrosh04f83452011-05-22 19:52:19 +0300477static void _clear_bio(struct bio *bio)
478{
479 struct bio_vec *bv;
480 unsigned i;
481
482 __bio_for_each_segment(bv, bio, i, 0) {
483 unsigned this_count = bv->bv_len;
484
485 if (likely(PAGE_SIZE == this_count))
486 clear_highpage(bv->bv_page);
487 else
488 zero_user(bv->bv_page, bv->bv_offset, this_count);
489 }
490}
491
492static int _io_check(struct objio_state *ios, bool is_write)
493{
494 enum osd_err_priority oep = OSD_ERR_PRI_NO_ERROR;
495 int lin_ret = 0;
496 int i;
497
498 for (i = 0; i < ios->numdevs; i++) {
499 struct osd_sense_info osi;
500 struct osd_request *or = ios->per_dev[i].or;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300501 int ret;
502
503 if (!or)
504 continue;
505
506 ret = osd_req_decode_sense(or, &osi);
507 if (likely(!ret))
508 continue;
509
510 if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
511 /* start read offset passed endof file */
512 BUG_ON(is_write);
513 _clear_bio(ios->per_dev[i].bio);
514 dprintk("%s: start read offset passed end of file "
515 "offset=0x%llx, length=0x%lx\n", __func__,
516 _LLU(ios->per_dev[i].offset),
517 ios->per_dev[i].length);
518
519 continue; /* we recovered */
520 }
Boaz Harroshe2e04352011-10-31 15:03:35 -0700521 objlayout_io_set_result(&ios->oir, i,
Boaz Harrosh9af7db32011-08-03 21:52:51 -0700522 &ios->layout->comps[i].oc_object_id,
Boaz Harroshadb58532011-05-26 21:49:46 +0300523 osd_pri_2_pnfs_err(osi.osd_err_pri),
524 ios->per_dev[i].offset,
525 ios->per_dev[i].length,
526 is_write);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300527
528 if (osi.osd_err_pri >= oep) {
529 oep = osi.osd_err_pri;
530 lin_ret = ret;
531 }
532 }
533
534 return lin_ret;
535}
536
537/*
538 * Common IO state helpers.
539 */
540static void _io_free(struct objio_state *ios)
541{
542 unsigned i;
543
544 for (i = 0; i < ios->numdevs; i++) {
545 struct _objio_per_comp *per_dev = &ios->per_dev[i];
546
547 if (per_dev->or) {
548 osd_end_request(per_dev->or);
549 per_dev->or = NULL;
550 }
551
552 if (per_dev->bio) {
553 bio_put(per_dev->bio);
554 per_dev->bio = NULL;
555 }
556 }
557}
558
559struct osd_dev *_io_od(struct objio_state *ios, unsigned dev)
560{
561 unsigned min_dev = ios->layout->comps_index;
562 unsigned max_dev = min_dev + ios->layout->num_comps;
563
564 BUG_ON(dev < min_dev || max_dev <= dev);
565 return ios->layout->ods[dev - min_dev]->od;
566}
567
568struct _striping_info {
569 u64 obj_offset;
570 u64 group_length;
571 unsigned dev;
572 unsigned unit_off;
573};
574
575static void _calc_stripe_info(struct objio_state *ios, u64 file_offset,
576 struct _striping_info *si)
577{
578 u32 stripe_unit = ios->layout->stripe_unit;
579 u32 group_width = ios->layout->group_width;
580 u64 group_depth = ios->layout->group_depth;
581 u32 U = stripe_unit * group_width;
582
583 u64 T = U * group_depth;
584 u64 S = T * ios->layout->group_count;
585 u64 M = div64_u64(file_offset, S);
586
587 /*
588 G = (L - (M * S)) / T
589 H = (L - (M * S)) % T
590 */
591 u64 LmodU = file_offset - M * S;
592 u32 G = div64_u64(LmodU, T);
593 u64 H = LmodU - G * T;
594
595 u32 N = div_u64(H, U);
596
597 div_u64_rem(file_offset, stripe_unit, &si->unit_off);
598 si->obj_offset = si->unit_off + (N * stripe_unit) +
599 (M * group_depth * stripe_unit);
600
601 /* "H - (N * U)" is just "H % U" so it's bound to u32 */
602 si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
603 si->dev *= ios->layout->mirrors_p1;
604
605 si->group_length = T - H;
606}
607
608static int _add_stripe_unit(struct objio_state *ios, unsigned *cur_pg,
Boaz Harrosh20618b22011-08-03 21:54:33 -0700609 unsigned pgbase, struct _objio_per_comp *per_dev, int len,
Boaz Harrosh04f83452011-05-22 19:52:19 +0300610 gfp_t gfp_flags)
611{
612 unsigned pg = *cur_pg;
Boaz Harrosh20618b22011-08-03 21:54:33 -0700613 int cur_len = len;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300614 struct request_queue *q =
615 osd_request_queue(_io_od(ios, per_dev->dev));
616
Boaz Harrosh04f83452011-05-22 19:52:19 +0300617 if (per_dev->bio == NULL) {
Boaz Harrosh20618b22011-08-03 21:54:33 -0700618 unsigned pages_in_stripe = ios->layout->group_width *
Boaz Harrosh04f83452011-05-22 19:52:19 +0300619 (ios->layout->stripe_unit / PAGE_SIZE);
Boaz Harrosh96218552011-10-31 14:47:32 -0700620 unsigned bio_size = (ios->nr_pages + pages_in_stripe) /
Boaz Harrosh20618b22011-08-03 21:54:33 -0700621 ios->layout->group_width;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300622
623 if (BIO_MAX_PAGES_KMALLOC < bio_size)
624 bio_size = BIO_MAX_PAGES_KMALLOC;
625
626 per_dev->bio = bio_kmalloc(gfp_flags, bio_size);
627 if (unlikely(!per_dev->bio)) {
628 dprintk("Faild to allocate BIO size=%u\n", bio_size);
629 return -ENOMEM;
630 }
631 }
632
633 while (cur_len > 0) {
634 unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
635 unsigned added_len;
636
Boaz Harrosh96218552011-10-31 14:47:32 -0700637 BUG_ON(ios->nr_pages <= pg);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300638 cur_len -= pglen;
639
640 added_len = bio_add_pc_page(q, per_dev->bio,
Boaz Harrosh96218552011-10-31 14:47:32 -0700641 ios->pages[pg], pglen, pgbase);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300642 if (unlikely(pglen != added_len))
643 return -ENOMEM;
644 pgbase = 0;
645 ++pg;
646 }
647 BUG_ON(cur_len);
648
Boaz Harrosh20618b22011-08-03 21:54:33 -0700649 per_dev->length += len;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300650 *cur_pg = pg;
651 return 0;
652}
653
654static int _prepare_one_group(struct objio_state *ios, u64 length,
655 struct _striping_info *si, unsigned *last_pg,
656 gfp_t gfp_flags)
657{
658 unsigned stripe_unit = ios->layout->stripe_unit;
659 unsigned mirrors_p1 = ios->layout->mirrors_p1;
660 unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
661 unsigned dev = si->dev;
662 unsigned first_dev = dev - (dev % devs_in_group);
663 unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
664 unsigned cur_pg = *last_pg;
665 int ret = 0;
666
667 while (length) {
Boaz Harrosh9af7db32011-08-03 21:52:51 -0700668 struct _objio_per_comp *per_dev = &ios->per_dev[dev - first_dev];
Boaz Harrosh04f83452011-05-22 19:52:19 +0300669 unsigned cur_len, page_off = 0;
670
671 if (!per_dev->length) {
672 per_dev->dev = dev;
673 if (dev < si->dev) {
674 per_dev->offset = si->obj_offset + stripe_unit -
675 si->unit_off;
676 cur_len = stripe_unit;
677 } else if (dev == si->dev) {
678 per_dev->offset = si->obj_offset;
679 cur_len = stripe_unit - si->unit_off;
680 page_off = si->unit_off & ~PAGE_MASK;
681 BUG_ON(page_off &&
Boaz Harrosh96218552011-10-31 14:47:32 -0700682 (page_off != ios->pgbase));
Boaz Harrosh04f83452011-05-22 19:52:19 +0300683 } else { /* dev > si->dev */
684 per_dev->offset = si->obj_offset - si->unit_off;
685 cur_len = stripe_unit;
686 }
687
Boaz Harrosh9af7db32011-08-03 21:52:51 -0700688 if (max_comp < dev - first_dev)
689 max_comp = dev - first_dev;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300690 } else {
691 cur_len = stripe_unit;
692 }
693 if (cur_len >= length)
694 cur_len = length;
695
696 ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
697 cur_len, gfp_flags);
698 if (unlikely(ret))
699 goto out;
700
701 dev += mirrors_p1;
702 dev = (dev % devs_in_group) + first_dev;
703
704 length -= cur_len;
705 ios->length += cur_len;
706 }
707out:
708 ios->numdevs = max_comp + mirrors_p1;
709 *last_pg = cur_pg;
710 return ret;
711}
712
713static int _io_rw_pagelist(struct objio_state *ios, gfp_t gfp_flags)
714{
Boaz Harrosh96218552011-10-31 14:47:32 -0700715 u64 length = ios->count;
716 u64 offset = ios->offset;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300717 struct _striping_info si;
718 unsigned last_pg = 0;
719 int ret = 0;
720
721 while (length) {
722 _calc_stripe_info(ios, offset, &si);
723
724 if (length < si.group_length)
725 si.group_length = length;
726
727 ret = _prepare_one_group(ios, si.group_length, &si, &last_pg, gfp_flags);
728 if (unlikely(ret))
729 goto out;
730
731 offset += si.group_length;
732 length -= si.group_length;
733 }
734
735out:
736 if (!ios->length)
737 return ret;
738
739 return 0;
740}
741
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700742static int _sync_done(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300743{
744 struct completion *waiting = ios->private;
745
746 complete(waiting);
747 return 0;
748}
749
750static void _last_io(struct kref *kref)
751{
752 struct objio_state *ios = container_of(kref, struct objio_state, kref);
753
754 ios->done(ios);
755}
756
757static void _done_io(struct osd_request *or, void *p)
758{
759 struct objio_state *ios = p;
760
761 kref_put(&ios->kref, _last_io);
762}
763
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700764static int _io_exec(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300765{
766 DECLARE_COMPLETION_ONSTACK(wait);
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700767 int ret = 0;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300768 unsigned i;
769 objio_done_fn saved_done_fn = ios->done;
Boaz Harrosh96218552011-10-31 14:47:32 -0700770 bool sync = ios->sync;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300771
772 if (sync) {
773 ios->done = _sync_done;
774 ios->private = &wait;
775 }
776
777 kref_init(&ios->kref);
778
779 for (i = 0; i < ios->numdevs; i++) {
780 struct osd_request *or = ios->per_dev[i].or;
781
782 if (!or)
783 continue;
784
785 kref_get(&ios->kref);
786 osd_execute_request_async(or, _done_io, ios);
787 }
788
789 kref_put(&ios->kref, _last_io);
790
791 if (sync) {
792 wait_for_completion(&wait);
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700793 ret = saved_done_fn(ios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300794 }
795
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700796 return ret;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300797}
798
799/*
800 * read
801 */
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700802static int _read_done(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300803{
804 ssize_t status;
805 int ret = _io_check(ios, false);
806
807 _io_free(ios);
808
809 if (likely(!ret))
810 status = ios->length;
811 else
812 status = ret;
813
Boaz Harroshe2e04352011-10-31 15:03:35 -0700814 objlayout_read_done(&ios->oir, status, ios->sync);
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700815 return ret;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300816}
817
818static int _read_mirrors(struct objio_state *ios, unsigned cur_comp)
819{
820 struct osd_request *or = NULL;
821 struct _objio_per_comp *per_dev = &ios->per_dev[cur_comp];
822 unsigned dev = per_dev->dev;
823 struct pnfs_osd_object_cred *cred =
Boaz Harrosh9af7db32011-08-03 21:52:51 -0700824 &ios->layout->comps[cur_comp];
Boaz Harrosh04f83452011-05-22 19:52:19 +0300825 struct osd_obj_id obj = {
826 .partition = cred->oc_object_id.oid_partition_id,
827 .id = cred->oc_object_id.oid_object_id,
828 };
829 int ret;
830
831 or = osd_start_request(_io_od(ios, dev), GFP_KERNEL);
832 if (unlikely(!or)) {
833 ret = -ENOMEM;
834 goto err;
835 }
836 per_dev->or = or;
837
838 osd_req_read(or, &obj, per_dev->offset, per_dev->bio, per_dev->length);
839
840 ret = osd_finalize_request(or, 0, cred->oc_cap.cred, NULL);
841 if (ret) {
842 dprintk("%s: Faild to osd_finalize_request() => %d\n",
843 __func__, ret);
844 goto err;
845 }
846
847 dprintk("%s:[%d] dev=%d obj=0x%llx start=0x%llx length=0x%lx\n",
848 __func__, cur_comp, dev, obj.id, _LLU(per_dev->offset),
849 per_dev->length);
850
851err:
852 return ret;
853}
854
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700855static int _read_exec(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300856{
857 unsigned i;
858 int ret;
859
860 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
861 if (!ios->per_dev[i].length)
862 continue;
863 ret = _read_mirrors(ios, i);
864 if (unlikely(ret))
865 goto err;
866 }
867
868 ios->done = _read_done;
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700869 return _io_exec(ios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300870
871err:
872 _io_free(ios);
873 return ret;
874}
875
Boaz Harrosh96218552011-10-31 14:47:32 -0700876int objio_read_pagelist(struct nfs_read_data *rdata)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300877{
Boaz Harrosh96218552011-10-31 14:47:32 -0700878 struct objio_state *ios;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300879 int ret;
880
Boaz Harrosh96218552011-10-31 14:47:32 -0700881 ret = objio_alloc_io_state(NFS_I(rdata->inode)->layout,
882 rdata->lseg, rdata->args.pages, rdata->args.pgbase,
883 rdata->args.offset, rdata->args.count, rdata,
884 GFP_KERNEL, &ios);
885 if (unlikely(ret))
886 return ret;
887
Boaz Harrosh04f83452011-05-22 19:52:19 +0300888 ret = _io_rw_pagelist(ios, GFP_KERNEL);
889 if (unlikely(ret))
890 return ret;
891
892 return _read_exec(ios);
893}
894
895/*
896 * write
897 */
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700898static int _write_done(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300899{
900 ssize_t status;
901 int ret = _io_check(ios, true);
902
903 _io_free(ios);
904
905 if (likely(!ret)) {
906 /* FIXME: should be based on the OSD's persistence model
907 * See OSD2r05 Section 4.13 Data persistence model */
Boaz Harroshe2e04352011-10-31 15:03:35 -0700908 ios->oir.committed = NFS_FILE_SYNC;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300909 status = ios->length;
910 } else {
911 status = ret;
912 }
913
Boaz Harroshe2e04352011-10-31 15:03:35 -0700914 objlayout_write_done(&ios->oir, status, ios->sync);
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700915 return ret;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300916}
917
918static int _write_mirrors(struct objio_state *ios, unsigned cur_comp)
919{
920 struct _objio_per_comp *master_dev = &ios->per_dev[cur_comp];
921 unsigned dev = ios->per_dev[cur_comp].dev;
922 unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
923 int ret;
924
925 for (; cur_comp < last_comp; ++cur_comp, ++dev) {
926 struct osd_request *or = NULL;
927 struct pnfs_osd_object_cred *cred =
Boaz Harrosh9af7db32011-08-03 21:52:51 -0700928 &ios->layout->comps[cur_comp];
Boaz Harrosh04f83452011-05-22 19:52:19 +0300929 struct osd_obj_id obj = {
930 .partition = cred->oc_object_id.oid_partition_id,
931 .id = cred->oc_object_id.oid_object_id,
932 };
933 struct _objio_per_comp *per_dev = &ios->per_dev[cur_comp];
934 struct bio *bio;
935
936 or = osd_start_request(_io_od(ios, dev), GFP_NOFS);
937 if (unlikely(!or)) {
938 ret = -ENOMEM;
939 goto err;
940 }
941 per_dev->or = or;
942
943 if (per_dev != master_dev) {
944 bio = bio_kmalloc(GFP_NOFS,
945 master_dev->bio->bi_max_vecs);
946 if (unlikely(!bio)) {
947 dprintk("Faild to allocate BIO size=%u\n",
948 master_dev->bio->bi_max_vecs);
949 ret = -ENOMEM;
950 goto err;
951 }
952
953 __bio_clone(bio, master_dev->bio);
954 bio->bi_bdev = NULL;
955 bio->bi_next = NULL;
956 per_dev->bio = bio;
957 per_dev->dev = dev;
958 per_dev->length = master_dev->length;
959 per_dev->offset = master_dev->offset;
960 } else {
961 bio = master_dev->bio;
962 bio->bi_rw |= REQ_WRITE;
963 }
964
965 osd_req_write(or, &obj, per_dev->offset, bio, per_dev->length);
966
967 ret = osd_finalize_request(or, 0, cred->oc_cap.cred, NULL);
968 if (ret) {
969 dprintk("%s: Faild to osd_finalize_request() => %d\n",
970 __func__, ret);
971 goto err;
972 }
973
974 dprintk("%s:[%d] dev=%d obj=0x%llx start=0x%llx length=0x%lx\n",
975 __func__, cur_comp, dev, obj.id, _LLU(per_dev->offset),
976 per_dev->length);
977 }
978
979err:
980 return ret;
981}
982
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700983static int _write_exec(struct objio_state *ios)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300984{
985 unsigned i;
986 int ret;
987
988 for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
989 if (!ios->per_dev[i].length)
990 continue;
991 ret = _write_mirrors(ios, i);
992 if (unlikely(ret))
993 goto err;
994 }
995
996 ios->done = _write_done;
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700997 return _io_exec(ios);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300998
999err:
1000 _io_free(ios);
1001 return ret;
1002}
1003
Boaz Harrosh96218552011-10-31 14:47:32 -07001004int objio_write_pagelist(struct nfs_write_data *wdata, int how)
Boaz Harrosh04f83452011-05-22 19:52:19 +03001005{
Boaz Harrosh96218552011-10-31 14:47:32 -07001006 struct objio_state *ios;
Boaz Harrosh04f83452011-05-22 19:52:19 +03001007 int ret;
1008
Boaz Harrosh96218552011-10-31 14:47:32 -07001009 ret = objio_alloc_io_state(NFS_I(wdata->inode)->layout,
1010 wdata->lseg, wdata->args.pages, wdata->args.pgbase,
1011 wdata->args.offset, wdata->args.count, wdata, GFP_NOFS,
1012 &ios);
1013 if (unlikely(ret))
1014 return ret;
1015
1016 ios->sync = 0 != (how & FLUSH_SYNC);
1017
Boaz Harrosh04f83452011-05-22 19:52:19 +03001018 /* TODO: ios->stable = stable; */
1019 ret = _io_rw_pagelist(ios, GFP_NOFS);
1020 if (unlikely(ret))
1021 return ret;
1022
1023 return _write_exec(ios);
1024}
1025
Boaz Harrosh93420772011-05-25 21:25:29 +03001026static bool objio_pg_test(struct nfs_pageio_descriptor *pgio,
1027 struct nfs_page *prev, struct nfs_page *req)
1028{
1029 if (!pnfs_generic_pg_test(pgio, prev, req))
1030 return false;
1031
1032 return pgio->pg_count + req->wb_bytes <=
1033 OBJIO_LSEG(pgio->pg_lseg)->max_io_size;
1034}
1035
Trond Myklebust1751c362011-06-10 13:30:23 -04001036static const struct nfs_pageio_ops objio_pg_read_ops = {
Trond Myklebustd8007d42011-06-10 13:30:23 -04001037 .pg_init = pnfs_generic_pg_init_read,
Trond Myklebust1751c362011-06-10 13:30:23 -04001038 .pg_test = objio_pg_test,
Trond Myklebust493292d2011-07-13 15:58:28 -04001039 .pg_doio = pnfs_generic_pg_readpages,
Trond Myklebust1751c362011-06-10 13:30:23 -04001040};
1041
1042static const struct nfs_pageio_ops objio_pg_write_ops = {
Trond Myklebustd8007d42011-06-10 13:30:23 -04001043 .pg_init = pnfs_generic_pg_init_write,
Trond Myklebust1751c362011-06-10 13:30:23 -04001044 .pg_test = objio_pg_test,
Trond Myklebustdce81292011-07-13 15:59:19 -04001045 .pg_doio = pnfs_generic_pg_writepages,
Trond Myklebust1751c362011-06-10 13:30:23 -04001046};
1047
Benny Halevyc93407d2011-05-22 19:49:06 +03001048static struct pnfs_layoutdriver_type objlayout_type = {
1049 .id = LAYOUT_OSD2_OBJECTS,
1050 .name = "LAYOUT_OSD2_OBJECTS",
Benny Halevy8a1636c2010-07-14 15:43:57 -04001051 .flags = PNFS_LAYOUTRET_ON_SETATTR,
Boaz Harrosh09f5bf42011-05-22 19:50:20 +03001052
Benny Halevye51b8412011-05-22 19:51:48 +03001053 .alloc_layout_hdr = objlayout_alloc_layout_hdr,
1054 .free_layout_hdr = objlayout_free_layout_hdr,
1055
Boaz Harrosh09f5bf42011-05-22 19:50:20 +03001056 .alloc_lseg = objlayout_alloc_lseg,
1057 .free_lseg = objlayout_free_lseg,
Boaz Harroshb6c05f12011-05-26 21:45:34 +03001058
Boaz Harrosh04f83452011-05-22 19:52:19 +03001059 .read_pagelist = objlayout_read_pagelist,
1060 .write_pagelist = objlayout_write_pagelist,
Trond Myklebust1751c362011-06-10 13:30:23 -04001061 .pg_read_ops = &objio_pg_read_ops,
1062 .pg_write_ops = &objio_pg_write_ops,
Boaz Harrosh04f83452011-05-22 19:52:19 +03001063
Boaz Harroshb6c05f12011-05-26 21:45:34 +03001064 .free_deviceid_node = objio_free_deviceid_node,
Boaz Harroshadb58532011-05-26 21:49:46 +03001065
Boaz Harrosha0fe8bf2011-05-22 19:54:13 +03001066 .encode_layoutcommit = objlayout_encode_layoutcommit,
Boaz Harroshadb58532011-05-26 21:49:46 +03001067 .encode_layoutreturn = objlayout_encode_layoutreturn,
Benny Halevyc93407d2011-05-22 19:49:06 +03001068};
1069
1070MODULE_DESCRIPTION("pNFS Layout Driver for OSD2 objects");
1071MODULE_AUTHOR("Benny Halevy <bhalevy@panasas.com>");
1072MODULE_LICENSE("GPL");
1073
1074static int __init
1075objlayout_init(void)
1076{
1077 int ret = pnfs_register_layoutdriver(&objlayout_type);
1078
1079 if (ret)
1080 printk(KERN_INFO
1081 "%s: Registering OSD pNFS Layout Driver failed: error=%d\n",
1082 __func__, ret);
1083 else
1084 printk(KERN_INFO "%s: Registered OSD pNFS Layout Driver\n",
1085 __func__);
1086 return ret;
1087}
1088
1089static void __exit
1090objlayout_exit(void)
1091{
1092 pnfs_unregister_layoutdriver(&objlayout_type);
1093 printk(KERN_INFO "%s: Unregistered OSD pNFS Layout Driver\n",
1094 __func__);
1095}
1096
J. Bruce Fieldsf85ef692011-07-15 19:18:42 -04001097MODULE_ALIAS("nfs-layouttype4-2");
1098
Benny Halevyc93407d2011-05-22 19:49:06 +03001099module_init(objlayout_init);
1100module_exit(objlayout_exit);