Linux Audio

Check our new training course

Loading...
v3.15
  1/*
  2 *  pNFS Objects layout driver high level definitions
  3 *
  4 *  Copyright (C) 2007 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/kmod.h>
 41#include <linux/moduleparam.h>
 42#include <linux/ratelimit.h>
 43#include <scsi/osd_initiator.h>
 44#include "objlayout.h"
 45
 46#define NFSDBG_FACILITY         NFSDBG_PNFS_LD
 47/*
 48 * Create a objlayout layout structure for the given inode and return it.
 49 */
 50struct pnfs_layout_hdr *
 51objlayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
 52{
 53	struct objlayout *objlay;
 54
 55	objlay = kzalloc(sizeof(struct objlayout), gfp_flags);
 56	if (objlay) {
 57		spin_lock_init(&objlay->lock);
 58		INIT_LIST_HEAD(&objlay->err_list);
 59	}
 60	dprintk("%s: Return %p\n", __func__, objlay);
 61	return &objlay->pnfs_layout;
 62}
 63
 64/*
 65 * Free an objlayout layout structure
 66 */
 67void
 68objlayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
 69{
 70	struct objlayout *objlay = OBJLAYOUT(lo);
 71
 72	dprintk("%s: objlay %p\n", __func__, objlay);
 73
 74	WARN_ON(!list_empty(&objlay->err_list));
 75	kfree(objlay);
 76}
 77
 78/*
 79 * Unmarshall layout and store it in pnfslay.
 80 */
 81struct pnfs_layout_segment *
 82objlayout_alloc_lseg(struct pnfs_layout_hdr *pnfslay,
 83		     struct nfs4_layoutget_res *lgr,
 84		     gfp_t gfp_flags)
 85{
 86	int status = -ENOMEM;
 87	struct xdr_stream stream;
 88	struct xdr_buf buf = {
 89		.pages =  lgr->layoutp->pages,
 90		.page_len =  lgr->layoutp->len,
 91		.buflen =  lgr->layoutp->len,
 92		.len = lgr->layoutp->len,
 93	};
 94	struct page *scratch;
 95	struct pnfs_layout_segment *lseg;
 96
 97	dprintk("%s: Begin pnfslay %p\n", __func__, pnfslay);
 98
 99	scratch = alloc_page(gfp_flags);
100	if (!scratch)
101		goto err_nofree;
102
103	xdr_init_decode(&stream, &buf, NULL);
104	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
105
106	status = objio_alloc_lseg(&lseg, pnfslay, &lgr->range, &stream, gfp_flags);
107	if (unlikely(status)) {
108		dprintk("%s: objio_alloc_lseg Return err %d\n", __func__,
109			status);
110		goto err;
111	}
112
113	__free_page(scratch);
114
115	dprintk("%s: Return %p\n", __func__, lseg);
116	return lseg;
117
118err:
119	__free_page(scratch);
120err_nofree:
121	dprintk("%s: Err Return=>%d\n", __func__, status);
122	return ERR_PTR(status);
123}
124
125/*
126 * Free a layout segement
127 */
128void
129objlayout_free_lseg(struct pnfs_layout_segment *lseg)
130{
131	dprintk("%s: freeing layout segment %p\n", __func__, lseg);
132
133	if (unlikely(!lseg))
134		return;
135
136	objio_free_lseg(lseg);
137}
138
139/*
140 * I/O Operations
141 */
142static inline u64
143end_offset(u64 start, u64 len)
144{
145	u64 end;
146
147	end = start + len;
148	return end >= start ? end : NFS4_MAX_UINT64;
149}
150
151static void _fix_verify_io_params(struct pnfs_layout_segment *lseg,
152			   struct page ***p_pages, unsigned *p_pgbase,
153			   u64 offset, unsigned long count)
154{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155	u64 lseg_end_offset;
156
 
 
 
 
157	BUG_ON(offset < lseg->pls_range.offset);
158	lseg_end_offset = end_offset(lseg->pls_range.offset,
159				     lseg->pls_range.length);
160	BUG_ON(offset >= lseg_end_offset);
161	WARN_ON(offset + count > lseg_end_offset);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
163	if (*p_pgbase > PAGE_SIZE) {
164		dprintk("%s: pgbase(0x%x) > PAGE_SIZE\n", __func__, *p_pgbase);
165		*p_pages += *p_pgbase >> PAGE_SHIFT;
166		*p_pgbase &= ~PAGE_MASK;
167	}
 
 
 
 
 
 
168}
169
170/*
171 * I/O done common code
172 */
173static void
174objlayout_iodone(struct objlayout_io_res *oir)
175{
176	if (likely(oir->status >= 0)) {
177		objio_free_result(oir);
 
 
178	} else {
179		struct objlayout *objlay = oir->objlay;
180
181		spin_lock(&objlay->lock);
182		objlay->delta_space_valid = OBJ_DSU_INVALID;
183		list_add(&objlay->err_list, &oir->err_list);
184		spin_unlock(&objlay->lock);
185	}
186}
187
188/*
189 * objlayout_io_set_result - Set an osd_error code on a specific osd comp.
190 *
191 * The @index component IO failed (error returned from target). Register
192 * the error for later reporting at layout-return.
193 */
194void
195objlayout_io_set_result(struct objlayout_io_res *oir, unsigned index,
196			struct pnfs_osd_objid *pooid, int osd_error,
197			u64 offset, u64 length, bool is_write)
198{
199	struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[index];
200
201	BUG_ON(index >= oir->num_comps);
202	if (osd_error) {
203		ioerr->oer_component = *pooid;
204		ioerr->oer_comp_offset = offset;
205		ioerr->oer_comp_length = length;
206		ioerr->oer_iswrite = is_write;
207		ioerr->oer_errno = osd_error;
208
209		dprintk("%s: err[%d]: errno=%d is_write=%d dev(%llx:%llx) "
210			"par=0x%llx obj=0x%llx offset=0x%llx length=0x%llx\n",
211			__func__, index, ioerr->oer_errno,
212			ioerr->oer_iswrite,
213			_DEVID_LO(&ioerr->oer_component.oid_device_id),
214			_DEVID_HI(&ioerr->oer_component.oid_device_id),
215			ioerr->oer_component.oid_partition_id,
216			ioerr->oer_component.oid_object_id,
217			ioerr->oer_comp_offset,
218			ioerr->oer_comp_length);
219	} else {
220		/* User need not call if no error is reported */
221		ioerr->oer_errno = 0;
222	}
223}
224
225/* Function scheduled on rpc workqueue to call ->nfs_readlist_complete().
226 * This is because the osd completion is called with ints-off from
227 * the block layer
228 */
229static void _rpc_read_complete(struct work_struct *work)
230{
231	struct rpc_task *task;
232	struct nfs_read_data *rdata;
233
234	dprintk("%s enter\n", __func__);
235	task = container_of(work, struct rpc_task, u.tk_work);
236	rdata = container_of(task, struct nfs_read_data, task);
237
238	pnfs_ld_read_done(rdata);
239}
240
241void
242objlayout_read_done(struct objlayout_io_res *oir, ssize_t status, bool sync)
243{
244	struct nfs_read_data *rdata = oir->rpcdata;
 
245
246	oir->status = rdata->task.tk_status = status;
247	if (status >= 0)
 
 
 
248		rdata->res.count = status;
249	else
250		rdata->header->pnfs_error = status;
251	objlayout_iodone(oir);
252	/* must not use oir after this point */
253
254	dprintk("%s: Return status=%zd eof=%d sync=%d\n", __func__,
255		status, rdata->res.eof, sync);
256
257	if (sync)
258		pnfs_ld_read_done(rdata);
259	else {
260		INIT_WORK(&rdata->task.u.tk_work, _rpc_read_complete);
261		schedule_work(&rdata->task.u.tk_work);
262	}
263}
264
265/*
266 * Perform sync or async reads.
267 */
268enum pnfs_try_status
269objlayout_read_pagelist(struct nfs_read_data *rdata)
270{
271	struct nfs_pgio_header *hdr = rdata->header;
272	struct inode *inode = hdr->inode;
273	loff_t offset = rdata->args.offset;
274	size_t count = rdata->args.count;
275	int err;
 
276	loff_t eof;
277
278	eof = i_size_read(inode);
 
 
 
279	if (unlikely(offset + count > eof)) {
280		if (offset >= eof) {
281			err = 0;
282			rdata->res.count = 0;
283			rdata->res.eof = 1;
284			/*FIXME: do we need to call pnfs_ld_read_done() */
285			goto out;
286		}
287		count = eof - offset;
288	}
289
290	rdata->res.eof = (offset + count) >= eof;
291	_fix_verify_io_params(hdr->lseg, &rdata->args.pages,
292			      &rdata->args.pgbase,
293			      rdata->args.offset, rdata->args.count);
 
 
 
 
 
294
295	dprintk("%s: inode(%lx) offset 0x%llx count 0x%Zx eof=%d\n",
296		__func__, inode->i_ino, offset, count, rdata->res.eof);
297
298	err = objio_read_pagelist(rdata);
299 out:
300	if (unlikely(err)) {
301		hdr->pnfs_error = err;
302		dprintk("%s: Returned Error %d\n", __func__, err);
303		return PNFS_NOT_ATTEMPTED;
304	}
305	return PNFS_ATTEMPTED;
306}
307
308/* Function scheduled on rpc workqueue to call ->nfs_writelist_complete().
309 * This is because the osd completion is called with ints-off from
310 * the block layer
311 */
312static void _rpc_write_complete(struct work_struct *work)
313{
314	struct rpc_task *task;
315	struct nfs_write_data *wdata;
316
317	dprintk("%s enter\n", __func__);
318	task = container_of(work, struct rpc_task, u.tk_work);
319	wdata = container_of(task, struct nfs_write_data, task);
320
321	pnfs_ld_write_done(wdata);
322}
323
324void
325objlayout_write_done(struct objlayout_io_res *oir, ssize_t status, bool sync)
 
