objio_osd.c 16.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 *  pNFS Objects layout implementation over open-osd initiator library
 *
 *  Copyright (C) 2009 Panasas Inc. [year of first publication]
 *  All rights reserved.
 *
 *  Benny Halevy <bhalevy@panasas.com>
 *  Boaz Harrosh <bharrosh@panasas.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  See the file COPYING included with this distribution for more details.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. Neither the name of the Panasas company nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <linux/module.h>
41
#include <scsi/osd_ore.h>
42 43 44 45 46

#include "objlayout.h"

#define NFSDBG_FACILITY         NFSDBG_PNFS_LD

47 48
struct objio_dev_ent {
	struct nfs4_deviceid_node id_node;
49
	struct ore_dev od;
50 51 52 53 54 55 56
};

static void
objio_free_deviceid_node(struct nfs4_deviceid_node *d)
{
	struct objio_dev_ent *de = container_of(d, struct objio_dev_ent, id_node);

57 58
	dprintk("%s: free od=%p\n", __func__, de->od.od);
	osduld_put_device(de->od.od);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	kfree(de);
}

static struct objio_dev_ent *_dev_list_find(const struct nfs_server *nfss,
	const struct nfs4_deviceid *d_id)
{
	struct nfs4_deviceid_node *d;
	struct objio_dev_ent *de;

	d = nfs4_find_get_deviceid(nfss->pnfs_curr_ld, nfss->nfs_client, d_id);
	if (!d)
		return NULL;

	de = container_of(d, struct objio_dev_ent, id_node);
	return de;
}

static struct objio_dev_ent *
_dev_list_add(const struct nfs_server *nfss,
	const struct nfs4_deviceid *d_id, struct osd_dev *od,
	gfp_t gfp_flags)
{
	struct nfs4_deviceid_node *d;
	struct objio_dev_ent *de = kzalloc(sizeof(*de), gfp_flags);
	struct objio_dev_ent *n;

	if (!de) {
		dprintk("%s: -ENOMEM od=%p\n", __func__, od);
		return NULL;
	}

	dprintk("%s: Adding od=%p\n", __func__, od);
	nfs4_init_deviceid_node(&de->id_node,
				nfss->pnfs_curr_ld,
				nfss->nfs_client,
				d_id);
95
	de->od.od = od;
96 97 98 99

	d = nfs4_insert_deviceid_node(&de->id_node);
	n = container_of(d, struct objio_dev_ent, id_node);
	if (n != de) {
100
		dprintk("%s: Race with other n->od=%p\n", __func__, n->od.od);
101 102 103 104 105 106 107
		objio_free_deviceid_node(&de->id_node);
		de = n;
	}

	return de;
}

108 109 110
struct objio_segment {
	struct pnfs_layout_segment lseg;

111 112
	struct ore_layout layout;
	struct ore_components oc;
113 114 115 116 117 118 119 120
};

static inline struct objio_segment *
OBJIO_LSEG(struct pnfs_layout_segment *lseg)
{
	return container_of(lseg, struct objio_segment, lseg);
}

121 122
struct objio_state {
	/* Generic layer */
123
	struct objlayout_io_res oir;
124

125
	bool sync;
126 127
	/*FIXME: Support for extra_bytes at ore_get_rw_state() */
	struct ore_io_state *ios;
128 129
};

130 131
/* Send and wait for a get_device_info of devices in the layout,
   then look them up with the osd_initiator library */
132 133 134
static int objio_devices_lookup(struct pnfs_layout_hdr *pnfslay,
	struct objio_segment *objio_seg, unsigned c, struct nfs4_deviceid *d_id,
	gfp_t gfp_flags)
