Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  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 <ooo@electrozaur.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>
 41#include <scsi/osd_ore.h>
 42
 43#include "objlayout.h"
 44#include "../internal.h"
 45
 46#define NFSDBG_FACILITY         NFSDBG_PNFS_LD
 47
 
 
 
 
 
 
 48struct objio_dev_ent {
 49	struct nfs4_deviceid_node id_node;
 50	struct ore_dev od;
 51};
 52
 53static void
 54objio_free_deviceid_node(struct nfs4_deviceid_node *d)
 55{
 56	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);
 59	osduld_put_device(de->od.od);
 60	kfree_rcu(d, rcu);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 61}
 62
 
 
 
 
 
 63struct objio_segment {
 64	struct pnfs_layout_segment lseg;
 65
 66	struct ore_layout layout;
 67	struct ore_components oc;
 
 
 
 
 
 
 
 
 
 
 
 
 68};
 69
 70static inline struct objio_segment *
 71OBJIO_LSEG(struct pnfs_layout_segment *lseg)
 72{
 73	return container_of(lseg, struct objio_segment, lseg);
 74}
 75
 
 
 
 76struct objio_state {
 77	/* Generic layer */
 78	struct objlayout_io_res oir;
 79
 80	bool sync;
 81	/*FIXME: Support for extra_bytes at ore_get_rw_state() */
 82	struct ore_io_state *ios;
 
 
 
 
 
 
 
 
 
 
 
 
 
 83};
 84
 85/* Send and wait for a get_device_info of devices in the layout,
 86   then look them up with the osd_initiator library */
 87struct nfs4_deviceid_node *
 88objio_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
 89			gfp_t gfp_flags)
 90{
 91	struct pnfs_osd_deviceaddr *deviceaddr;
 92	struct objio_dev_ent *ode = NULL;
 
 93	struct osd_dev *od;
 94	struct osd_dev_info odi;
 95	bool retry_flag = true;
 96	__be32 *p;
 97	int err;
 98
 99	deviceaddr = kzalloc(sizeof(*deviceaddr), gfp_flags);
100	if (!deviceaddr)
101		return NULL;
102
103	p = page_address(pdev->pages[0]);
104	pnfs_osd_xdr_decode_deviceaddr(deviceaddr, p);
 
 
 
 
 
 
 
 
105
106	odi.systemid_len = deviceaddr->oda_systemid.len;
107	if (odi.systemid_len > sizeof(odi.systemid)) {
108		dprintk("%s: odi.systemid_len > sizeof(systemid=%zd)\n",
109			__func__, sizeof(odi.systemid));
110		err = -EINVAL;
111		goto out;
112	} else if (odi.systemid_len)
113		memcpy(odi.systemid, deviceaddr->oda_systemid.data,
114		       odi.systemid_len);
115	odi.osdname_len	 = deviceaddr->oda_osdname.len;
116	odi.osdname	 = (u8 *)deviceaddr->oda_osdname.data;
117
118	if (!odi.osdname_len && !odi.systemid_len) {
119		dprintk("%s: !odi.osdname_len && !odi.systemid_len\n",
120			__func__);
121		err = -ENODEV;
122		goto out;
123	}
124
125retry_lookup:
126	od = osduld_info_lookup(&odi);
127	if (IS_ERR(od)) {
128		err = PTR_ERR(od);
129		dprintk("%s: osduld_info_lookup => %d\n", __func__, err);
130		if (err == -ENODEV && retry_flag) {
131			err = objlayout_autologin(deviceaddr);
132			if (likely(!err)) {
133				retry_flag = false;
134				goto retry_lookup;
135			}
136		}
137		goto out;
138	}
139
140	dprintk("Adding new dev_id(%llx:%llx)\n",
141		_DEVID_LO(&pdev->dev_id), _DEVID_HI(&pdev->dev_id));
142
143	ode = kzalloc(sizeof(*ode), gfp_flags);
144	if (!ode) {
145		dprintk("%s: -ENOMEM od=%p\n", __func__, od);
146		goto out;
147	}
148
149	nfs4_init_deviceid_node(&ode->id_node, server, &pdev->dev_id);
150	kfree(deviceaddr);
151
152	ode->od.od = od;
153	return &ode->id_node;
154
155out:
156	kfree(deviceaddr);
157	return NULL;
 
158}
159
160static void copy_single_comp(struct ore_components *oc, unsigned c,
161			     struct pnfs_osd_object_cred *src_comp)
 