326{
327	struct nfs_write_data *wdata = oir->rpcdata;
328
329	oir->status = wdata->task.tk_status = status;
 
 
 
330	if (status >= 0) {
331		wdata->res.count = status;
332		wdata->verf.committed = oir->committed;
333	} else {
334		wdata->header->pnfs_error = status;
335	}
336	objlayout_iodone(oir);
337	/* must not use oir after this point */
338
339	dprintk("%s: Return status %zd committed %d sync=%d\n", __func__,
340		status, wdata->verf.committed, sync);
341
342	if (sync)
343		pnfs_ld_write_done(wdata);
344	else {
345		INIT_WORK(&wdata->task.u.tk_work, _rpc_write_complete);
346		schedule_work(&wdata->task.u.tk_work);
347	}
348}
349
350/*
351 * Perform sync or async writes.
352 */
353enum pnfs_try_status
354objlayout_write_pagelist(struct nfs_write_data *wdata,
355			 int how)
356{
357	struct nfs_pgio_header *hdr = wdata->header;
358	int err;
359
360	_fix_verify_io_params(hdr->lseg, &wdata->args.pages,
361			      &wdata->args.pgbase,
362			      wdata->args.offset, wdata->args.count);
363
364	err = objio_write_pagelist(wdata, how);
365	if (unlikely(err)) {
366		hdr->pnfs_error = err;
367		dprintk("%s: Returned Error %d\n", __func__, err);
368		return PNFS_NOT_ATTEMPTED;
 
 
 
 
369	}
 
 
 
 
 
 
 
370	return PNFS_ATTEMPTED;
371}
372
373void
374objlayout_encode_layoutcommit(struct pnfs_layout_hdr *pnfslay,
375			      struct xdr_stream *xdr,
376			      const struct nfs4_layoutcommit_args *args)
377{
378	struct objlayout *objlay = OBJLAYOUT(pnfslay);
379	struct pnfs_osd_layoutupdate lou;
380	__be32 *start;
381
382	dprintk("%s: Begin\n", __func__);
383
384	spin_lock(&objlay->lock);
385	lou.dsu_valid = (objlay->delta_space_valid == OBJ_DSU_VALID);
386	lou.dsu_delta = objlay->delta_space_used;
387	objlay->delta_space_used = 0;
388	objlay->delta_space_valid = OBJ_DSU_INIT;
389	lou.olu_ioerr_flag = !list_empty(&objlay->err_list);
390	spin_unlock(&objlay->lock);
391
392	start = xdr_reserve_space(xdr, 4);
393
394	BUG_ON(pnfs_osd_xdr_encode_layoutupdate(xdr, &lou));
395
396	*start = cpu_to_be32((xdr->p - start - 1) * 4);
397
398	dprintk("%s: Return delta_space_used %lld err %d\n", __func__,
399		lou.dsu_delta, lou.olu_ioerr_flag);
400}
401
402static int
403err_prio(u32 oer_errno)
404{
405	switch (oer_errno) {
406	case 0:
407		return 0;
408
409	case PNFS_OSD_ERR_RESOURCE:
410		return OSD_ERR_PRI_RESOURCE;
411	case PNFS_OSD_ERR_BAD_CRED:
412		return OSD_ERR_PRI_BAD_CRED;
413	case PNFS_OSD_ERR_NO_ACCESS:
414		return OSD_ERR_PRI_NO_ACCESS;
415	case PNFS_OSD_ERR_UNREACHABLE:
416		return OSD_ERR_PRI_UNREACHABLE;
417	case PNFS_OSD_ERR_NOT_FOUND:
418		return OSD_ERR_PRI_NOT_FOUND;
419	case PNFS_OSD_ERR_NO_SPACE:
420		return OSD_ERR_PRI_NO_SPACE;
421	default:
422		WARN_ON(1);
423		/* fallthrough */
424	case PNFS_OSD_ERR_EIO:
425		return OSD_ERR_PRI_EIO;
426	}
427}
428
429static void
430merge_ioerr(struct pnfs_osd_ioerr *dest_err,
431	    const struct pnfs_osd_ioerr *src_err)
432{
433	u64 dest_end, src_end;
434
435	if (!dest_err->oer_errno) {
436		*dest_err = *src_err;
437		/* accumulated device must be blank */
438		memset(&dest_err->oer_component.oid_device_id, 0,
439			sizeof(dest_err->oer_component.oid_device_id));
440
441		return;
442	}
443
444	if (dest_err->oer_component.oid_partition_id !=
445				src_err->oer_component.oid_partition_id)
446		dest_err->oer_component.oid_partition_id = 0;
447
448	if (dest_err->oer_component.oid_object_id !=
449				src_err->oer_component.oid_object_id)
450		dest_err->oer_component.oid_object_id = 0;
451
452	if (dest_err->oer_comp_offset > src_err->oer_comp_offset)
453		dest_err->oer_comp_offset = src_err->oer_comp_offset;
454
455	dest_end = end_offset(dest_err->oer_comp_offset,
456			      dest_err->oer_comp_length);
457	src_end =  end_offset(src_err->oer_comp_offset,
458			      src_err->oer_comp_length);
459	if (dest_end < src_end)
460		dest_end = src_end;
461
462	dest_err->oer_comp_length = dest_end - dest_err->oer_comp_offset;
463
464	if ((src_err->oer_iswrite == dest_err->oer_iswrite) &&
465	    (err_prio(src_err->oer_errno) > err_prio(dest_err->oer_errno))) {
466			dest_err->oer_errno = src_err->oer_errno;
467	} else if (src_err->oer_iswrite) {
468		dest_err->oer_iswrite = true;
469		dest_err->oer_errno = src_err->oer_errno;
470	}
471}
472
473static void
474encode_accumulated_error(struct objlayout *objlay, __be32 *p)
475{
476	struct objlayout_io_res *oir, *tmp;
477	struct pnfs_osd_ioerr accumulated_err = {.oer_errno = 0};
478
479	list_for_each_entry_safe(oir, tmp, &objlay->err_list, err_list) {
480		unsigned i;
481
482		for (i = 0; i < oir->num_comps; i++) {
483			struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[i];
484
485			if (!ioerr->oer_errno)
486				continue;
487
488			printk(KERN_ERR "NFS: %s: err[%d]: errno=%d "
489				"is_write=%d dev(%llx:%llx) par=0x%llx "
490				"obj=0x%llx offset=0x%llx length=0x%llx\n",
491				__func__, i, ioerr->oer_errno,
492				ioerr->oer_iswrite,
493				_DEVID_LO(&ioerr->oer_component.oid_device_id),
494				_DEVID_HI(&ioerr->oer_component.oid_device_id),
495				ioerr->oer_component.oid_partition_id,
496				ioerr->oer_component.oid_object_id,
497				ioerr->oer_comp_offset,
498				ioerr->oer_comp_length);
499
500			merge_ioerr(&accumulated_err, ioerr);
501		}
502		list_del(&oir->err_list);
503		objio_free_result(oir);
504	}
505
506	pnfs_osd_xdr_encode_ioerr(p, &accumulated_err);
507}
508
509void
510objlayout_encode_layoutreturn(struct pnfs_layout_hdr *pnfslay,
511			      struct xdr_stream *xdr,
512			      const struct nfs4_layoutreturn_args *args)
513{
514	struct objlayout *objlay = OBJLAYOUT(pnfslay);
515	struct objlayout_io_res *oir, *tmp;
516	__be32 *start;
517
518	dprintk("%s: Begin\n", __func__);
519	start = xdr_reserve_space(xdr, 4);
520	BUG_ON(!start);
521
522	spin_lock(&objlay->lock);
523
524	list_for_each_entry_safe(oir, tmp, &objlay->err_list, err_list) {
525		__be32 *last_xdr = NULL, *p;
526		unsigned i;
527		int res = 0;
528
529		for (i = 0; i < oir->num_comps; i++) {
530			struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[i];
531
532			if (!ioerr->oer_errno)
533				continue;
534
535			dprintk("%s: err[%d]: errno=%d is_write=%d "
536				"dev(%llx:%llx) par=0x%llx obj=0x%llx "
537				"offset=0x%llx length=0x%llx\n",
538				__func__, i, ioerr->oer_errno,
539				ioerr->oer_iswrite,
540				_DEVID_LO(&ioerr->oer_component.oid_device_id),
541				_DEVID_HI(&ioerr->oer_component.oid_device_id),
542				ioerr->oer_component.oid_partition_id,
543				ioerr->oer_component.oid_object_id,
544				ioerr->oer_comp_offset,
545				ioerr->oer_comp_length);
546
547			p = pnfs_osd_xdr_ioerr_reserve_space(xdr);
548			if (unlikely(!p)) {
549				res = -E2BIG;
550				break; /* accumulated_error */
551			}
552
553			last_xdr = p;
554			pnfs_osd_xdr_encode_ioerr(p, &oir->ioerrs[i]);
555		}
556
557		/* TODO: use xdr_write_pages */
558		if (unlikely(res)) {
559			/* no space for even one error descriptor */
560			BUG_ON(!last_xdr);
561
562			/* we've encountered a situation with lots and lots of
563			 * errors and no space to encode them all. Use the last
564			 * available slot to report the union of all the
565			 * remaining errors.
566			 */
567			encode_accumulated_error(objlay, last_xdr);
568			goto loop_done;
569		}
570		list_del(&oir->err_list);
571		objio_free_result(oir);
572	}
573loop_done:
574	spin_unlock(&objlay->lock);
575
576	*start = cpu_to_be32((xdr->p - start - 1) * 4);
577	dprintk("%s: Return\n", __func__);
578}
579
580
581/*
582 * Get Device Info API for io engines
583 */
584struct objlayout_deviceinfo {
585	struct page *page;
586	struct pnfs_osd_deviceaddr da; /* This must be last */
587};
588
589/* Initialize and call nfs_getdeviceinfo, then decode and return a
590 * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo()
591 * should be called.
592 */
593int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay,
594	struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr,
595	gfp_t gfp_flags)
596{
597	struct objlayout_deviceinfo *odi;
598	struct pnfs_device pd;
 
599	struct page *page, **pages;
600	u32 *p;
601	int err;
602
603	page = alloc_page(gfp_flags);
604	if (!page)
605		return -ENOMEM;
606
607	pages = &page;
608	pd.pages = pages;
609
610	memcpy(&pd.dev_id, d_id, sizeof(*d_id));
611	pd.layout_type = LAYOUT_OSD2_OBJECTS;
612	pd.pages = &page;
613	pd.pgbase = 0;
614	pd.pglen = PAGE_SIZE;
615	pd.mincount = 0;
616	pd.maxcount = PAGE_SIZE;
617
618	err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd,
619			pnfslay->plh_lc_cred);
620	dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err);
621	if (err)
622		goto err_out;
623
624	p = page_address(page);
625	odi = kzalloc(sizeof(*odi), gfp_flags);
626	if (!odi) {
627		err = -ENOMEM;
628		goto err_out;
629	}
630	pnfs_osd_xdr_decode_deviceaddr(&odi->da, p);
631	odi->page = page;
632	*deviceaddr = &odi->da;
633	return 0;
634
635err_out:
636	__free_page(page);
637	return err;
638}
639
640void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr)
641{
642	struct objlayout_deviceinfo *odi = container_of(deviceaddr,
643						struct objlayout_deviceinfo,
644						da);
645
646	__free_page(odi->page);
647	kfree(odi);
648}
649
650enum {
651	OBJLAYOUT_MAX_URI_LEN = 256, OBJLAYOUT_MAX_OSDNAME_LEN = 64,
652	OBJLAYOUT_MAX_SYSID_HEX_LEN = OSD_SYSTEMID_LEN * 2 + 1,
653	OSD_LOGIN_UPCALL_PATHLEN  = 256
654};
655
656static char osd_login_prog[OSD_LOGIN_UPCALL_PATHLEN] = "/sbin/osd_login";
657
658module_param_string(osd_login_prog, osd_login_prog, sizeof(osd_login_prog),
659		    0600);
660MODULE_PARM_DESC(osd_login_prog, "Path to the osd_login upcall program");
661
662struct __auto_login {
663	char uri[OBJLAYOUT_MAX_URI_LEN];
664	char osdname[OBJLAYOUT_MAX_OSDNAME_LEN];
665	char systemid_hex[OBJLAYOUT_MAX_SYSID_HEX_LEN];
666};
667
668static int __objlayout_upcall(struct __auto_login *login)
669{
670	static char *envp[] = { "HOME=/",
671		"TERM=linux",
672		"PATH=/sbin:/usr/sbin:/bin:/usr/bin",
673		NULL
674	};
675	char *argv[8];
676	int ret;
677
678	if (unlikely(!osd_login_prog[0])) {
679		dprintk("%s: osd_login_prog is disabled\n", __func__);
680		return -EACCES;
681	}
682
683	dprintk("%s uri: %s\n", __func__, login->uri);
684	dprintk("%s osdname %s\n", __func__, login->osdname);
685	dprintk("%s systemid_hex %s\n", __func__, login->systemid_hex);
686
687	argv[0] = (char *)osd_login_prog;
688	argv[1] = "-u";
689	argv[2] = login->uri;
690	argv[3] = "-o";
691	argv[4] = login->osdname;
692	argv[5] = "-s";
693	argv[6] = login->systemid_hex;
694	argv[7] = NULL;
695
696	ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
697	/*
698	 * Disable the upcall mechanism if we're getting an ENOENT or
699	 * EACCES error. The admin can re-enable it on the fly by using
700	 * sysfs to set the objlayoutdriver.osd_login_prog module parameter once
701	 * the problem has been fixed.
702	 */
703	if (ret == -ENOENT || ret == -EACCES) {
704		printk(KERN_ERR "PNFS-OBJ: %s was not found please set "
705			"objlayoutdriver.osd_login_prog kernel parameter!\n",
706			osd_login_prog);
707		osd_login_prog[0] = '\0';
708	}
709	dprintk("%s %s return value: %d\n", __func__, osd_login_prog, ret);
710
711	return ret;
712}
713
714/* Assume dest is all zeros */
715static void __copy_nfsS_and_zero_terminate(struct nfs4_string s,
716					   char *dest, int max_len,
717					   const char *var_name)
718{
719	if (!s.len)
720		return;
721
722	if (s.len >= max_len) {
723		pr_warn_ratelimited(
724			"objlayout_autologin: %s: s.len(%d) >= max_len(%d)",
725			var_name, s.len, max_len);
726		s.len = max_len - 1; /* space for null terminator */
727	}
728
729	memcpy(dest, s.data, s.len);
730}
731
732/* Assume sysid is all zeros */
733static void _sysid_2_hex(struct nfs4_string s,
734		  char sysid[OBJLAYOUT_MAX_SYSID_HEX_LEN])
735{
736	int i;
737	char *cur;
738
739	if (!s.len)
740		return;
741
742	if (s.len != OSD_SYSTEMID_LEN) {
743		pr_warn_ratelimited(
744		    "objlayout_autologin: systemid_len(%d) != OSD_SYSTEMID_LEN",
745		    s.len);
746		if (s.len > OSD_SYSTEMID_LEN)
747			s.len = OSD_SYSTEMID_LEN;
748	}
749
750	cur = sysid;
751	for (i = 0; i < s.len; i++)
752		cur = hex_byte_pack(cur, s.data[i]);
753}
754
755int objlayout_autologin(struct pnfs_osd_deviceaddr *deviceaddr)
756{
757	int rc;
758	struct __auto_login login;
759
760	if (!deviceaddr->oda_targetaddr.ota_netaddr.r_addr.len)
761		return -ENODEV;
762
763	memset(&login, 0, sizeof(login));
764	__copy_nfsS_and_zero_terminate(
765		deviceaddr->oda_targetaddr.ota_netaddr.r_addr,
766		login.uri, sizeof(login.uri), "URI");
767
768	__copy_nfsS_and_zero_terminate(
769		deviceaddr->oda_osdname,
770		login.osdname, sizeof(login.osdname), "OSDNAME");
771
772	_sysid_2_hex(deviceaddr->oda_systemid, login.systemid_hex);
773
774	rc = __objlayout_upcall(&login);
775	if (rc > 0) /* script returns positive values */
776		rc = -ENODEV;
777
778	return rc;
779}
v3.1
  1/*
  2 *  pNFS Objects layout driver high level definitions
  3 *
  4 *  Copyright (C) 2007 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 <scsi/osd_initiator.h>
 41#include "objlayout.h"
 42
 43#define NFSDBG_FACILITY         NFSDBG_PNFS_LD
 44/*
 45 * Create a objlayout layout structure for the given inode and return it.
 46 */
 47struct pnfs_layout_hdr *
 48objlayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
 49{
 50	struct objlayout *objlay;
 51
 52	objlay = kzalloc(sizeof(struct objlayout), gfp_flags);
 53	if (objlay) {
 54		spin_lock_init(&objlay->lock);
 55		INIT_LIST_HEAD(&objlay->err_list);
 56	}
 57	dprintk("%s: Return %p\n", __func__, objlay);
 58	return &objlay->pnfs_layout;
 59}
 60
 61/*
 62 * Free an objlayout layout structure
 63 */
 64void
 65objlayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
 66{
 67	struct objlayout *objlay = OBJLAYOUT(lo);
 68
 69	dprintk("%s: objlay %p\n", __func__, objlay);
 70
 71	WARN_ON(!list_empty(&objlay->err_list));
 72	kfree(objlay);
 73}
 74
 75/*
 76 * Unmarshall layout and store it in pnfslay.
 77 */
 78struct pnfs_layout_segment *
 79objlayout_alloc_lseg(struct pnfs_layout_hdr *pnfslay,
 80		     struct nfs4_layoutget_res *lgr,
 81		     gfp_t gfp_flags)
 82{
 83	int status = -ENOMEM;
 84	struct xdr_stream stream;
 85	struct xdr_buf buf = {
 86		.pages =  lgr->layoutp->pages,
 87		.page_len =  lgr->layoutp->len,
 88		.buflen =  lgr->layoutp->len,
 89		.len = lgr->layoutp->len,
 90	};
 91	struct page *scratch;
 92	struct pnfs_layout_segment *lseg;
 93
 94	dprintk("%s: Begin pnfslay %p\n", __func__, pnfslay);
 95
 96	scratch = alloc_page(gfp_flags);
 97	if (!scratch)
 98		goto err_nofree;
 99
100	xdr_init_decode(&stream, &buf, NULL);
101	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
102
103	status = objio_alloc_lseg(&lseg, pnfslay, &lgr->range, &stream, gfp_flags);
104	if (unlikely(status)) {
105		dprintk("%s: objio_alloc_lseg Return err %d\n", __func__,
106			status);
107		goto err;
108	}
109
110	__free_page(scratch);
111
112	dprintk("%s: Return %p\n", __func__, lseg);
113	return lseg;
114
115err:
116	__free_page(scratch);
117err_nofree:
118	dprintk("%s: Err Return=>%d\n", __func__, status);
119	return ERR_PTR(status);
120}
121
122/*
123 * Free a layout segement
124 */
125void
126objlayout_free_lseg(struct pnfs_layout_segment *lseg)
127{
128	dprintk("%s: freeing layout segment %p\n", __func__, lseg);
129
130	if (unlikely(!lseg))
131		return;
132
133	objio_free_lseg(lseg);
134}
135
136/*
137 * I/O Operations
138 */
139static inline u64
140end_offset(u64 start, u64 len)
141{
142	u64 end;
143
144	end = start + len;
145	return end >= start ? end : NFS4_MAX_UINT64;
146}
147
148/* last octet in a range */
149static inline u64
150last_byte_offset(u64 start, u64 len)
151{
152	u64 end;
153
154	BUG_ON(!len);
155	end = start + len;
156	return end > start ? end - 1 : NFS4_MAX_UINT64;
157}
158
159static struct objlayout_io_state *
160objlayout_alloc_io_state(struct pnfs_layout_hdr *pnfs_layout_type,
161			struct page **pages,
162			unsigned pgbase,
163			loff_t offset,
164			size_t count,
165			struct pnfs_layout_segment *lseg,
166			void *rpcdata,
167			gfp_t gfp_flags)
168{
169	struct objlayout_io_state *state;
170	u64 lseg_end_offset;
171
172	dprintk("%s: allocating io_state\n", __func__);
173	if (objio_alloc_io_state(lseg, &state, gfp_flags))
174		return NULL;
175
176	BUG_ON(offset < lseg->pls_range.offset);
177	lseg_end_offset = end_offset(lseg->pls_range.offset,
178				     lseg->pls_range.length);
179	BUG_ON(offset >= lseg_end_offset);
180	if (offset + count > lseg_end_offset) {
181		count = lseg->pls_range.length -
182				(offset - lseg->pls_range.offset);
183		dprintk("%s: truncated count %Zd\n", __func__, count);
184	}
185
186	if (pgbase > PAGE_SIZE) {
187		pages += pgbase >> PAGE_SHIFT;
188		pgbase &= ~PAGE_MASK;
189	}
190
191	INIT_LIST_HEAD(&state->err_list);
192	state->lseg = lseg;
193	state->rpcdata = rpcdata;
194	state->pages = pages;
195	state->pgbase = pgbase;
196	state->nr_pages = (pgbase + count + PAGE_SIZE - 1) >> PAGE_SHIFT;
197	state->offset = offset;
198	state->count = count;
199	state->sync = 0;
200
201	return state;
202}
203
204static void
205objlayout_free_io_state(struct objlayout_io_state *state)
206{
207	dprintk("%s: freeing io_state\n", __func__);
208	if (unlikely(!state))
209		return;
210
211	objio_free_io_state(state);
212}
213
214/*
215 * I/O done common code
216 */
217static void
218objlayout_iodone(struct objlayout_io_state *state)
219{
220	dprintk("%s: state %p status\n", __func__, state);
221
222	if (likely(state->status >= 0)) {
223		objlayout_free_io_state(state);
224	} else {
225		struct objlayout *objlay = OBJLAYOUT(state->lseg->pls_layout);
226
227		spin_lock(&objlay->lock);
228		objlay->delta_space_valid = OBJ_DSU_INVALID;
229		list_add(&objlay->err_list, &state->err_list);
230		spin_unlock(&objlay->lock);
231	}
232}
233
234/*
235 * objlayout_io_set_result - Set an osd_error code on a specific osd comp.
236 *
237 * The @index component IO failed (error returned from target). Register
238 * the error for later reporting at layout-return.
239 */
240void
241objlayout_io_set_result(struct objlayout_io_state *state, unsigned index,
242			struct pnfs_osd_objid *pooid, int osd_error,
243			u64 offset, u64 length, bool is_write)
244{
245	struct pnfs_osd_ioerr *ioerr = &state->ioerrs[index];
246
247	BUG_ON(index >= state->num_comps);
248	if (osd_error) {
249		ioerr->oer_component = *pooid;
250		ioerr->oer_comp_offset = offset;
251		ioerr->oer_comp_length = length;
252		ioerr->oer_iswrite = is_write;
253		ioerr->oer_errno = osd_error;
254
255		dprintk("%s: err[%d]: errno=%d is_write=%d dev(%llx:%llx) "
256			"par=0x%llx obj=0x%llx offset=0x%llx length=0x%llx\n",
257			__func__, index, ioerr->oer_errno,
258			ioerr->oer_iswrite,
259			_DEVID_LO(&ioerr->oer_component.oid_device_id),
260			_DEVID_HI(&ioerr->oer_component.oid_device_id),
261			ioerr->oer_component.oid_partition_id,
262			ioerr->oer_component.oid_object_id,
263			ioerr->oer_comp_offset,
264			ioerr->oer_comp_length);
265	} else {
266		/* User need not call if no error is reported */
267		ioerr->oer_errno = 0;
268	}
269}
270
271/* Function scheduled on rpc workqueue to call ->nfs_readlist_complete().
272 * This is because the osd completion is called with ints-off from
273 * the block layer
274 */
275static void _rpc_read_complete(struct work_struct *work)
276{
277	struct rpc_task *task;
278	struct nfs_read_data *rdata;
279
280	dprintk("%s enter\n", __func__);
281	task = container_of(work, struct rpc_task, u.tk_work);
282	rdata = container_of(task, struct nfs_read_data, task);
283
284	pnfs_ld_read_done(rdata);
285}
286
287void
288objlayout_read_done(struct objlayout_io_state *state, ssize_t status, bool sync)
289{
290	int eof = state->eof;
291	struct nfs_read_data *rdata;
292
293	state->status = status;
294	dprintk("%s: Begin status=%zd eof=%d\n", __func__, status, eof);
295	rdata = state->rpcdata;
296	rdata->task.tk_status = status;
297	if (status >= 0) {
298		rdata->res.count = status;
299		rdata->res.eof = eof;
300	}
301	objlayout_iodone(state);
302	/* must not use state after this point */
 
 
 
303
304	if (sync)
305		pnfs_ld_read_done(rdata);
306	else {
307		INIT_WORK(&rdata->task.u.tk_work, _rpc_read_complete);
308		schedule_work(&rdata->task.u.tk_work);
309	}
310}
311
312/*
313 * Perform sync or async reads.
314 */
315enum pnfs_try_status
316objlayout_read_pagelist(struct nfs_read_data *rdata)
317{
 
 
318	loff_t offset = rdata->args.offset;
319	size_t count = rdata->args.count;
320	struct objlayout_io_state *state;
321	ssize_t status = 0;
322	loff_t eof;
323
324	dprintk("%s: Begin inode %p offset %llu count %d\n",
325		__func__, rdata->inode, offset, (int)count);
326
327	eof = i_size_read(rdata->inode);
328	if (unlikely(offset + count > eof)) {
329		if (offset >= eof) {
330			status = 0;
331			rdata->res.count = 0;
332			rdata->res.eof = 1;
 
333			goto out;
334		}
335		count = eof - offset;
336	}
337
338	state = objlayout_alloc_io_state(NFS_I(rdata->inode)->layout,
339					 rdata->args.pages, rdata->args.pgbase,
340					 offset, count,
341					 rdata->lseg, rdata,
342					 GFP_KERNEL);
343	if (unlikely(!state)) {
344		status = -ENOMEM;
345		goto out;
346	}
347
348	state->eof = state->offset + state->count >= eof;
 
349
350	status = objio_read_pagelist(state);
351 out:
352	dprintk("%s: Return status %Zd\n", __func__, status);
353	rdata->pnfs_error = status;
 
 
 
354	return PNFS_ATTEMPTED;
355}
356
357/* Function scheduled on rpc workqueue to call ->nfs_writelist_complete().
358 * This is because the osd completion is called with ints-off from
359 * the block layer
360 */
361static void _rpc_write_complete(struct work_struct *work)
362{
363	struct rpc_task *task;
364	struct nfs_write_data *wdata;
365
366	dprintk("%s enter\n", __func__);
367	task = container_of(work, struct rpc_task, u.tk_work);
368	wdata = container_of(task, struct nfs_write_data, task);
369
370	pnfs_ld_write_done(wdata);
371}
372
373void
374objlayout_write_done(struct objlayout_io_state *state, ssize_t status,
375		     bool sync)
376{
377	struct nfs_write_data *wdata;
378
379	dprintk("%s: Begin\n", __func__);
380	wdata = state->rpcdata;
381	state->status = status;
382	wdata->task.tk_status = status;
383	if (status >= 0) {
384		wdata->res.count = status;
385		wdata->verf.committed = state->committed;
386		dprintk("%s: Return status %d committed %d\n",
387			__func__, wdata->task.tk_status,
388			wdata->verf.committed);
389	} else
390		dprintk("%s: Return status %d\n",
391			__func__, wdata->task.tk_status);
392	objlayout_iodone(state);
393	/* must not use state after this point */
394
395	if (sync)
396		pnfs_ld_write_done(wdata);
397	else {
398		INIT_WORK(&wdata->task.u.tk_work, _rpc_write_complete);
399		schedule_work(&wdata->task.u.tk_work);
400	}
401}
402
403/*
404 * Perform sync or async writes.
405 */
406enum pnfs_try_status
407objlayout_write_pagelist(struct nfs_write_data *wdata,
408			 int how)
409{
410	struct objlayout_io_state *state;
411	ssize_t status;
412
413	dprintk("%s: Begin inode %p offset %llu count %u\n",
414		__func__, wdata->inode, wdata->args.offset, wdata->args.count);
415
416	state = objlayout_alloc_io_state(NFS_I(wdata->inode)->layout,
417					 wdata->args.pages,
418					 wdata->args.pgbase,
419					 wdata->args.offset,
420					 wdata->args.count,
421					 wdata->lseg, wdata,
422					 GFP_NOFS);
423	if (unlikely(!state)) {
424		status = -ENOMEM;
425		goto out;
426	}
427
428	state->sync = how & FLUSH_SYNC;
429
430	status = objio_write_pagelist(state, how & FLUSH_STABLE);
431 out:
432	dprintk("%s: Return status %Zd\n", __func__, status);
433	wdata->pnfs_error = status;
434	return PNFS_ATTEMPTED;
435}
436
437void
438objlayout_encode_layoutcommit(struct pnfs_layout_hdr *pnfslay,
439			      struct xdr_stream *xdr,
440			      const struct nfs4_layoutcommit_args *args)
441{
442	struct objlayout *objlay = OBJLAYOUT(pnfslay);
443	struct pnfs_osd_layoutupdate lou;
444	__be32 *start;
445
446	dprintk("%s: Begin\n", __func__);
447
448	spin_lock(&objlay->lock);
449	lou.dsu_valid = (objlay->delta_space_valid == OBJ_DSU_VALID);
450	lou.dsu_delta = objlay->delta_space_used;
451	objlay->delta_space_used = 0;
452	objlay->delta_space_valid = OBJ_DSU_INIT;
453	lou.olu_ioerr_flag = !list_empty(&objlay->err_list);
454	spin_unlock(&objlay->lock);
455
456	start = xdr_reserve_space(xdr, 4);
457
458	BUG_ON(pnfs_osd_xdr_encode_layoutupdate(xdr, &lou));
459
460	*start = cpu_to_be32((xdr->p - start - 1) * 4);
461
462	dprintk("%s: Return delta_space_used %lld err %d\n", __func__,
463		lou.dsu_delta, lou.olu_ioerr_flag);
464}
465
466static int
467err_prio(u32 oer_errno)
468{
469	switch (oer_errno) {
470	case 0:
471		return 0;
472
473	case PNFS_OSD_ERR_RESOURCE:
474		return OSD_ERR_PRI_RESOURCE;
475	case PNFS_OSD_ERR_BAD_CRED:
476		return OSD_ERR_PRI_BAD_CRED;
477	case PNFS_OSD_ERR_NO_ACCESS:
478		return OSD_ERR_PRI_NO_ACCESS;
479	case PNFS_OSD_ERR_UNREACHABLE:
480		return OSD_ERR_PRI_UNREACHABLE;
481	case PNFS_OSD_ERR_NOT_FOUND:
482		return OSD_ERR_PRI_NOT_FOUND;
483	case PNFS_OSD_ERR_NO_SPACE:
484		return OSD_ERR_PRI_NO_SPACE;
485	default:
486		WARN_ON(1);
487		/* fallthrough */
488	case PNFS_OSD_ERR_EIO:
489		return OSD_ERR_PRI_EIO;
490	}
491}
492
493static void
494merge_ioerr(struct pnfs_osd_ioerr *dest_err,
495	    const struct pnfs_osd_ioerr *src_err)
496{
497	u64 dest_end, src_end;
498
499	if (!dest_err->oer_errno) {
500		*dest_err = *src_err;
501		/* accumulated device must be blank */
502		memset(&dest_err->oer_component.oid_device_id, 0,
503			sizeof(dest_err->oer_component.oid_device_id));
504
505		return;
506	}
507
508	if (dest_err->oer_component.oid_partition_id !=
509				src_err->oer_component.oid_partition_id)
510		dest_err->oer_component.oid_partition_id = 0;
511
512	if (dest_err->oer_component.oid_object_id !=
513				src_err->oer_component.oid_object_id)
514		dest_err->oer_component.oid_object_id = 0;
515
516	if (dest_err->oer_comp_offset > src_err->oer_comp_offset)
517		dest_err->oer_comp_offset = src_err->oer_comp_offset;
518
519	dest_end = end_offset(dest_err->oer_comp_offset,
520			      dest_err->oer_comp_length);
521	src_end =  end_offset(src_err->oer_comp_offset,
522			      src_err->oer_comp_length);
523	if (dest_end < src_end)
524		dest_end = src_end;
525
526	dest_err->oer_comp_length = dest_end - dest_err->oer_comp_offset;
527
528	if ((src_err->oer_iswrite == dest_err->oer_iswrite) &&
529	    (err_prio(src_err->oer_errno) > err_prio(dest_err->oer_errno))) {
530			dest_err->oer_errno = src_err->oer_errno;
531	} else if (src_err->oer_iswrite) {
532		dest_err->oer_iswrite = true;
533		dest_err->oer_errno = src_err->oer_errno;
534	}
535}
536
537static void
538encode_accumulated_error(struct objlayout *objlay, __be32 *p)
539{
540	struct objlayout_io_state *state, *tmp;
541	struct pnfs_osd_ioerr accumulated_err = {.oer_errno = 0};
542
543	list_for_each_entry_safe(state, tmp, &objlay->err_list, err_list) {
544		unsigned i;
545
546		for (i = 0; i < state->num_comps; i++) {
547			struct pnfs_osd_ioerr *ioerr = &state->ioerrs[i];
548
549			if (!ioerr->oer_errno)
550				continue;
551
552			printk(KERN_ERR "%s: err[%d]: errno=%d is_write=%d "
553				"dev(%llx:%llx) par=0x%llx obj=0x%llx "
554				"offset=0x%llx length=0x%llx\n",
555				__func__, i, ioerr->oer_errno,
556				ioerr->oer_iswrite,
557				_DEVID_LO(&ioerr->oer_component.oid_device_id),
558				_DEVID_HI(&ioerr->oer_component.oid_device_id),
559				ioerr->oer_component.oid_partition_id,
560				ioerr->oer_component.oid_object_id,
561				ioerr->oer_comp_offset,
562				ioerr->oer_comp_length);
563
564			merge_ioerr(&accumulated_err, ioerr);
565		}
566		list_del(&state->err_list);
567		objlayout_free_io_state(state);
568	}
569
570	pnfs_osd_xdr_encode_ioerr(p, &accumulated_err);
571}
572
573void
574objlayout_encode_layoutreturn(struct pnfs_layout_hdr *pnfslay,
575			      struct xdr_stream *xdr,
576			      const struct nfs4_layoutreturn_args *args)
577{
578	struct objlayout *objlay = OBJLAYOUT(pnfslay);
579	struct objlayout_io_state *state, *tmp;
580	__be32 *start;
581
582	dprintk("%s: Begin\n", __func__);
583	start = xdr_reserve_space(xdr, 4);
584	BUG_ON(!start);
585
586	spin_lock(&objlay->lock);
587
588	list_for_each_entry_safe(state, tmp, &objlay->err_list, err_list) {
589		__be32 *last_xdr = NULL, *p;
590		unsigned i;
591		int res = 0;
592
593		for (i = 0; i < state->num_comps; i++) {
594			struct pnfs_osd_ioerr *ioerr = &state->ioerrs[i];
595
596			if (!ioerr->oer_errno)
597				continue;
598
599			dprintk("%s: err[%d]: errno=%d is_write=%d "
600				"dev(%llx:%llx) par=0x%llx obj=0x%llx "
601				"offset=0x%llx length=0x%llx\n",
602				__func__, i, ioerr->oer_errno,
603				ioerr->oer_iswrite,
604				_DEVID_LO(&ioerr->oer_component.oid_device_id),
605				_DEVID_HI(&ioerr->oer_component.oid_device_id),
606				ioerr->oer_component.oid_partition_id,
607				ioerr->oer_component.oid_object_id,
608				ioerr->oer_comp_offset,
609				ioerr->oer_comp_length);
610
611			p = pnfs_osd_xdr_ioerr_reserve_space(xdr);
612			if (unlikely(!p)) {
613				res = -E2BIG;
614				break; /* accumulated_error */
615			}
616
617			last_xdr = p;
618			pnfs_osd_xdr_encode_ioerr(p, &state->ioerrs[i]);
619		}
620
621		/* TODO: use xdr_write_pages */
622		if (unlikely(res)) {
623			/* no space for even one error descriptor */
624			BUG_ON(!last_xdr);
625
626			/* we've encountered a situation with lots and lots of
627			 * errors and no space to encode them all. Use the last
628			 * available slot to report the union of all the
629			 * remaining errors.
630			 */
631			encode_accumulated_error(objlay, last_xdr);
632			goto loop_done;
633		}
634		list_del(&state->err_list);
635		objlayout_free_io_state(state);
636	}
637loop_done:
638	spin_unlock(&objlay->lock);
639
640	*start = cpu_to_be32((xdr->p - start - 1) * 4);
641	dprintk("%s: Return\n", __func__);
642}
643
644
645/*
646 * Get Device Info API for io engines
647 */
648struct objlayout_deviceinfo {
649	struct page *page;
650	struct pnfs_osd_deviceaddr da; /* This must be last */
651};
652
653/* Initialize and call nfs_getdeviceinfo, then decode and return a
654 * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo()
655 * should be called.
656 */
657int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay,
658	struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr,
659	gfp_t gfp_flags)
660{
661	struct objlayout_deviceinfo *odi;
662	struct pnfs_device pd;
663	struct super_block *sb;
664	struct page *page, **pages;
665	u32 *p;
666	int err;
667
668	page = alloc_page(gfp_flags);
669	if (!page)
670		return -ENOMEM;
671
672	pages = &page;
673	pd.pages = pages;
674
675	memcpy(&pd.dev_id, d_id, sizeof(*d_id));
676	pd.layout_type = LAYOUT_OSD2_OBJECTS;
677	pd.pages = &page;
678	pd.pgbase = 0;
679	pd.pglen = PAGE_SIZE;
680	pd.mincount = 0;
 
681
682	sb = pnfslay->plh_inode->i_sb;
683	err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd);
684	dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err);
685	if (err)
686		goto err_out;
687
688	p = page_address(page);
689	odi = kzalloc(sizeof(*odi), gfp_flags);
690	if (!odi) {
691		err = -ENOMEM;
692		goto err_out;
693	}
694	pnfs_osd_xdr_decode_deviceaddr(&odi->da, p);
695	odi->page = page;
696	*deviceaddr = &odi->da;
697	return 0;
698
699err_out:
700	__free_page(page);
701	return err;
702}
703
704void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr)
705{
706	struct objlayout_deviceinfo *odi = container_of(deviceaddr,
707						struct objlayout_deviceinfo,
708						da);
709
710	__free_page(odi->page);
711	kfree(odi);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
712}