135 136 137 138 139 140 141 142
{
	struct pnfs_osd_deviceaddr *deviceaddr;
	struct objio_dev_ent *ode;
	struct osd_dev *od;
	struct osd_dev_info odi;
	int err;

	ode = _dev_list_find(NFS_SERVER(pnfslay->plh_inode), d_id);
143 144 145 146
	if (ode) {
		objio_seg->oc.ods[c] = &ode->od; /* must use container_of */
		return 0;
	}
147 148 149 150 151

	err = objlayout_get_deviceinfo(pnfslay, d_id, &deviceaddr, gfp_flags);
	if (unlikely(err)) {
		dprintk("%s: objlayout_get_deviceinfo dev(%llx:%llx) =>%d\n",
			__func__, _DEVID_LO(d_id), _DEVID_HI(d_id), err);
152
		return err;
153 154 155 156
	}

	odi.systemid_len = deviceaddr->oda_systemid.len;
	if (odi.systemid_len > sizeof(odi.systemid)) {
157 158
		dprintk("%s: odi.systemid_len > sizeof(systemid=%zd)\n",
			__func__, sizeof(odi.systemid));
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
		err = -EINVAL;
		goto out;
	} else if (odi.systemid_len)
		memcpy(odi.systemid, deviceaddr->oda_systemid.data,
		       odi.systemid_len);
	odi.osdname_len	 = deviceaddr->oda_osdname.len;
	odi.osdname	 = (u8 *)deviceaddr->oda_osdname.data;

	if (!odi.osdname_len && !odi.systemid_len) {
		dprintk("%s: !odi.osdname_len && !odi.systemid_len\n",
			__func__);
		err = -ENODEV;
		goto out;
	}

	od = osduld_info_lookup(&odi);
	if (unlikely(IS_ERR(od))) {
		err = PTR_ERR(od);
		dprintk("%s: osduld_info_lookup => %d\n", __func__, err);
		goto out;
	}

	ode = _dev_list_add(NFS_SERVER(pnfslay->plh_inode), d_id, od,
			    gfp_flags);
183 184 185
	objio_seg->oc.ods[c] = &ode->od; /* must use container_of */
	dprintk("Adding new dev_id(%llx:%llx)\n",
		_DEVID_LO(d_id), _DEVID_HI(d_id));
186 187 188 189 190
out:
	objlayout_put_deviceinfo(deviceaddr);
	return err;
}

191 192
static void copy_single_comp(struct ore_components *oc, unsigned c,
			     struct pnfs_osd_object_cred *src_comp)
193
{
194
	struct ore_comp *ocomp = &oc->comps[c];
195

196 197
	WARN_ON(src_comp->oc_cap_key.cred_len > 0); /* libosd is NO_SEC only */
	WARN_ON(src_comp->oc_cap.cred_len > sizeof(ocomp->cred));
198

199 200
	ocomp->obj.partition = src_comp->oc_object_id.oid_partition_id;
	ocomp->obj.id = src_comp->oc_object_id.oid_object_id;
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	memcpy(ocomp->cred, src_comp->oc_cap.cred, sizeof(ocomp->cred));
}

int __alloc_objio_seg(unsigned numdevs, gfp_t gfp_flags,
		       struct objio_segment **pseg)
{
	struct __alloc_objio_segment {
		struct objio_segment olseg;
		struct ore_dev *ods[numdevs];
		struct ore_comp	comps[numdevs];
	} *aolseg;

	aolseg = kzalloc(sizeof(*aolseg), gfp_flags);
	if (unlikely(!aolseg)) {
		dprintk("%s: Faild allocation numdevs=%d size=%zd\n", __func__,
			numdevs, sizeof(*aolseg));
		return -ENOMEM;
	}

	aolseg->olseg.oc.numdevs = numdevs;
	aolseg->olseg.oc.single_comp = EC_MULTPLE_COMPS;
	aolseg->olseg.oc.comps = aolseg->comps;
	aolseg->olseg.oc.ods = aolseg->ods;

	*pseg = &aolseg->olseg;
	return 0;
228 229 230 231 232 233 234 235 236 237 238
}