162{
163	struct ore_comp *ocomp = &oc->comps[c];
 
164
165	WARN_ON(src_comp->oc_cap_key.cred_len > 0); /* libosd is NO_SEC only */
166	WARN_ON(src_comp->oc_cap.cred_len > sizeof(ocomp->cred));
 
167
168	ocomp->obj.partition = src_comp->oc_object_id.oid_partition_id;
169	ocomp->obj.id = src_comp->oc_object_id.oid_object_id;
 
 
 
 
 
 
170
171	memcpy(ocomp->cred, src_comp->oc_cap.cred, sizeof(ocomp->cred));
 
 
172}
173
174static int __alloc_objio_seg(unsigned numdevs, gfp_t gfp_flags,
175		       struct objio_segment **pseg)
176{
177/*	This is the in memory structure of the objio_segment
178 *
179 *	struct __alloc_objio_segment {
180 *		struct objio_segment olseg;
181 *		struct ore_dev *ods[numdevs];
182 *		struct ore_comp	comps[numdevs];
183 *	} *aolseg;
184 *	NOTE: The code as above compiles and runs perfectly. It is elegant,
185 *	type safe and compact. At some Past time Linus has decided he does not
186 *	like variable length arrays, For the sake of this principal we uglify
187 *	the code as below.
188 */
189	struct objio_segment *lseg;
190	size_t lseg_size = sizeof(*lseg) +
191			numdevs * sizeof(lseg->oc.ods[0]) +
192			numdevs * sizeof(*lseg->oc.comps);
193
194	lseg = kzalloc(lseg_size, gfp_flags);
195	if (unlikely(!lseg)) {
196		dprintk("%s: Failed allocation numdevs=%d size=%zd\n", __func__,
197			numdevs, lseg_size);
198		return -ENOMEM;
199	}
200
201	lseg->oc.numdevs = numdevs;
202	lseg->oc.single_comp = EC_MULTPLE_COMPS;
203	lseg->oc.ods = (void *)(lseg + 1);
204	lseg->oc.comps = (void *)(lseg->oc.ods + numdevs);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
206	*pseg = lseg;
207	return 0;
208}
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210int objio_alloc_lseg(struct pnfs_layout_segment **outp,
211	struct pnfs_layout_hdr *pnfslay,
212	struct pnfs_layout_range *range,
213	struct xdr_stream *xdr,
214	gfp_t gfp_flags)
215{
216	struct nfs_server *server = NFS_SERVER(pnfslay->plh_inode);
217	struct objio_segment *objio_seg;
218	struct pnfs_osd_xdr_decode_layout_iter iter;
219	struct pnfs_osd_layout layout;
220	struct pnfs_osd_object_cred src_comp;
221	unsigned cur_comp;
222	int err;
223
224	err = pnfs_osd_xdr_decode_layout_map(&layout, &iter, xdr);
225	if (unlikely(err))
226		return err;
227
228	err = __alloc_objio_seg(layout.olo_num_comps, gfp_flags, &objio_seg);
229	if (unlikely(err))
230		return err;
231
232	objio_seg->layout.stripe_unit = layout.olo_map.odm_stripe_unit;
233	objio_seg->layout.group_width = layout.olo_map.odm_group_width;
234	objio_seg->layout.group_depth = layout.olo_map.odm_group_depth;
235	objio_seg->layout.mirrors_p1 = layout.olo_map.odm_mirror_cnt + 1;
236	objio_seg->layout.raid_algorithm = layout.olo_map.odm_raid_algorithm;
 
 
237
238	err = ore_verify_layout(layout.olo_map.odm_num_comps,
239					  &objio_seg->layout);
 
 
 
240	if (unlikely(err))
241		goto err;
242
243	objio_seg->oc.first_dev = layout.olo_comps_index;
244	cur_comp = 0;
245	while (pnfs_osd_xdr_decode_layout_comp(&src_comp, &iter, xdr, &err)) {
246		struct nfs4_deviceid_node *d;
247		struct objio_dev_ent *ode;
248
249		copy_single_comp(&objio_seg->oc, cur_comp, &src_comp);
250
251		d = nfs4_find_get_deviceid(server,
252				&src_comp.oc_object_id.oid_device_id,
253				pnfslay->plh_lc_cred, gfp_flags);
254		if (!d) {
255			err = -ENXIO;
256			goto err;
257		}
258
259		ode = container_of(d, struct objio_dev_ent, id_node);
260		objio_seg->oc.ods[cur_comp++] = &ode->od;
 
 
 
 
 
 
 
 
 
 
 
261	}
262	/* pnfs_osd_xdr_decode_layout_comp returns false on error */
263	if (unlikely(err))
264		goto err;
 
 
265
266	*outp = &objio_seg->lseg;
267	return 0;
268
269err:
270	kfree(objio_seg);
271	dprintk("%s: Error: return %d\n", __func__, err);
272	*outp = NULL;
273	return err;
274}
275
276void objio_free_lseg(struct pnfs_layout_segment *lseg)
277{
278	int i;
279	struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
280
281	for (i = 0; i < objio_seg->oc.numdevs; i++) {
282		struct ore_dev *od = objio_seg->oc.ods[i];
283		struct objio_dev_ent *ode;
284
285		if (!od)
286			break;
287		ode = container_of(od, typeof(*ode), od);
288		nfs4_put_deviceid_node(&ode->id_node);
289	}
290	kfree(objio_seg);
291}
292
293static int
294objio_alloc_io_state(struct pnfs_layout_hdr *pnfs_layout_type, bool is_reading,
295	struct pnfs_layout_segment *lseg, struct page **pages, unsigned pgbase,
296	loff_t offset, size_t count, void *rpcdata, gfp_t gfp_flags,
297	struct objio_state **outp)
298{
299	struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
300	struct ore_io_state *ios;
301	int ret;
302	struct __alloc_objio_state {
303		struct objio_state objios;
304		struct pnfs_osd_ioerr ioerrs[objio_seg->oc.numdevs];
305	} *aos;
306
307	aos = kzalloc(sizeof(*aos), gfp_flags);
308	if (unlikely(!aos))
309		return -ENOMEM;
310
311	objlayout_init_ioerrs(&aos->objios.oir, objio_seg->oc.numdevs,
312			aos->ioerrs, rpcdata, pnfs_layout_type);
313
314	ret = ore_get_rw_state(&objio_seg->layout, &objio_seg->oc, is_reading,
315			       offset, count, &ios);
316	if (unlikely(ret)) {
317		kfree(aos);
318		return ret;
319	}
320
321	ios->pages = pages;
322	ios->pgbase = pgbase;
323	ios->private = aos;
324	BUG_ON(ios->nr_pages > (pgbase + count + PAGE_SIZE - 1) >> PAGE_SHIFT);
325
326	aos->objios.sync = 0;
327	aos->objios.ios = ios;
328	*outp = &aos->objios;
329	return 0;
330}
331
332void objio_free_result(struct objlayout_io_res *oir)
333{
334	struct objio_state *objios = container_of(oir, struct objio_state, oir);
 
335
336	ore_put_io_state(objios->ios);
337	kfree(objios);
338}
339
340static enum pnfs_osd_errno osd_pri_2_pnfs_err(enum osd_err_priority oep)
341{
342	switch (oep) {
343	case OSD_ERR_PRI_NO_ERROR:
344		return (enum pnfs_osd_errno)0;
345
346	case OSD_ERR_PRI_CLEAR_PAGES:
347		BUG_ON(1);
348		return 0;
349
350	case OSD_ERR_PRI_RESOURCE:
351		return PNFS_OSD_ERR_RESOURCE;
352	case OSD_ERR_PRI_BAD_CRED:
353		return PNFS_OSD_ERR_BAD_CRED;
354	case OSD_ERR_PRI_NO_ACCESS:
355		return PNFS_OSD_ERR_NO_ACCESS;
356	case OSD_ERR_PRI_UNREACHABLE:
357		return PNFS_OSD_ERR_UNREACHABLE;
358	case OSD_ERR_PRI_NOT_FOUND:
359		return PNFS_OSD_ERR_NOT_FOUND;
360	case OSD_ERR_PRI_NO_SPACE:
361		return PNFS_OSD_ERR_NO_SPACE;
362	default:
363		WARN_ON(1);
364		/* fallthrough */
365	case OSD_ERR_PRI_EIO:
366		return PNFS_OSD_ERR_EIO;
367	}
368}
369
370static void __on_dev_error(struct ore_io_state *ios,
371	struct ore_dev *od, unsigned dev_index, enum osd_err_priority oep,
372	u64 dev_offset, u64  dev_len)
373{
374	struct objio_state *objios = ios->private;
375	struct pnfs_osd_objid pooid;
376	struct objio_dev_ent *ode = container_of(od, typeof(*ode), od);
377	/* FIXME: what to do with more-then-one-group layouts. We need to
378	 * translate from ore_io_state index to oc->comps index
379	 */
380	unsigned comp = dev_index;
381
382	pooid.oid_device_id = ode->id_node.deviceid;
383	pooid.oid_partition_id = ios->oc->comps[comp].obj.partition;
384	pooid.oid_object_id = ios->oc->comps[comp].obj.id;
385
386	objlayout_io_set_result(&objios->oir, comp,
387				&pooid, osd_pri_2_pnfs_err(oep),
388				dev_offset, dev_len, !ios->reading);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
389}
390
391/*
392 * read
393 */
394static void _read_done(struct ore_io_state *ios, void *private)
395{
396	struct objio_state *objios = private;
397	ssize_t status;
398	int ret = ore_check_io(ios, &__on_dev_error);
399
400	/* FIXME: _io_free(ios) can we dealocate the libosd resources; */
401
402	if (likely(!ret))
403		status = ios->length;
404	else
405		status = ret;
406
407	objlayout_read_done(&objios->oir, status, objios->sync);
 
408}
409
410int objio_read_pagelist(struct nfs_pgio_header *hdr)
411{
412	struct objio_state *objios;
 
 
 
 
 
 
 
 
413	int ret;
414
415	ret = objio_alloc_io_state(NFS_I(hdr->inode)->layout, true,
416			hdr->lseg, hdr->args.pages, hdr->args.pgbase,
417			hdr->args.offset, hdr->args.count, hdr,
418			GFP_KERNEL, &objios);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419	if (unlikely(ret))
420		return ret;
421
422	objios->ios->done = _read_done;
423	dprintk("%s: offset=0x%llx length=0x%x\n", __func__,
424		hdr->args.offset, hdr->args.count);
425	ret = ore_read(objios->ios);
426	if (unlikely(ret))
427		objio_free_result(&objios->oir);
428	return ret;
429}
430
431/*
432 * write
433 */
434static void _write_done(struct ore_io_state *ios, void *private)
435{
436	struct objio_state *objios = private;
437	ssize_t status;
438	int ret = ore_check_io(ios, &__on_dev_error);
439
440	/* FIXME: _io_free(ios) can we dealocate the libosd resources; */
441
442	if (likely(!ret)) {
443		/* FIXME: should be based on the OSD's persistence model
444		 * See OSD2r05 Section 4.13 Data persistence model */
445		objios->oir.committed = NFS_FILE_SYNC;
446		status = ios->length;
447	} else {
448		status = ret;
449	}
450
451	objlayout_write_done(&objios->oir, status, objios->sync);
 
452}
453
454static struct page *__r4w_get_page(void *priv, u64 offset, bool *uptodate)
455{
456	struct objio_state *objios = priv;
457	struct nfs_pgio_header *hdr = objios->oir.rpcdata;
458	struct address_space *mapping = hdr->inode->i_mapping;
459	pgoff_t index = offset / PAGE_SIZE;
460	struct page *page;
461	loff_t i_size = i_size_read(hdr->inode);
462
463	if (offset >= i_size) {
464		*uptodate = true;
465		dprintk("%s: g_zero_page index=0x%lx\n", __func__, index);
466		return ZERO_PAGE(0);
467	}
468
469	page = find_get_page(mapping, index);
470	if (!page) {
471		page = find_or_create_page(mapping, index, GFP_NOFS);
472		if (unlikely(!page)) {
473			dprintk("%s: grab_cache_page Failed index=0x%lx\n",
474				__func__, index);
475			return NULL;
476		}
477		unlock_page(page);
478	}
479	*uptodate = PageUptodate(page);
480	dprintk("%s: index=0x%lx uptodate=%d\n", __func__, index, *uptodate);
481	return page;
482}
483
484static void __r4w_put_page(void *priv, struct page *page)
485{
486	dprintk("%s: index=0x%lx\n", __func__,
487		(page == ZERO_PAGE(0)) ? -1UL : page->index);
488	if (ZERO_PAGE(0) != page)
489		put_page(page);
490	return;
491}
492
493static const struct _ore_r4w_op _r4w_op = {
494	.get_page = &__r4w_get_page,
495	.put_page = &__r4w_put_page,
496};
 
 
 
 
 
 
 
 
 
 
 
 
 