int objio_alloc_lseg(struct pnfs_layout_segment **outp,
	struct pnfs_layout_hdr *pnfslay,
	struct pnfs_layout_range *range,
	struct xdr_stream *xdr,
	gfp_t gfp_flags)
{
	struct objio_segment *objio_seg;
	struct pnfs_osd_xdr_decode_layout_iter iter;
	struct pnfs_osd_layout layout;
239 240
	struct pnfs_osd_object_cred src_comp;
	unsigned cur_comp;
241 242 243 244 245 246
	int err;

	err = pnfs_osd_xdr_decode_layout_map(&layout, &iter, xdr);
	if (unlikely(err))
		return err;

247
	err = __alloc_objio_seg(layout.olo_num_comps, gfp_flags, &objio_seg);
248 249 250
	if (unlikely(err))
		return err;

251 252 253 254 255
	objio_seg->layout.stripe_unit = layout.olo_map.odm_stripe_unit;
	objio_seg->layout.group_width = layout.olo_map.odm_group_width;
	objio_seg->layout.group_depth = layout.olo_map.odm_group_depth;
	objio_seg->layout.mirrors_p1 = layout.olo_map.odm_mirror_cnt + 1;
	objio_seg->layout.raid_algorithm = layout.olo_map.odm_raid_algorithm;
256

257 258
	err = ore_verify_layout(layout.olo_map.odm_num_comps,
					  &objio_seg->layout);
259 260 261
	if (unlikely(err))
		goto err;

262 263 264 265 266 267 268 269 270 271
	objio_seg->oc.first_dev = layout.olo_comps_index;
	cur_comp = 0;
	while (pnfs_osd_xdr_decode_layout_comp(&src_comp, &iter, xdr, &err)) {
		copy_single_comp(&objio_seg->oc, cur_comp, &src_comp);
		err = objio_devices_lookup(pnfslay, objio_seg, cur_comp,
					   &src_comp.oc_object_id.oid_device_id,
					   gfp_flags);
		if (err)
			goto err;
		++cur_comp;
272
	}
273 274 275
	/* pnfs_osd_xdr_decode_layout_comp returns false on error */
	if (unlikely(err))
		goto err;
276

277 278 279 280 281 282 283 284 285 286 287 288
	*outp = &objio_seg->lseg;
	return 0;

err:
	kfree(objio_seg);
	dprintk("%s: Error: return %d\n", __func__, err);
	*outp = NULL;
	return err;
}

void objio_free_lseg(struct pnfs_layout_segment *lseg)
{
289
	int i;
290 291
	struct objio_segment *objio_seg = OBJIO_LSEG(lseg);

292 293 294 295 296
	for (i = 0; i < objio_seg->oc.numdevs; i++) {
		struct ore_dev *od = objio_seg->oc.ods[i];
		struct objio_dev_ent *ode;

		if (!od)
297
			break;
298 299
		ode = container_of(od, typeof(*ode), od);
		nfs4_put_deviceid_node(&ode->id_node);
300
	}
301 302 303
	kfree(objio_seg);
}

304
static int
305
objio_alloc_io_state(struct pnfs_layout_hdr *pnfs_layout_type, bool is_reading,
306 307 308
	struct pnfs_layout_segment *lseg, struct page **pages, unsigned pgbase,
	loff_t offset, size_t count, void *rpcdata, gfp_t gfp_flags,
	struct objio_state **outp)
309 310
{
	struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
311 312
	struct ore_io_state *ios;
	int ret;
313 314
	struct __alloc_objio_state {
		struct objio_state objios;
315
		struct pnfs_osd_ioerr ioerrs[objio_seg->oc.numdevs];
316 317 318 319
	} *aos;

	aos = kzalloc(sizeof(*aos), gfp_flags);
	if (unlikely(!aos))
320 321
		return -ENOMEM;

322
	objlayout_init_ioerrs(&aos->objios.oir, objio_seg->oc.numdevs,
323 324
			aos->ioerrs, rpcdata, pnfs_layout_type);

325 326 327 328 329 330 331
	ret = ore_get_rw_state(&objio_seg->layout, &objio_seg->oc, is_reading,
			       offset, count, &ios);
	if (unlikely(ret)) {
		kfree(aos);
		return ret;
	}

332 333
	ios->pages = pages;
	ios->pgbase = pgbase;
334
	ios->private = aos;
335 336
	BUG_ON(ios->nr_pages > (pgbase + count + PAGE_SIZE - 1) >> PAGE_SHIFT);

337 338 339
	aos->objios.sync = 0;
	aos->objios.ios = ios;
	*outp = &aos->objios;
340 341 342
	return 0;
}

343
void objio_free_result(struct objlayout_io_res *oir)
344
{
345
	struct objio_state *objios = container_of(oir, struct objio_state, oir);
346

347 348
	ore_put_io_state(objios->ios);
	kfree(objios);
349 350
}

351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
enum pnfs_osd_errno osd_pri_2_pnfs_err(enum osd_err_priority oep)
{
	switch (oep) {
	case OSD_ERR_PRI_NO_ERROR:
		return (enum pnfs_osd_errno)0;

	case OSD_ERR_PRI_CLEAR_PAGES:
		BUG_ON(1);
		return 0;

	case OSD_ERR_PRI_RESOURCE:
		return PNFS_OSD_ERR_RESOURCE;
	case OSD_ERR_PRI_BAD_CRED:
		return PNFS_OSD_ERR_BAD_CRED;
	case OSD_ERR_PRI_NO_ACCESS:
		return PNFS_OSD_ERR_NO_ACCESS;
	case OSD_ERR_PRI_UNREACHABLE:
		return PNFS_OSD_ERR_UNREACHABLE;
	case OSD_ERR_PRI_NOT_FOUND:
		return PNFS_OSD_ERR_NOT_FOUND;
	case OSD_ERR_PRI_NO_SPACE:
		return PNFS_OSD_ERR_NO_SPACE;
	default:
		WARN_ON(1);
		/* fallthrough */
	case OSD_ERR_PRI_EIO:
		return PNFS_OSD_ERR_EIO;
	}
}

381
static void __on_dev_error(struct ore_io_state *ios,
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	struct ore_dev *od, unsigned dev_index, enum osd_err_priority oep,
	u64 dev_offset, u64  dev_len)
{
	struct objio_state *objios = ios->private;
	struct pnfs_osd_objid pooid;
	struct objio_dev_ent *ode = container_of(od, typeof(*ode), od);
	/* FIXME: what to do with more-then-one-group layouts. We need to
	 * translate from ore_io_state index to oc->comps index
	 */
	unsigned comp = dev_index;

	pooid.oid_device_id = ode->id_node.deviceid;
	pooid.oid_partition_id = ios->oc->comps[comp].obj.partition;
	pooid.oid_object_id = ios->oc->comps[comp].obj.id;

	objlayout_io_set_result(&objios->oir, comp,
				&pooid, osd_pri_2_pnfs_err(oep),
399
				dev_offset, dev_len, !ios->reading);
400 401
}

402 403 404
/*
 * read
 */
405
static void _read_done(struct ore_io_state *ios, void *private)
406
{
407
	struct objio_state *objios = private;
408
	ssize_t status;
409
	int ret = ore_check_io(ios, &__on_dev_error);
410

411
	/* FIXME: _io_free(ios) can we dealocate the libosd resources; */
412 413 414 415 416 417

	if (likely(!ret))
		status = ios->length;
	else
		status = ret;

418
	objlayout_read_done(&objios->oir, status, objios->sync);
419 420
}

421
int objio_read_pagelist(struct nfs_read_data *rdata)
422
{
423
	struct objio_state *objios;
424 425
	int ret;

426
	ret = objio_alloc_io_state(NFS_I(rdata->inode)->layout, true,
427 428
			rdata->lseg, rdata->args.pages, rdata->args.pgbase,
			rdata->args.offset, rdata->args.count, rdata,
429
			GFP_KERNEL, &objios);
430 431 432
	if (unlikely(ret))
		return ret;

433 434 435 436
	objios->ios->done = _read_done;
	dprintk("%s: offset=0x%llx length=0x%x\n", __func__,
		rdata->args.offset, rdata->args.count);
	return ore_read(objios->ios);
437 438 439 440 441
}

/*
 * write
 */