497
498int objio_write_pagelist(struct nfs_pgio_header *hdr, int how)
499{
500	struct objio_state *objios;
501	int ret;
 
 
 
 
 
502
503	ret = objio_alloc_io_state(NFS_I(hdr->inode)->layout, false,
504			hdr->lseg, hdr->args.pages, hdr->args.pgbase,
505			hdr->args.offset, hdr->args.count, hdr, GFP_NOFS,
506			&objios);
507	if (unlikely(ret))
508		return ret;
 
 
 
 
 
509
510	objios->sync = 0 != (how & FLUSH_SYNC);
511	objios->ios->r4w = &_r4w_op;
512
513	if (!objios->sync)
514		objios->ios->done = _write_done;
 
 
 
 
515
516	dprintk("%s: offset=0x%llx length=0x%x\n", __func__,
517		hdr->args.offset, hdr->args.count);
518	ret = ore_write(objios->ios);
519	if (unlikely(ret)) {
520		objio_free_result(&objios->oir);
521		return ret;
522	}
523
524	if (objios->sync)
525		_write_done(objios->ios, objios);
526
527	return 0;
528}
529
530/*
531 * Return 0 if @req cannot be coalesced into @pgio, otherwise return the number
532 * of bytes (maximum @req->wb_bytes) that can be coalesced.
533 */
534static size_t objio_pg_test(struct nfs_pageio_descriptor *pgio,
535			  struct nfs_page *prev, struct nfs_page *req)
536{
537	struct nfs_pgio_mirror *mirror = nfs_pgio_current_mirror(pgio);
538	unsigned int size;
539
540	size = pnfs_generic_pg_test(pgio, prev, req);
541
542	if (!size || mirror->pg_count + req->wb_bytes >
543	    (unsigned long)pgio->pg_layout_private)
544		return 0;
 
 
 
 
545
546	return min(size, req->wb_bytes);
547}
548
549static void objio_init_read(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
550{
551	pnfs_generic_pg_init_read(pgio, req);
552	if (unlikely(pgio->pg_lseg == NULL))
553		return; /* Not pNFS */
554
555	pgio->pg_layout_private = (void *)
556				OBJIO_LSEG(pgio->pg_lseg)->layout.max_io_length;
557}
558
559static bool aligned_on_raid_stripe(u64 offset, struct ore_layout *layout,
560				   unsigned long *stripe_end)
561{
562	u32 stripe_off;
563	unsigned stripe_size;
564
565	if (layout->raid_algorithm == PNFS_OSD_RAID_0)
566		return true;
567
568	stripe_size = layout->stripe_unit *
569				(layout->group_width - layout->parity);
570
571	div_u64_rem(offset, stripe_size, &stripe_off);
572	if (!stripe_off)
573		return true;
 
574
575	*stripe_end = stripe_size - stripe_off;
576	return false;
577}
578
579static void objio_init_write(struct nfs_pageio_descriptor *pgio, struct nfs_page *req)
 
580{
581	unsigned long stripe_end = 0;
582	u64 wb_size;
583
584	if (pgio->pg_dreq == NULL)
585		wb_size = i_size_read(pgio->pg_inode) - req_offset(req);
586	else
587		wb_size = nfs_dreq_bytes_left(pgio->pg_dreq);
588
589	pnfs_generic_pg_init_write(pgio, req, wb_size);
590	if (unlikely(pgio->pg_lseg == NULL))
591		return; /* Not pNFS */
592
593	if (req->wb_offset ||
594	    !aligned_on_raid_stripe(req->wb_index * PAGE_SIZE,
595			       &OBJIO_LSEG(pgio->pg_lseg)->layout,
596			       &stripe_end)) {
597		pgio->pg_layout_private = (void *)stripe_end;
598	} else {
599		pgio->pg_layout_private = (void *)
600				OBJIO_LSEG(pgio->pg_lseg)->layout.max_io_length;
601	}
602}
603
604static const struct nfs_pageio_ops objio_pg_read_ops = {
605	.pg_init = objio_init_read,
606	.pg_test = objio_pg_test,
607	.pg_doio = pnfs_generic_pg_readpages,
608	.pg_cleanup = pnfs_generic_pg_cleanup,
609};
610
611static const struct nfs_pageio_ops objio_pg_write_ops = {
612	.pg_init = objio_init_write,
613	.pg_test = objio_pg_test,
614	.pg_doio = pnfs_generic_pg_writepages,
615	.pg_cleanup = pnfs_generic_pg_cleanup,
616};
617
618static struct pnfs_layoutdriver_type objlayout_type = {
619	.id = LAYOUT_OSD2_OBJECTS,
620	.name = "LAYOUT_OSD2_OBJECTS",
621	.flags                   = PNFS_LAYOUTRET_ON_SETATTR |
622				   PNFS_LAYOUTRET_ON_ERROR,
623
624	.max_deviceinfo_size	 = PAGE_SIZE,
625	.owner		       	 = THIS_MODULE,
626	.alloc_layout_hdr        = objlayout_alloc_layout_hdr,
627	.free_layout_hdr         = objlayout_free_layout_hdr,
628
629	.alloc_lseg              = objlayout_alloc_lseg,
630	.free_lseg               = objlayout_free_lseg,
631
632	.read_pagelist           = objlayout_read_pagelist,
633	.write_pagelist          = objlayout_write_pagelist,
634	.pg_read_ops             = &objio_pg_read_ops,
635	.pg_write_ops            = &objio_pg_write_ops,
636
637	.sync			 = pnfs_generic_sync,
638
639	.free_deviceid_node	 = objio_free_deviceid_node,
640
641	.encode_layoutcommit	 = objlayout_encode_layoutcommit,
642	.encode_layoutreturn     = objlayout_encode_layoutreturn,
643};
644
645MODULE_DESCRIPTION("pNFS Layout Driver for OSD2 objects");
646MODULE_AUTHOR("Benny Halevy <bhalevy@panasas.com>");
647MODULE_LICENSE("GPL");
648
649static int __init
650objlayout_init(void)
651{
652	int ret = pnfs_register_layoutdriver(&objlayout_type);
653
654	if (ret)
655		printk(KERN_INFO
656			"NFS: %s: Registering OSD pNFS Layout Driver failed: error=%d\n",
657			__func__, ret);
658	else
659		printk(KERN_INFO "NFS: %s: Registered OSD pNFS Layout Driver\n",
660			__func__);
661	return ret;
662}
663
664static void __exit
665objlayout_exit(void)
666{
667	pnfs_unregister_layoutdriver(&objlayout_type);
668	printk(KERN_INFO "NFS: %s: Unregistered OSD pNFS Layout Driver\n",
669	       __func__);
670}
671
672MODULE_ALIAS("nfs-layouttype4-2");
673
674module_init(objlayout_init);
675module_exit(objlayout_exit);
v3.1
   1/*
   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>
  41#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
  49enum { BIO_MAX_PAGES_KMALLOC =
  50		(PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),
  51};
  52
  53struct 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	return de;
 112}
 113
 114struct 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
 130	unsigned max_io_size;
 131
 132	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
 144struct objio_state;
 145typedef ssize_t (*objio_done_fn)(struct objio_state *ios);
 146
 147struct objio_state {
 148	/* Generic layer */
 149	struct objlayout_io_state ol_state;
 150
 151	struct objio_segment *layout;
 152
 153	struct kref kref;
 154	objio_done_fn done;
 155	void *private;
 156
 157	unsigned long length;
 158	unsigned numdevs; /* Actually used devs in this IO */
 159	/* A per-device variable array of size numdevs */
 160	struct _objio_per_comp {
 161		struct bio *bio;
 162		struct osd_request *or;
 163		unsigned long length;
 164		u64 offset;
 165		unsigned dev;
 166	} per_dev[];
 167};
 168
 169/* Send and wait for a get_device_info of devices in the layout,
 170   then look them up with the osd_initiator library */
 171static struct objio_dev_ent *_device_lookup(struct pnfs_layout_hdr *pnfslay,
 172				struct objio_segment *objio_seg, unsigned comp,
 173				gfp_t gfp_flags)
 174{
 175	struct pnfs_osd_deviceaddr *deviceaddr;
 176	struct nfs4_deviceid *d_id;
 177	struct objio_dev_ent *ode;
 178	struct osd_dev *od;
 179	struct osd_dev_info odi;
 
 
 180	int err;
 181
 182	d_id = &objio_seg->comps[comp].oc_object_id.oid_device_id;
 
 
 183
 184	ode = _dev_list_find(NFS_SERVER(pnfslay->plh_inode), d_id);
 185	if (ode)
 186		return ode;
 187
 188	err = objlayout_get_deviceinfo(pnfslay, d_id, &deviceaddr, gfp_flags);
 189	if (unlikely(err)) {
 190		dprintk("%s: objlayout_get_deviceinfo dev(%llx:%llx) =>%d\n",
 191			__func__, _DEVID_LO(d_id), _DEVID_HI(d_id), err);
 192		return ERR_PTR(err);
 193	}
 194
 195	odi.systemid_len = deviceaddr->oda_systemid.len;
 196	if (odi.systemid_len > sizeof(odi.systemid)) {
 
 
 197		err = -EINVAL;
 198		goto out;
 199	} else if (odi.systemid_len)
 200		memcpy(odi.systemid, deviceaddr->oda_systemid.data,
 201		       odi.systemid_len);
 202	odi.osdname_len	 = deviceaddr->oda_osdname.len;
 203	odi.osdname	 = (u8 *)deviceaddr->oda_osdname.data;
 204
 205	if (!odi.osdname_len && !odi.systemid_len) {
 206		dprintk("%s: !odi.osdname_len && !odi.systemid_len\n",
 207			__func__);
 208		err = -ENODEV;
 209		goto out;
 210	}
 211
 
 212	od = osduld_info_lookup(&odi);
 213	if (unlikely(IS_ERR(od))) {
 214		err = PTR_ERR(od);
 215		dprintk("%s: osduld_info_lookup => %d\n", __func__, err);
 
 
 
 
 
 
 
 216		goto out;
 217	}
 218
 219	ode = _dev_list_add(NFS_SERVER(pnfslay->plh_inode), d_id, od,
 220			    gfp_flags);
 
 
 
 
 
 
 
 
 
 
 
 
 221
 222out:
 223	dprintk("%s: return=%d\n", __func__, err);
 224	objlayout_put_deviceinfo(deviceaddr);
 225	return err ? ERR_PTR(err) : ode;
 226}
 227
 228static int objio_devices_lookup(struct pnfs_layout_hdr *pnfslay,
 229	struct objio_segment *objio_seg,
 230	gfp_t gfp_flags)
 231{
 232	unsigned i;
 233	int err;
 234
 235	/* lookup all devices */
 236	for (i = 0; i < objio_seg->num_comps; i++) {
 237		struct objio_dev_ent *ode;
 238
 239		ode = _device_lookup(pnfslay, objio_seg, i, gfp_flags);
 240		if (unlikely(IS_ERR(ode))) {
 241			err = PTR_ERR(ode);
 242			goto out;
 243		}
 244		objio_seg->ods[i] = ode;
 245	}
 246	err = 0;
 247
 248out:
 249	dprintk("%s: return=%d\n", __func__, err);
 250	return err;
 251}
 252
 253static int _verify_data_map(struct pnfs_osd_layout *layout)
 
 254{
 255	struct pnfs_osd_data_map *data_map = &layout->olo_map;
 256	u64 stripe_length;
 257	u32 group_width;
 258
 259/* FIXME: Only raid0 for now. if not go through MDS */
 260	if (data_map->odm_raid_algorithm != PNFS_OSD_RAID_0) {
 261		printk(KERN_ERR "Only RAID_0 for now\n");
 262		return -ENOTSUPP;
 263	}
 264	if (0 != (data_map->odm_num_comps % (data_map->odm_mirror_cnt + 1))) {
 265		printk(KERN_ERR "Data Map wrong, num_comps=%u mirrors=%u\n",
 266			  data_map->odm_num_comps, data_map->odm_mirror_cnt);
 267		return -EINVAL;
 
 
 
 
 
 
 
 
 
 268	}
 269
 270	if (data_map->odm_group_width)
 271		group_width = data_map->odm_group_width;
 272	else
 273		group_width = data_map->odm_num_comps /
 274						(data_map->odm_mirror_cnt + 1);
 275
 276	stripe_length = (u64)data_map->odm_stripe_unit * group_width;
 277	if (stripe_length >= (1ULL << 32)) {
 278		printk(KERN_ERR "Total Stripe length(0x%llx)"
 279			  " >= 32bit is not supported\n", _LLU(stripe_length));
 280		return -ENOTSUPP;
 281	}
 282
 283	if (0 != (data_map->odm_stripe_unit & ~PAGE_MASK)) {
 284		printk(KERN_ERR "Stripe Unit(0x%llx)"
 285			  " must be Multples of PAGE_SIZE(0x%lx)\n",
 286			  _LLU(data_map->odm_stripe_unit), PAGE_SIZE);
 287		return -ENOTSUPP;
 288	}
 289
 
 290	return 0;
 291}
 292
 293static void copy_single_comp(struct pnfs_osd_object_cred *cur_comp,
 294			     struct pnfs_osd_object_cred *src_comp,
 295			     struct caps_buffers *caps_p)
 296{
 297	WARN_ON(src_comp->oc_cap_key.cred_len > sizeof(caps_p->caps_key));
 298	WARN_ON(src_comp->oc_cap.cred_len > sizeof(caps_p->creds));
 299
 300	*cur_comp = *src_comp;
 301
 302	memcpy(caps_p->caps_key, src_comp->oc_cap_key.cred,
 303	       sizeof(caps_p->caps_key));
 304	cur_comp->oc_cap_key.cred = caps_p->caps_key;
 305
 306	memcpy(caps_p->creds, src_comp->oc_cap.cred,
 307	       sizeof(caps_p->creds));
 308	cur_comp->oc_cap.cred = caps_p->creds;
 309}
 310
 311int objio_alloc_lseg(struct pnfs_layout_segment **outp,
 312	struct pnfs_layout_hdr *pnfslay,
 313	struct pnfs_layout_range *range,
 314	struct xdr_stream *xdr,
 315	gfp_t gfp_flags)
 316{
 
 317	struct objio_segment *objio_seg;
 318	struct pnfs_osd_xdr_decode_layout_iter iter;
 319	struct pnfs_osd_layout layout;
 320	struct pnfs_osd_object_cred *cur_comp, src_comp;
 321	struct caps_buffers *caps_p;
 322	int err;
 323
 324	err = pnfs_osd_xdr_decode_layout_map(&layout, &iter, xdr);
 325	if (unlikely(err))
 326		return err;
 327
 328	err = _verify_data_map(&layout);
 329	if (unlikely(err))
 330		return err;
 331
 332	objio_seg = kzalloc(sizeof(*objio_seg) +
 333			    sizeof(objio_seg->ods[0]) * layout.olo_num_comps +
 334			    sizeof(*objio_seg->comps) * layout.olo_num_comps +
 335			    sizeof(struct caps_buffers) * layout.olo_num_comps,
 336			    gfp_flags);
 337	if (!objio_seg)
 338		return -ENOMEM;
 339
 340	objio_seg->comps = (void *)(objio_seg->ods + layout.olo_num_comps);
 341	cur_comp = objio_seg->comps;
 342	caps_p = (void *)(cur_comp + layout.olo_num_comps);
 343	while (pnfs_osd_xdr_decode_layout_comp(&src_comp, &iter, xdr, &err))
 344		copy_single_comp(cur_comp++, &src_comp, caps_p++);
 345	if (unlikely(err))
 346		goto err;
 347
 348	objio_seg->num_comps = layout.olo_num_comps;
 349	objio_seg->comps_index = layout.olo_comps_index;
 350	err = objio_devices_lookup(pnfslay, objio_seg, gfp_flags);
 351	if (err)
 352		goto err;
 
 
 
 
 
 
 
 
 
 
 353
 354	objio_seg->mirrors_p1 = layout.olo_map.odm_mirror_cnt + 1;
 355	objio_seg->stripe_unit = layout.olo_map.odm_stripe_unit;
 356	if (layout.olo_map.odm_group_width) {
 357		objio_seg->group_width = layout.olo_map.odm_group_width;
 358		objio_seg->group_depth = layout.olo_map.odm_group_depth;
 359		objio_seg->group_count = layout.olo_map.odm_num_comps /
 360						objio_seg->mirrors_p1 /
 361						objio_seg->group_width;
 362	} else {
 363		objio_seg->group_width = layout.olo_map.odm_num_comps /
 364						objio_seg->mirrors_p1;
 365		objio_seg->group_depth = -1;
 366		objio_seg->group_count = 1;
 367	}
 368
 369	/* Cache this calculation it will hit for every page */
 370	objio_seg->max_io_size = (BIO_MAX_PAGES_KMALLOC * PAGE_SIZE -
 371				  objio_seg->stripe_unit) *
 372				 objio_seg->group_width;
 373
 374	*outp = &objio_seg->lseg;
 375	return 0;
 376
 377err:
 378	kfree(objio_seg);
 379	dprintk("%s: Error: return %d\n", __func__, err);
 380	*outp = NULL;
 381	return err;
 382}
 383
 384void objio_free_lseg(struct pnfs_layout_segment *lseg)
 385{
 386	int i;
 387	struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
 388
 389	for (i = 0; i < objio_seg->num_comps; i++) {
 390		if (!objio_seg->ods[i])
 
 
 
 391			break;
 392		nfs4_put_deviceid_node(&objio_seg->ods[i]->id_node);
 
 393	}
 394	kfree(objio_seg);
 395}
 396
 397int objio_alloc_io_state(struct pnfs_layout_segment *lseg,
 398			 struct objlayout_io_state **outp,
 399			 gfp_t gfp_flags)
 
 
 400{
 401	struct objio_segment *objio_seg = OBJIO_LSEG(lseg);
 402	struct objio_state *ios;
 403	const unsigned first_size = sizeof(*ios) +
 404				objio_seg->num_comps * sizeof(ios->per_dev[0]);
 405	const unsigned sec_size = objio_seg->num_comps *
 406						sizeof(ios->ol_state.ioerrs[0]);
 
 407
 408	ios = kzalloc(first_size + sec_size, gfp_flags);
 409	if (unlikely(!ios))
 410		return -ENOMEM;
 411
 412	ios->layout = objio_seg;
 413	ios->ol_state.ioerrs = ((void *)ios) + first_size;
 414	ios->ol_state.num_comps = objio_seg->num_comps;
 
 
 
 
 
 
 415
 416	*outp = &ios->ol_state;
 
 
 
 
 
 
 
 417	return 0;
 418}
 419
 420void objio_free_io_state(struct objlayout_io_state *ol_state)
 421{
 422	struct objio_state *ios = container_of(ol_state, struct objio_state,
 423					       ol_state);
 424
 425	kfree(ios);
 
 426}
 427
 428enum pnfs_osd_errno osd_pri_2_pnfs_err(enum osd_err_priority oep)
 429{
 430	switch (oep) {
 431	case OSD_ERR_PRI_NO_ERROR:
 432		return (enum pnfs_osd_errno)0;
 433
 434	case OSD_ERR_PRI_CLEAR_PAGES:
 435		BUG_ON(1);
 436		return 0;
 437
 438	case OSD_ERR_PRI_RESOURCE:
 439		return PNFS_OSD_ERR_RESOURCE;
 440	case OSD_ERR_PRI_BAD_CRED:
 441		return PNFS_OSD_ERR_BAD_CRED;
 442	case OSD_ERR_PRI_NO_ACCESS:
 443		return PNFS_OSD_ERR_NO_ACCESS;
 444	case OSD_ERR_PRI_UNREACHABLE:
 445		return PNFS_OSD_ERR_UNREACHABLE;
 446	case OSD_ERR_PRI_NOT_FOUND:
 447		return PNFS_OSD_ERR_NOT_FOUND;
 448	case OSD_ERR_PRI_NO_SPACE:
 449		return PNFS_OSD_ERR_NO_SPACE;
 450	default:
 451		WARN_ON(1);
 452		/* fallthrough */
 453	case OSD_ERR_PRI_EIO:
 454		return PNFS_OSD_ERR_EIO;
 455	}
 456}
 457
 458static void _clear_bio(struct bio *bio)
 459{
 460	struct bio_vec *bv;
 461	unsigned i;
 462
 463	__bio_for_each_segment(bv, bio, i, 0) {
 464		unsigned this_count = bv->bv_len;
 465
 466		if (likely(PAGE_SIZE == this_count))
 467			clear_highpage(bv->bv_page);
 468		else
 469			zero_user(bv->bv_page, bv->bv_offset, this_count);
 470	}
 471}
 472
 473static int _io_check(struct objio_state *ios, bool is_write)
 474{
 475	enum osd_err_priority oep = OSD_ERR_PRI_NO_ERROR;
 476	int lin_ret = 0;
 477	int i;
 478
 479	for (i = 0; i <  ios->numdevs; i++) {
 480		struct osd_sense_info osi;
 481		struct osd_request *or = ios->per_dev[i].or;
 482		int ret;
 483
 484		if (!or)
 485			continue;
 486
 487		ret = osd_req_decode_sense(or, &osi);
 488		if (likely(!ret))
 489			continue;
 490
 491		if (OSD_ERR_PRI_CLEAR_PAGES == osi.osd_err_pri) {
 492			/* start read offset passed endof file */
 493			BUG_ON(is_write);
 494			_clear_bio(ios->per_dev[i].bio);
 495			dprintk("%s: start read offset passed end of file "
 496				"offset=0x%llx, length=0x%lx\n", __func__,
 497				_LLU(ios->per_dev[i].offset),
 498				ios->per_dev[i].length);
 499
 500			continue; /* we recovered */
 501		}
 502		objlayout_io_set_result(&ios->ol_state, i,
 503					&ios->layout->comps[i].oc_object_id,
 504					osd_pri_2_pnfs_err(osi.osd_err_pri),
 505					ios->per_dev[i].offset,
 506					ios->per_dev[i].length,
 507					is_write);
 508
 509		if (osi.osd_err_pri >= oep) {
 510			oep = osi.osd_err_pri;
 511			lin_ret = ret;
 512		}
 513	}
 514
 515	return lin_ret;
 516}
 517
 518/*
 519 * Common IO state helpers.
 520 */
 521static void _io_free(struct objio_state *ios)
 522{
 523	unsigned i;
 524
 525	for (i = 0; i < ios->numdevs; i++) {
 526		struct _objio_per_comp *per_dev = &ios->per_dev[i];
 527
 528		if (per_dev->or) {
 529			osd_end_request(per_dev->or);
 530			per_dev->or = NULL;
 531		}
 532
 533		if (per_dev->bio) {
 534			bio_put(per_dev->bio);
 535			per_dev->bio = NULL;
 536		}
 537	}
 538}
 539
 540struct osd_dev *_io_od(struct objio_state *ios, unsigned dev)
 541{
 542	unsigned min_dev = ios->layout->comps_index;
 543	unsigned max_dev = min_dev + ios->layout->num_comps;
 544
 545	BUG_ON(dev < min_dev || max_dev <= dev);
 546	return ios->layout->ods[dev - min_dev]->od;
 547}
 548
 549struct _striping_info {
 550	u64 obj_offset;
 551	u64 group_length;
 552	unsigned dev;
 553	unsigned unit_off;
 554};
 555
 556static void _calc_stripe_info(struct objio_state *ios, u64 file_offset,
 557			      struct _striping_info *si)
 558{
 559	u32	stripe_unit = ios->layout->stripe_unit;
 560	u32	group_width = ios->layout->group_width;
 561	u64	group_depth = ios->layout->group_depth;
 562	u32	U = stripe_unit * group_width;
 563
 564	u64	T = U * group_depth;
 565	u64	S = T * ios->layout->group_count;
 566	u64	M = div64_u64(file_offset, S);
 567
 568	/*
 569	G = (L - (M * S)) / T
 570	H = (L - (M * S)) % T
 571	*/
 572	u64	LmodU = file_offset - M * S;
 573	u32	G = div64_u64(LmodU, T);
 574	u64	H = LmodU - G * T;
 575
 576	u32	N = div_u64(H, U);
 577
 578	div_u64_rem(file_offset, stripe_unit, &si->unit_off);
 579	si->obj_offset = si->unit_off + (N * stripe_unit) +
 580				  (M * group_depth * stripe_unit);
 581
 582	/* "H - (N * U)" is just "H % U" so it's bound to u32 */
 583	si->dev = (u32)(H - (N * U)) / stripe_unit + G * group_width;
 584	si->dev *= ios->layout->mirrors_p1;
 585
 586	si->group_length = T - H;
 587}
 588
 589static int _add_stripe_unit(struct objio_state *ios,  unsigned *cur_pg,
 590		unsigned pgbase, struct _objio_per_comp *per_dev, int len,
 591		gfp_t gfp_flags)
 592{
 593	unsigned pg = *cur_pg;
 594	int cur_len = len;
 595	struct request_queue *q =
 596			osd_request_queue(_io_od(ios, per_dev->dev));
 597
 598	if (per_dev->bio == NULL) {
 599		unsigned pages_in_stripe = ios->layout->group_width *
 600				      (ios->layout->stripe_unit / PAGE_SIZE);
 601		unsigned bio_size = (ios->ol_state.nr_pages + pages_in_stripe) /
 602				    ios->layout->group_width;
 603
 604		if (BIO_MAX_PAGES_KMALLOC < bio_size)
 605			bio_size = BIO_MAX_PAGES_KMALLOC;
 606
 607		per_dev->bio = bio_kmalloc(gfp_flags, bio_size);
 608		if (unlikely(!per_dev->bio)) {
 609			dprintk("Faild to allocate BIO size=%u\n", bio_size);
 610			return -ENOMEM;
 611		}
 612	}
 613
 614	while (cur_len > 0) {
 615		unsigned pglen = min_t(unsigned, PAGE_SIZE - pgbase, cur_len);
 616		unsigned added_len;
 617
 618		BUG_ON(ios->ol_state.nr_pages <= pg);
 619		cur_len -= pglen;
 620
 621		added_len = bio_add_pc_page(q, per_dev->bio,
 622					ios->ol_state.pages[pg], pglen, pgbase);
 623		if (unlikely(pglen != added_len))
 624			return -ENOMEM;
 625		pgbase = 0;
 626		++pg;
 627	}
 628	BUG_ON(cur_len);
 629
 630	per_dev->length += len;
 631	*cur_pg = pg;
 632	return 0;
 633}
 634
 635static int _prepare_one_group(struct objio_state *ios, u64 length,
 636			      struct _striping_info *si, unsigned *last_pg,
 637			      gfp_t gfp_flags)
 638{
 639	unsigned stripe_unit = ios->layout->stripe_unit;
 640	unsigned mirrors_p1 = ios->layout->mirrors_p1;
 641	unsigned devs_in_group = ios->layout->group_width * mirrors_p1;
 642	unsigned dev = si->dev;
 643	unsigned first_dev = dev - (dev % devs_in_group);
 644	unsigned max_comp = ios->numdevs ? ios->numdevs - mirrors_p1 : 0;
 645	unsigned cur_pg = *last_pg;
 646	int ret = 0;
 647
 648	while (length) {
 649		struct _objio_per_comp *per_dev = &ios->per_dev[dev - first_dev];
 650		unsigned cur_len, page_off = 0;
 651
 652		if (!per_dev->length) {
 653			per_dev->dev = dev;
 654			if (dev < si->dev) {
 655				per_dev->offset = si->obj_offset + stripe_unit -
 656								   si->unit_off;
 657				cur_len = stripe_unit;
 658			} else if (dev == si->dev) {
 659				per_dev->offset = si->obj_offset;
 660				cur_len = stripe_unit - si->unit_off;
 661				page_off = si->unit_off & ~PAGE_MASK;
 662				BUG_ON(page_off &&
 663				      (page_off != ios->ol_state.pgbase));
 664			} else { /* dev > si->dev */
 665				per_dev->offset = si->obj_offset - si->unit_off;
 666				cur_len = stripe_unit;
 667			}
 668
 669			if (max_comp < dev - first_dev)
 670				max_comp = dev - first_dev;
 671		} else {
 672			cur_len = stripe_unit;
 673		}
 674		if (cur_len >= length)
 675			cur_len = length;
 676
 677		ret = _add_stripe_unit(ios, &cur_pg, page_off , per_dev,
 678				       cur_len, gfp_flags);
 679		if (unlikely(ret))
 680			goto out;
 681
 682		dev += mirrors_p1;
 683		dev = (dev % devs_in_group) + first_dev;
 684
 685		length -= cur_len;
 686		ios->length += cur_len;
 687	}
 688out:
 689	ios->numdevs = max_comp + mirrors_p1;
 690	*last_pg = cur_pg;
 691	return ret;
 692}
 693
 694static int _io_rw_pagelist(struct objio_state *ios, gfp_t gfp_flags)
 695{
 696	u64 length = ios->ol_state.count;
 697	u64 offset = ios->ol_state.offset;
 698	struct _striping_info si;
 699	unsigned last_pg = 0;
 700	int ret = 0;
 701
 702	while (length) {
 703		_calc_stripe_info(ios, offset, &si);
 704
 705		if (length < si.group_length)
 706			si.group_length = length;
 707
 708		ret = _prepare_one_group(ios, si.group_length, &si, &last_pg, gfp_flags);
 709		if (unlikely(ret))
 710			goto out;
 711
 712		offset += si.group_length;
 713		length -= si.group_length;
 714	}
 715
 716out:
 717	if (!ios->length)
 718		return ret;
 719
 720	return 0;
 721}
 722
 723static ssize_t _sync_done(struct objio_state *ios)
 724{
 725	struct completion *waiting = ios->private;
 726
 727	complete(waiting);
 728	return 0;
 729}
 730
 731static void _last_io(struct kref *kref)
 732{
 733	struct objio_state *ios = container_of(kref, struct objio_state, kref);
 734
 735	ios->done(ios);
 736}
 737
 738static void _done_io(struct osd_request *or, void *p)
 739{
 740	struct objio_state *ios = p;
 741
 742	kref_put(&ios->kref, _last_io);
 743}
 744
 745static ssize_t _io_exec(struct objio_state *ios)
 746{
 747	DECLARE_COMPLETION_ONSTACK(wait);
 748	ssize_t status = 0; /* sync status */
 749	unsigned i;
 750	objio_done_fn saved_done_fn = ios->done;
 751	bool sync = ios->ol_state.sync;
 752
 753	if (sync) {
 754		ios->done = _sync_done;
 755		ios->private = &wait;
 756	}
 757
 758	kref_init(&ios->kref);
 759
 760	for (i = 0; i < ios->numdevs; i++) {
 761		struct osd_request *or = ios->per_dev[i].or;
 762
 763		if (!or)
 764			continue;
 765
 766		kref_get(&ios->kref);
 767		osd_execute_request_async(or, _done_io, ios);
 768	}
 769
 770	kref_put(&ios->kref, _last_io);
 771
 772	if (sync) {
 773		wait_for_completion(&wait);
 774		status = saved_done_fn(ios);
 775	}
 776
 777	return status;
 778}
 779
 780/*
 781 * read
 782 */
 783static ssize_t _read_done(struct objio_state *ios)
 784{
 
 785	ssize_t status;
 786	int ret = _io_check(ios, false);
 787
 788	_io_free(ios);
 789
 790	if (likely(!ret))
 791		status = ios->length;
 792	else
 793		status = ret;
 794
 795	objlayout_read_done(&ios->ol_state, status, ios->ol_state.sync);
 796	return status;
 797}
 798
 799static int _read_mirrors(struct objio_state *ios, unsigned cur_comp)
 800{
 801	struct osd_request *or = NULL;
 802	struct _objio_per_comp *per_dev = &ios->per_dev[cur_comp];
 803	unsigned dev = per_dev->dev;
 804	struct pnfs_osd_object_cred *cred =
 805			&ios->layout->comps[cur_comp];
 806	struct osd_obj_id obj = {
 807		.partition = cred->oc_object_id.oid_partition_id,
 808		.id = cred->oc_object_id.oid_object_id,
 809	};
 810	int ret;
 811
 812	or = osd_start_request(_io_od(ios, dev), GFP_KERNEL);
 813	if (unlikely(!or)) {
 814		ret = -ENOMEM;
 815		goto err;
 816	}
 817	per_dev->or = or;
 818
 819	osd_req_read(or, &obj, per_dev->offset, per_dev->bio, per_dev->length);
 820
 821	ret = osd_finalize_request(or, 0, cred->oc_cap.cred, NULL);
 822	if (ret) {
 823		dprintk("%s: Faild to osd_finalize_request() => %d\n",
 824			__func__, ret);
 825		goto err;
 826	}
 827
 828	dprintk("%s:[%d] dev=%d obj=0x%llx start=0x%llx length=0x%lx\n",
 829		__func__, cur_comp, dev, obj.id, _LLU(per_dev->offset),
 830		per_dev->length);
 831
 832err:
 833	return ret;
 834}
 835
 836static ssize_t _read_exec(struct objio_state *ios)
 837{
 838	unsigned i;
 839	int ret;
 840
 841	for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
 842		if (!ios->per_dev[i].length)
 843			continue;
 844		ret = _read_mirrors(ios, i);
 845		if (unlikely(ret))
 846			goto err;
 847	}
 848
 849	ios->done = _read_done;
 850	return _io_exec(ios); /* In sync mode exec returns the io status */
 851
 852err:
 853	_io_free(ios);
 854	return ret;
 855}
 856
 857ssize_t objio_read_pagelist(struct objlayout_io_state *ol_state)
 858{
 859	struct objio_state *ios = container_of(ol_state, struct objio_state,
 860					       ol_state);
 861	int ret;
 862
 863	ret = _io_rw_pagelist(ios, GFP_KERNEL);
 864	if (unlikely(ret))
 865		return ret;
 866
 867	return _read_exec(ios);
 
 
 
 
 
 
 868}
 869
 870/*
 871 * write
 872 */
 873static ssize_t _write_done(struct objio_state *ios)
 874{
 
 875	ssize_t status;
 876	int ret = _io_check(ios, true);
 877
 878	_io_free(ios);
 879
 880	if (likely(!ret)) {
 881		/* FIXME: should be based on the OSD's persistence model
 882		 * See OSD2r05 Section 4.13 Data persistence model */
 883		ios->ol_state.committed = NFS_FILE_SYNC;
 884		status = ios->length;
 885	} else {
 886		status = ret;
 887	}
 888
 889	objlayout_write_done(&ios->ol_state, status, ios->ol_state.sync);
 890	return status;
 891}
 892
 893static int _write_mirrors(struct objio_state *ios, unsigned cur_comp)
 894{
 895	struct _objio_per_comp *master_dev = &ios->per_dev[cur_comp];
 896	unsigned dev = ios->per_dev[cur_comp].dev;
 897	unsigned last_comp = cur_comp + ios->layout->mirrors_p1;
 898	int ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 899
 900	for (; cur_comp < last_comp; ++cur_comp, ++dev) {
 901		struct osd_request *or = NULL;
 902		struct pnfs_osd_object_cred *cred =
 903					&ios->layout->comps[cur_comp];
 904		struct osd_obj_id obj = {
 905			.partition = cred->oc_object_id.oid_partition_id,
 906			.id = cred->oc_object_id.oid_object_id,
 907		};
 908		struct _objio_per_comp *per_dev = &ios->per_dev[cur_comp];
 909		struct bio *bio;
 910
 911		or = osd_start_request(_io_od(ios, dev), GFP_NOFS);
 912		if (unlikely(!or)) {
 913			ret = -ENOMEM;
 914			goto err;
 915		}
 916		per_dev->or = or;
 917
 918		if (per_dev != master_dev) {
 919			bio = bio_kmalloc(GFP_NOFS,
 920					  master_dev->bio->bi_max_vecs);
 921			if (unlikely(!bio)) {
 922				dprintk("Faild to allocate BIO size=%u\n",
 923					master_dev->bio->bi_max_vecs);
 924				ret = -ENOMEM;
 925				goto err;
 926			}
 927
 928			__bio_clone(bio, master_dev->bio);
 929			bio->bi_bdev = NULL;
 930			bio->bi_next = NULL;
 931			per_dev->bio = bio;
 932			per_dev->dev = dev;
 933			per_dev->length = master_dev->length;
 934			per_dev->offset =  master_dev->offset;
 935		} else {
 936			bio = master_dev->bio;
 937			bio->bi_rw |= REQ_WRITE;
 938		}
 939
 940		osd_req_write(or, &obj, per_dev->offset, bio, per_dev->length);
 
 941
 942		ret = osd_finalize_request(or, 0, cred->oc_cap.cred, NULL);
 943		if (ret) {
 944			dprintk("%s: Faild to osd_finalize_request() => %d\n",
 945				__func__, ret);
 946			goto err;
 947		}
 948
 949		dprintk("%s:[%d] dev=%d obj=0x%llx start=0x%llx length=0x%lx\n",
 950			__func__, cur_comp, dev, obj.id, _LLU(per_dev->offset),
 951			per_dev->length);
 
 
 
 952	}
 953
 954err:
 955	return ret;
 
 
 956}
 957
 958static ssize_t _write_exec(struct objio_state *ios)
 
 
 
 
 
 959{
 960	unsigned i;
 961	int ret;
 
 
 962
 963	for (i = 0; i < ios->numdevs; i += ios->layout->mirrors_p1) {
 964		if (!ios->per_dev[i].length)
 965			continue;
 966		ret = _write_mirrors(ios, i);
 967		if (unlikely(ret))
 968			goto err;
 969	}
 970
 971	ios->done = _write_done;
 972	return _io_exec(ios); /* In sync mode exec returns the io->status */
 973
 974err:
 975	_io_free(ios);
 976	return ret;
 
 
 
 
 
 977}
 978
 979ssize_t objio_write_pagelist(struct objlayout_io_state *ol_state, bool stable)
 
 980{
 981	struct objio_state *ios = container_of(ol_state, struct objio_state,
 982					       ol_state);
 983	int ret;
 
 
 
 
 
 984
 985	/* TODO: ios->stable = stable; */
 986	ret = _io_rw_pagelist(ios, GFP_NOFS);
 987	if (unlikely(ret))
 988		return ret;
 989
 990	return _write_exec(ios);
 
 991}
 992
 993static bool objio_pg_test(struct nfs_pageio_descriptor *pgio,
 994			  struct nfs_page *prev, struct nfs_page *req)
 995{
 996	if (!pnfs_generic_pg_test(pgio, prev, req))
 997		return false;
 
 
 
 
 
 998
 999	return pgio->pg_count + req->wb_bytes <=
1000			OBJIO_LSEG(pgio->pg_lseg)->max_io_size;
 
 
 
 
 
 
 
 
 
 
 
1001}
1002
1003static const struct nfs_pageio_ops objio_pg_read_ops = {
1004	.pg_init = pnfs_generic_pg_init_read,
1005	.pg_test = objio_pg_test,
1006	.pg_doio = pnfs_generic_pg_readpages,
 
1007};
1008
1009static const struct nfs_pageio_ops objio_pg_write_ops = {
1010	.pg_init = pnfs_generic_pg_init_write,
1011	.pg_test = objio_pg_test,
1012	.pg_doio = pnfs_generic_pg_writepages,
 
1013};
1014
1015static struct pnfs_layoutdriver_type objlayout_type = {
1016	.id = LAYOUT_OSD2_OBJECTS,
1017	.name = "LAYOUT_OSD2_OBJECTS",
1018	.flags                   = PNFS_LAYOUTRET_ON_SETATTR,
 
1019
 
 
1020	.alloc_layout_hdr        = objlayout_alloc_layout_hdr,
1021	.free_layout_hdr         = objlayout_free_layout_hdr,
1022
1023	.alloc_lseg              = objlayout_alloc_lseg,
1024	.free_lseg               = objlayout_free_lseg,
1025
1026	.read_pagelist           = objlayout_read_pagelist,
1027	.write_pagelist          = objlayout_write_pagelist,
1028	.pg_read_ops             = &objio_pg_read_ops,
1029	.pg_write_ops            = &objio_pg_write_ops,
1030
 
 
1031	.free_deviceid_node	 = objio_free_deviceid_node,
1032
1033	.encode_layoutcommit	 = objlayout_encode_layoutcommit,
1034	.encode_layoutreturn     = objlayout_encode_layoutreturn,
1035};
1036
1037MODULE_DESCRIPTION("pNFS Layout Driver for OSD2 objects");
1038MODULE_AUTHOR("Benny Halevy <bhalevy@panasas.com>");
1039MODULE_LICENSE("GPL");
1040
1041static int __init
1042objlayout_init(void)
1043{
1044	int ret = pnfs_register_layoutdriver(&objlayout_type);
1045
1046	if (ret)
1047		printk(KERN_INFO
1048			"%s: Registering OSD pNFS Layout Driver failed: error=%d\n",
1049			__func__, ret);
1050	else
1051		printk(KERN_INFO "%s: Registered OSD pNFS Layout Driver\n",
1052			__func__);
1053	return ret;
1054}
1055
1056static void __exit
1057objlayout_exit(void)
1058{
1059	pnfs_unregister_layoutdriver(&objlayout_type);
1060	printk(KERN_INFO "%s: Unregistered OSD pNFS Layout Driver\n",
1061	       __func__);
1062}
1063
1064MODULE_ALIAS("nfs-layouttype4-2");
1065
1066module_init(objlayout_init);
1067module_exit(objlayout_exit);