442
static void _write_done(struct ore_io_state *ios, void *private)
443
{
444
	struct objio_state *objios = private;
445
	ssize_t status;
446
	int ret = ore_check_io(ios, &__on_dev_error);
447

448
	/* FIXME: _io_free(ios) can we dealocate the libosd resources; */
449 450 451 452

	if (likely(!ret)) {
		/* FIXME: should be based on the OSD's persistence model
		 * See OSD2r05 Section 4.13 Data persistence model */
453
		objios->oir.committed = NFS_FILE_SYNC;
454 455 456 457 458
		status = ios->length;
	} else {
		status = ret;
	}

459
	objlayout_write_done(&objios->oir, status, objios->sync);
460 461
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
static struct page *__r4w_get_page(void *priv, u64 offset, bool *uptodate)
{
	struct objio_state *objios = priv;
	struct nfs_write_data *wdata = objios->oir.rpcdata;
	pgoff_t index = offset / PAGE_SIZE;
	struct page *page = find_get_page(wdata->inode->i_mapping, index);

	if (!page) {
		page = find_or_create_page(wdata->inode->i_mapping,
						index, GFP_NOFS);
		if (unlikely(!page)) {
			dprintk("%s: grab_cache_page Failed index=0x%lx\n",
				__func__, index);
			return NULL;
		}
		unlock_page(page);
	}
	if (PageDirty(page) || PageWriteback(page))
		*uptodate = true;
	else
		*uptodate = PageUptodate(page);
	dprintk("%s: index=0x%lx uptodate=%d\n", __func__, index, *uptodate);
	return page;
}

static void __r4w_put_page(void *priv, struct page *page)
{
	dprintk("%s: index=0x%lx\n", __func__, page->index);
	page_cache_release(page);
	return;
}

static const struct _ore_r4w_op _r4w_op = {
	.get_page = &__r4w_get_page,
	.put_page = &__r4w_put_page,
};

499
int objio_write_pagelist(struct nfs_write_data *wdata, int how)
500
{
501
	struct objio_state *objios;
502 503
	int ret;

504
	ret = objio_alloc_io_state(NFS_I(wdata->inode)->layout, false,
505 506
			wdata->lseg, wdata->args.pages, wdata->args.pgbase,
			wdata->args.offset, wdata->args.count, wdata, GFP_NOFS,
507
			&objios);
508 509 510
	if (unlikely(ret))
		return ret;

511
	objios->sync = 0 != (how & FLUSH_SYNC);
512
	objios->ios->r4w = &_r4w_op;
513

514 515 516 517 518 519
	if (!objios->sync)
		objios->ios->done = _write_done;

	dprintk("%s: offset=0x%llx length=0x%x\n", __func__,
		wdata->args.offset, wdata->args.count);
	ret = ore_write(objios->ios);
520 521 522
	if (unlikely(ret))
		return ret;

523 524 525 526
	if (objios->sync)
		_write_done(objios->ios, objios);

	return 0;
527 528
}

529 530 531 532 533 534 535
static bool objio_pg_test(struct nfs_pageio_descriptor *pgio,
			  struct nfs_page *prev, struct nfs_page *req)
{
	if (!pnfs_generic_pg_test(pgio, prev, req))
		return false;

	return pgio->pg_count + req->wb_bytes <=
536
			OBJIO_LSEG(pgio->pg_lseg)->layout.max_io_length;
537 538
}

539
static const struct nfs_pageio_ops objio_pg_read_ops = {
540
	.pg_init = pnfs_generic_pg_init_read,
541
	.pg_test = objio_pg_test,
542
	.pg_doio = pnfs_generic_pg_readpages,
543 544 545
};

static const struct nfs_pageio_ops objio_pg_write_ops = {
546
	.pg_init = pnfs_generic_pg_init_write,
547
	.pg_test = objio_pg_test,
548
	.pg_doio = pnfs_generic_pg_writepages,
549 550
};

551 552 553
static struct pnfs_layoutdriver_type objlayout_type = {
	.id = LAYOUT_OSD2_OBJECTS,
	.name = "LAYOUT_OSD2_OBJECTS",
554 555
	.flags                   = PNFS_LAYOUTRET_ON_SETATTR |
				   PNFS_LAYOUTRET_ON_ERROR,
556

557 558 559
	.alloc_layout_hdr        = objlayout_alloc_layout_hdr,
	.free_layout_hdr         = objlayout_free_layout_hdr,

560 561
	.alloc_lseg              = objlayout_alloc_lseg,
	.free_lseg               = objlayout_free_lseg,
562

563 564
	.read_pagelist           = objlayout_read_pagelist,
	.write_pagelist          = objlayout_write_pagelist,
565 566
	.pg_read_ops             = &objio_pg_read_ops,
	.pg_write_ops            = &objio_pg_write_ops,
567

568
	.free_deviceid_node	 = objio_free_deviceid_node,
569

570
	.encode_layoutcommit	 = objlayout_encode_layoutcommit,
571
	.encode_layoutreturn     = objlayout_encode_layoutreturn,
572 573 574 575 576 577 578 579 580 581 582 583 584
};

MODULE_DESCRIPTION("pNFS Layout Driver for OSD2 objects");
MODULE_AUTHOR("Benny Halevy <bhalevy@panasas.com>");
MODULE_LICENSE("GPL");

static int __init
objlayout_init(void)
{
	int ret = pnfs_register_layoutdriver(&objlayout_type);

	if (ret)
		printk(KERN_INFO
585
			"NFS: %s: Registering OSD pNFS Layout Driver failed: error=%d\n",
586 587
			__func__, ret);
	else
588
		printk(KERN_INFO "NFS: %s: Registered OSD pNFS Layout Driver\n",
589 590 591 592 593 594 595 596
			__func__);
	return ret;
}

static void __exit
objlayout_exit(void)
{
	pnfs_unregister_layoutdriver(&objlayout_type);
597
	printk(KERN_INFO "NFS: %s: Unregistered OSD pNFS Layout Driver\n",
598 599 600
	       __func__);
}

601 602
MODULE_ALIAS("nfs-layouttype4-2");

603 604
module_init(objlayout_init);
module_exit(objlayout_exit);