blob: 8746135453011dc70d30ebbd5b70e2b813f7b15d [file] [log] [blame]
Boaz Harrosh09f5bf42011-05-22 19:50:20 +03001/*
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
Sachin Bhamare18d98f62012-03-19 20:47:58 -070040#include <linux/kmod.h>
41#include <linux/moduleparam.h>
42#include <linux/ratelimit.h>
Boaz Harrosh09f5bf42011-05-22 19:50:20 +030043#include <scsi/osd_initiator.h>
44#include "objlayout.h"
45
46#define NFSDBG_FACILITY NFSDBG_PNFS_LD
47/*
Benny Halevye51b8412011-05-22 19:51:48 +030048 * 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);
Boaz Harroshadb58532011-05-26 21:49:46 +030056 if (objlay) {
57 spin_lock_init(&objlay->lock);
58 INIT_LIST_HEAD(&objlay->err_list);
59 }
Benny Halevye51b8412011-05-22 19:51:48 +030060 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
Boaz Harroshadb58532011-05-26 21:49:46 +030074 WARN_ON(!list_empty(&objlay->err_list));
Benny Halevye51b8412011-05-22 19:51:48 +030075 kfree(objlay);
76}
77
78/*
Boaz Harrosh09f5bf42011-05-22 19:50:20 +030079 * 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
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300139/*
Boaz Harrosh04f83452011-05-22 19:52:19 +0300140 * 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
151/* last octet in a range */
152static inline u64
153last_byte_offset(u64 start, u64 len)
154{
155 u64 end;
156
157 BUG_ON(!len);
158 end = start + len;
159 return end > start ? end - 1 : NFS4_MAX_UINT64;
160}
161
Trond Myklebust17280172012-03-11 13:11:00 -0400162static void _fix_verify_io_params(struct pnfs_layout_segment *lseg,
Boaz Harrosh96218552011-10-31 14:47:32 -0700163 struct page ***p_pages, unsigned *p_pgbase,
164 u64 offset, unsigned long count)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300165{
Boaz Harrosh04f83452011-05-22 19:52:19 +0300166 u64 lseg_end_offset;
167
Boaz Harrosh04f83452011-05-22 19:52:19 +0300168 BUG_ON(offset < lseg->pls_range.offset);
169 lseg_end_offset = end_offset(lseg->pls_range.offset,
170 lseg->pls_range.length);
171 BUG_ON(offset >= lseg_end_offset);
Boaz Harrosh96218552011-10-31 14:47:32 -0700172 WARN_ON(offset + count > lseg_end_offset);
173
174 if (*p_pgbase > PAGE_SIZE) {
175 dprintk("%s: pgbase(0x%x) > PAGE_SIZE\n", __func__, *p_pgbase);
176 *p_pages += *p_pgbase >> PAGE_SHIFT;
177 *p_pgbase &= ~PAGE_MASK;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300178 }
Boaz Harrosh04f83452011-05-22 19:52:19 +0300179}
180
181/*
182 * I/O done common code
183 */
184static void
Boaz Harroshe2e04352011-10-31 15:03:35 -0700185objlayout_iodone(struct objlayout_io_res *oir)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300186{
Boaz Harroshe2e04352011-10-31 15:03:35 -0700187 if (likely(oir->status >= 0)) {
188 objio_free_result(oir);
Boaz Harroshadb58532011-05-26 21:49:46 +0300189 } else {
Boaz Harroshe2e04352011-10-31 15:03:35 -0700190 struct objlayout *objlay = oir->objlay;
Boaz Harroshadb58532011-05-26 21:49:46 +0300191
192 spin_lock(&objlay->lock);
Boaz Harrosha0fe8bf2011-05-22 19:54:13 +0300193 objlay->delta_space_valid = OBJ_DSU_INVALID;
Boaz Harroshe2e04352011-10-31 15:03:35 -0700194 list_add(&objlay->err_list, &oir->err_list);
Boaz Harroshadb58532011-05-26 21:49:46 +0300195 spin_unlock(&objlay->lock);
196 }
197}
198
199/*
200 * objlayout_io_set_result - Set an osd_error code on a specific osd comp.
201 *
202 * The @index component IO failed (error returned from target). Register
203 * the error for later reporting at layout-return.
204 */
205void
Boaz Harroshe2e04352011-10-31 15:03:35 -0700206objlayout_io_set_result(struct objlayout_io_res *oir, unsigned index,
Boaz Harroshadb58532011-05-26 21:49:46 +0300207 struct pnfs_osd_objid *pooid, int osd_error,
208 u64 offset, u64 length, bool is_write)
209{
Boaz Harroshe2e04352011-10-31 15:03:35 -0700210 struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[index];
Boaz Harroshadb58532011-05-26 21:49:46 +0300211
Boaz Harroshe2e04352011-10-31 15:03:35 -0700212 BUG_ON(index >= oir->num_comps);
Boaz Harroshadb58532011-05-26 21:49:46 +0300213 if (osd_error) {
214 ioerr->oer_component = *pooid;
215 ioerr->oer_comp_offset = offset;
216 ioerr->oer_comp_length = length;
217 ioerr->oer_iswrite = is_write;
218 ioerr->oer_errno = osd_error;
219
220 dprintk("%s: err[%d]: errno=%d is_write=%d dev(%llx:%llx) "
221 "par=0x%llx obj=0x%llx offset=0x%llx length=0x%llx\n",
222 __func__, index, ioerr->oer_errno,
223 ioerr->oer_iswrite,
224 _DEVID_LO(&ioerr->oer_component.oid_device_id),
225 _DEVID_HI(&ioerr->oer_component.oid_device_id),
226 ioerr->oer_component.oid_partition_id,
227 ioerr->oer_component.oid_object_id,
228 ioerr->oer_comp_offset,
229 ioerr->oer_comp_length);
230 } else {
231 /* User need not call if no error is reported */
232 ioerr->oer_errno = 0;
233 }
Boaz Harrosh04f83452011-05-22 19:52:19 +0300234}
235
236/* Function scheduled on rpc workqueue to call ->nfs_readlist_complete().
237 * This is because the osd completion is called with ints-off from
238 * the block layer
239 */
240static void _rpc_read_complete(struct work_struct *work)
241{
242 struct rpc_task *task;
243 struct nfs_read_data *rdata;
244
245 dprintk("%s enter\n", __func__);
246 task = container_of(work, struct rpc_task, u.tk_work);
247 rdata = container_of(task, struct nfs_read_data, task);
248
249 pnfs_ld_read_done(rdata);
250}
251
252void
Boaz Harroshe2e04352011-10-31 15:03:35 -0700253objlayout_read_done(struct objlayout_io_res *oir, ssize_t status, bool sync)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300254{
Boaz Harroshe2e04352011-10-31 15:03:35 -0700255 struct nfs_read_data *rdata = oir->rpcdata;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300256
Boaz Harroshe2e04352011-10-31 15:03:35 -0700257 oir->status = rdata->task.tk_status = status;
Boaz Harrosh4cdc6852011-10-31 14:45:06 -0700258 if (status >= 0)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300259 rdata->res.count = status;
Boaz Harrosh5c0b4122012-01-06 09:28:12 +0200260 else
Fred Isamancd841602012-04-20 14:47:44 -0400261 rdata->header->pnfs_error = status;
Boaz Harroshe2e04352011-10-31 15:03:35 -0700262 objlayout_iodone(oir);
263 /* must not use oir after this point */
Boaz Harrosh04f83452011-05-22 19:52:19 +0300264
Boaz Harrosh96218552011-10-31 14:47:32 -0700265 dprintk("%s: Return status=%zd eof=%d sync=%d\n", __func__,
266 status, rdata->res.eof, sync);
267
Boaz Harrosh04f83452011-05-22 19:52:19 +0300268 if (sync)
269 pnfs_ld_read_done(rdata);
270 else {
271 INIT_WORK(&rdata->task.u.tk_work, _rpc_read_complete);
272 schedule_work(&rdata->task.u.tk_work);
273 }
274}
275
276/*
277 * Perform sync or async reads.
278 */
279enum pnfs_try_status
280objlayout_read_pagelist(struct nfs_read_data *rdata)
281{
Fred Isamancd841602012-04-20 14:47:44 -0400282 struct nfs_pgio_header *hdr = rdata->header;
283 struct inode *inode = hdr->inode;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300284 loff_t offset = rdata->args.offset;
285 size_t count = rdata->args.count;
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700286 int err;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300287 loff_t eof;
288
Fred Isamancd841602012-04-20 14:47:44 -0400289 eof = i_size_read(inode);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300290 if (unlikely(offset + count > eof)) {
291 if (offset >= eof) {
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700292 err = 0;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300293 rdata->res.count = 0;
294 rdata->res.eof = 1;
Boaz Harrosh4cdc6852011-10-31 14:45:06 -0700295 /*FIXME: do we need to call pnfs_ld_read_done() */
Boaz Harrosh04f83452011-05-22 19:52:19 +0300296 goto out;
297 }
298 count = eof - offset;
299 }
300
Boaz Harrosh4cdc6852011-10-31 14:45:06 -0700301 rdata->res.eof = (offset + count) >= eof;
Fred Isamancd841602012-04-20 14:47:44 -0400302 _fix_verify_io_params(hdr->lseg, &rdata->args.pages,
Boaz Harrosh96218552011-10-31 14:47:32 -0700303 &rdata->args.pgbase,
304 rdata->args.offset, rdata->args.count);
Boaz Harrosh4cdc6852011-10-31 14:45:06 -0700305
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700306 dprintk("%s: inode(%lx) offset 0x%llx count 0x%Zx eof=%d\n",
Fred Isamancd841602012-04-20 14:47:44 -0400307 __func__, inode->i_ino, offset, count, rdata->res.eof);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300308
Boaz Harrosh96218552011-10-31 14:47:32 -0700309 err = objio_read_pagelist(rdata);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300310 out:
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700311 if (unlikely(err)) {
Fred Isamancd841602012-04-20 14:47:44 -0400312 hdr->pnfs_error = err;
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700313 dprintk("%s: Returned Error %d\n", __func__, err);
314 return PNFS_NOT_ATTEMPTED;
315 }
Boaz Harrosh04f83452011-05-22 19:52:19 +0300316 return PNFS_ATTEMPTED;
317}
318
319/* Function scheduled on rpc workqueue to call ->nfs_writelist_complete().
320 * This is because the osd completion is called with ints-off from
321 * the block layer
322 */
323static void _rpc_write_complete(struct work_struct *work)
324{
325 struct rpc_task *task;
326 struct nfs_write_data *wdata;
327
328 dprintk("%s enter\n", __func__);
329 task = container_of(work, struct rpc_task, u.tk_work);
330 wdata = container_of(task, struct nfs_write_data, task);
331
332 pnfs_ld_write_done(wdata);
333}
334
335void
Boaz Harroshe2e04352011-10-31 15:03:35 -0700336objlayout_write_done(struct objlayout_io_res *oir, ssize_t status, bool sync)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300337{
Boaz Harroshe2e04352011-10-31 15:03:35 -0700338 struct nfs_write_data *wdata = oir->rpcdata;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300339
Boaz Harroshe2e04352011-10-31 15:03:35 -0700340 oir->status = wdata->task.tk_status = status;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300341 if (status >= 0) {
342 wdata->res.count = status;
Boaz Harroshe2e04352011-10-31 15:03:35 -0700343 wdata->verf.committed = oir->committed;
Boaz Harrosh5c0b4122012-01-06 09:28:12 +0200344 } else {
Fred Isamancd841602012-04-20 14:47:44 -0400345 wdata->header->pnfs_error = status;
Boaz Harrosh96218552011-10-31 14:47:32 -0700346 }
Boaz Harroshe2e04352011-10-31 15:03:35 -0700347 objlayout_iodone(oir);
Boaz Harrosh96218552011-10-31 14:47:32 -0700348 /* must not use oir after this point */
349
350 dprintk("%s: Return status %zd committed %d sync=%d\n", __func__,
351 status, wdata->verf.committed, sync);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300352
353 if (sync)
354 pnfs_ld_write_done(wdata);
355 else {
356 INIT_WORK(&wdata->task.u.tk_work, _rpc_write_complete);
357 schedule_work(&wdata->task.u.tk_work);
358 }
359}
360
361/*
362 * Perform sync or async writes.
363 */
364enum pnfs_try_status
365objlayout_write_pagelist(struct nfs_write_data *wdata,
366 int how)
367{
Fred Isamancd841602012-04-20 14:47:44 -0400368 struct nfs_pgio_header *hdr = wdata->header;
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700369 int err;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300370
Fred Isamancd841602012-04-20 14:47:44 -0400371 _fix_verify_io_params(hdr->lseg, &wdata->args.pages,
Boaz Harrosh96218552011-10-31 14:47:32 -0700372 &wdata->args.pgbase,
373 wdata->args.offset, wdata->args.count);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300374
Boaz Harrosh96218552011-10-31 14:47:32 -0700375 err = objio_write_pagelist(wdata, how);
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700376 if (unlikely(err)) {
Fred Isamancd841602012-04-20 14:47:44 -0400377 hdr->pnfs_error = err;
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700378 dprintk("%s: Returned Error %d\n", __func__, err);
379 return PNFS_NOT_ATTEMPTED;
380 }
Boaz Harrosh04f83452011-05-22 19:52:19 +0300381 return PNFS_ATTEMPTED;
382}
383
Boaz Harrosha0fe8bf2011-05-22 19:54:13 +0300384void
385objlayout_encode_layoutcommit(struct pnfs_layout_hdr *pnfslay,
386 struct xdr_stream *xdr,
387 const struct nfs4_layoutcommit_args *args)
388{
389 struct objlayout *objlay = OBJLAYOUT(pnfslay);
390 struct pnfs_osd_layoutupdate lou;
391 __be32 *start;
392
393 dprintk("%s: Begin\n", __func__);
394
395 spin_lock(&objlay->lock);
396 lou.dsu_valid = (objlay->delta_space_valid == OBJ_DSU_VALID);
397 lou.dsu_delta = objlay->delta_space_used;
398 objlay->delta_space_used = 0;
399 objlay->delta_space_valid = OBJ_DSU_INIT;
400 lou.olu_ioerr_flag = !list_empty(&objlay->err_list);
401 spin_unlock(&objlay->lock);
402
403 start = xdr_reserve_space(xdr, 4);
404
405 BUG_ON(pnfs_osd_xdr_encode_layoutupdate(xdr, &lou));
406
407 *start = cpu_to_be32((xdr->p - start - 1) * 4);
408
409 dprintk("%s: Return delta_space_used %lld err %d\n", __func__,
410 lou.dsu_delta, lou.olu_ioerr_flag);
411}
412
Boaz Harroshadb58532011-05-26 21:49:46 +0300413static int
414err_prio(u32 oer_errno)
415{
416 switch (oer_errno) {
417 case 0:
418 return 0;
419
420 case PNFS_OSD_ERR_RESOURCE:
421 return OSD_ERR_PRI_RESOURCE;
422 case PNFS_OSD_ERR_BAD_CRED:
423 return OSD_ERR_PRI_BAD_CRED;
424 case PNFS_OSD_ERR_NO_ACCESS:
425 return OSD_ERR_PRI_NO_ACCESS;
426 case PNFS_OSD_ERR_UNREACHABLE:
427 return OSD_ERR_PRI_UNREACHABLE;
428 case PNFS_OSD_ERR_NOT_FOUND:
429 return OSD_ERR_PRI_NOT_FOUND;
430 case PNFS_OSD_ERR_NO_SPACE:
431 return OSD_ERR_PRI_NO_SPACE;
432 default:
433 WARN_ON(1);
434 /* fallthrough */
435 case PNFS_OSD_ERR_EIO:
436 return OSD_ERR_PRI_EIO;
437 }
438}
439
440static void
441merge_ioerr(struct pnfs_osd_ioerr *dest_err,
442 const struct pnfs_osd_ioerr *src_err)
443{
444 u64 dest_end, src_end;
445
446 if (!dest_err->oer_errno) {
447 *dest_err = *src_err;
448 /* accumulated device must be blank */
449 memset(&dest_err->oer_component.oid_device_id, 0,
450 sizeof(dest_err->oer_component.oid_device_id));
451
452 return;
453 }
454
455 if (dest_err->oer_component.oid_partition_id !=
456 src_err->oer_component.oid_partition_id)
457 dest_err->oer_component.oid_partition_id = 0;
458
459 if (dest_err->oer_component.oid_object_id !=
460 src_err->oer_component.oid_object_id)
461 dest_err->oer_component.oid_object_id = 0;
462
463 if (dest_err->oer_comp_offset > src_err->oer_comp_offset)
464 dest_err->oer_comp_offset = src_err->oer_comp_offset;
465
466 dest_end = end_offset(dest_err->oer_comp_offset,
467 dest_err->oer_comp_length);
468 src_end = end_offset(src_err->oer_comp_offset,
469 src_err->oer_comp_length);
470 if (dest_end < src_end)
471 dest_end = src_end;
472
473 dest_err->oer_comp_length = dest_end - dest_err->oer_comp_offset;
474
475 if ((src_err->oer_iswrite == dest_err->oer_iswrite) &&
476 (err_prio(src_err->oer_errno) > err_prio(dest_err->oer_errno))) {
477 dest_err->oer_errno = src_err->oer_errno;
478 } else if (src_err->oer_iswrite) {
479 dest_err->oer_iswrite = true;
480 dest_err->oer_errno = src_err->oer_errno;
481 }
482}
483
484static void
485encode_accumulated_error(struct objlayout *objlay, __be32 *p)
486{
Boaz Harroshe2e04352011-10-31 15:03:35 -0700487 struct objlayout_io_res *oir, *tmp;
Boaz Harroshadb58532011-05-26 21:49:46 +0300488 struct pnfs_osd_ioerr accumulated_err = {.oer_errno = 0};
489
Boaz Harroshe2e04352011-10-31 15:03:35 -0700490 list_for_each_entry_safe(oir, tmp, &objlay->err_list, err_list) {
Boaz Harroshadb58532011-05-26 21:49:46 +0300491 unsigned i;
492
Boaz Harroshe2e04352011-10-31 15:03:35 -0700493 for (i = 0; i < oir->num_comps; i++) {
494 struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[i];
Boaz Harroshadb58532011-05-26 21:49:46 +0300495
496 if (!ioerr->oer_errno)
497 continue;
498
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500499 printk(KERN_ERR "NFS: %s: err[%d]: errno=%d "
500 "is_write=%d dev(%llx:%llx) par=0x%llx "
501 "obj=0x%llx offset=0x%llx length=0x%llx\n",
Boaz Harroshadb58532011-05-26 21:49:46 +0300502 __func__, i, ioerr->oer_errno,
503 ioerr->oer_iswrite,
504 _DEVID_LO(&ioerr->oer_component.oid_device_id),
505 _DEVID_HI(&ioerr->oer_component.oid_device_id),
506 ioerr->oer_component.oid_partition_id,
507 ioerr->oer_component.oid_object_id,
508 ioerr->oer_comp_offset,
509 ioerr->oer_comp_length);
510
511 merge_ioerr(&accumulated_err, ioerr);
512 }
Boaz Harroshe2e04352011-10-31 15:03:35 -0700513 list_del(&oir->err_list);
514 objio_free_result(oir);
Boaz Harroshadb58532011-05-26 21:49:46 +0300515 }
516
517 pnfs_osd_xdr_encode_ioerr(p, &accumulated_err);
518}
519
520void
521objlayout_encode_layoutreturn(struct pnfs_layout_hdr *pnfslay,
522 struct xdr_stream *xdr,
523 const struct nfs4_layoutreturn_args *args)
524{
525 struct objlayout *objlay = OBJLAYOUT(pnfslay);
Boaz Harroshe2e04352011-10-31 15:03:35 -0700526 struct objlayout_io_res *oir, *tmp;
Boaz Harroshadb58532011-05-26 21:49:46 +0300527 __be32 *start;
528
529 dprintk("%s: Begin\n", __func__);
530 start = xdr_reserve_space(xdr, 4);
531 BUG_ON(!start);
532
533 spin_lock(&objlay->lock);
534
Boaz Harroshe2e04352011-10-31 15:03:35 -0700535 list_for_each_entry_safe(oir, tmp, &objlay->err_list, err_list) {
Boaz Harroshadb58532011-05-26 21:49:46 +0300536 __be32 *last_xdr = NULL, *p;
537 unsigned i;
538 int res = 0;
539
Boaz Harroshe2e04352011-10-31 15:03:35 -0700540 for (i = 0; i < oir->num_comps; i++) {
541 struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[i];
Boaz Harroshadb58532011-05-26 21:49:46 +0300542
543 if (!ioerr->oer_errno)
544 continue;
545
546 dprintk("%s: err[%d]: errno=%d is_write=%d "
547 "dev(%llx:%llx) par=0x%llx obj=0x%llx "
548 "offset=0x%llx length=0x%llx\n",
549 __func__, i, ioerr->oer_errno,
550 ioerr->oer_iswrite,
551 _DEVID_LO(&ioerr->oer_component.oid_device_id),
552 _DEVID_HI(&ioerr->oer_component.oid_device_id),
553 ioerr->oer_component.oid_partition_id,
554 ioerr->oer_component.oid_object_id,
555 ioerr->oer_comp_offset,
556 ioerr->oer_comp_length);
557
558 p = pnfs_osd_xdr_ioerr_reserve_space(xdr);
559 if (unlikely(!p)) {
560 res = -E2BIG;
561 break; /* accumulated_error */
562 }
563
564 last_xdr = p;
Boaz Harroshe2e04352011-10-31 15:03:35 -0700565 pnfs_osd_xdr_encode_ioerr(p, &oir->ioerrs[i]);
Boaz Harroshadb58532011-05-26 21:49:46 +0300566 }
567
568 /* TODO: use xdr_write_pages */
569 if (unlikely(res)) {
570 /* no space for even one error descriptor */
571 BUG_ON(!last_xdr);
572
573 /* we've encountered a situation with lots and lots of
574 * errors and no space to encode them all. Use the last
575 * available slot to report the union of all the
576 * remaining errors.
577 */
578 encode_accumulated_error(objlay, last_xdr);
579 goto loop_done;
580 }
Boaz Harroshe2e04352011-10-31 15:03:35 -0700581 list_del(&oir->err_list);
582 objio_free_result(oir);
Boaz Harroshadb58532011-05-26 21:49:46 +0300583 }
584loop_done:
585 spin_unlock(&objlay->lock);
586
587 *start = cpu_to_be32((xdr->p - start - 1) * 4);
588 dprintk("%s: Return\n", __func__);
589}
590
591
Boaz Harrosh04f83452011-05-22 19:52:19 +0300592/*
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300593 * Get Device Info API for io engines
594 */
595struct objlayout_deviceinfo {
596 struct page *page;
597 struct pnfs_osd_deviceaddr da; /* This must be last */
598};
599
600/* Initialize and call nfs_getdeviceinfo, then decode and return a
601 * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo()
602 * should be called.
603 */
604int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay,
605 struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr,
606 gfp_t gfp_flags)
607{
608 struct objlayout_deviceinfo *odi;
609 struct pnfs_device pd;
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300610 struct page *page, **pages;
611 u32 *p;
612 int err;
613
614 page = alloc_page(gfp_flags);
615 if (!page)
616 return -ENOMEM;
617
618 pages = &page;
619 pd.pages = pages;
620
621 memcpy(&pd.dev_id, d_id, sizeof(*d_id));
622 pd.layout_type = LAYOUT_OSD2_OBJECTS;
623 pd.pages = &page;
624 pd.pgbase = 0;
625 pd.pglen = PAGE_SIZE;
626 pd.mincount = 0;
627
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300628 err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd);
629 dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err);
630 if (err)
631 goto err_out;
632
633 p = page_address(page);
634 odi = kzalloc(sizeof(*odi), gfp_flags);
635 if (!odi) {
636 err = -ENOMEM;
637 goto err_out;
638 }
639 pnfs_osd_xdr_decode_deviceaddr(&odi->da, p);
640 odi->page = page;
641 *deviceaddr = &odi->da;
642 return 0;
643
644err_out:
645 __free_page(page);
646 return err;
647}
648
649void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr)
650{
651 struct objlayout_deviceinfo *odi = container_of(deviceaddr,
652 struct objlayout_deviceinfo,
653 da);
654
655 __free_page(odi->page);
656 kfree(odi);
657}
Sachin Bhamare18d98f62012-03-19 20:47:58 -0700658
659enum {
660 OBJLAYOUT_MAX_URI_LEN = 256, OBJLAYOUT_MAX_OSDNAME_LEN = 64,
661 OBJLAYOUT_MAX_SYSID_HEX_LEN = OSD_SYSTEMID_LEN * 2 + 1,
662 OSD_LOGIN_UPCALL_PATHLEN = 256
663};
664
665static char osd_login_prog[OSD_LOGIN_UPCALL_PATHLEN] = "/sbin/osd_login";
666
667module_param_string(osd_login_prog, osd_login_prog, sizeof(osd_login_prog),
668 0600);
669MODULE_PARM_DESC(osd_login_prog, "Path to the osd_login upcall program");
670
671struct __auto_login {
672 char uri[OBJLAYOUT_MAX_URI_LEN];
673 char osdname[OBJLAYOUT_MAX_OSDNAME_LEN];
674 char systemid_hex[OBJLAYOUT_MAX_SYSID_HEX_LEN];
675};
676
677static int __objlayout_upcall(struct __auto_login *login)
678{
679 static char *envp[] = { "HOME=/",
680 "TERM=linux",
681 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
682 NULL
683 };
684 char *argv[8];
685 int ret;
686
687 if (unlikely(!osd_login_prog[0])) {
688 dprintk("%s: osd_login_prog is disabled\n", __func__);
689 return -EACCES;
690 }
691
692 dprintk("%s uri: %s\n", __func__, login->uri);
693 dprintk("%s osdname %s\n", __func__, login->osdname);
694 dprintk("%s systemid_hex %s\n", __func__, login->systemid_hex);
695
696 argv[0] = (char *)osd_login_prog;
697 argv[1] = "-u";
698 argv[2] = login->uri;
699 argv[3] = "-o";
700 argv[4] = login->osdname;
701 argv[5] = "-s";
702 argv[6] = login->systemid_hex;
703 argv[7] = NULL;
704
705 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
706 /*
707 * Disable the upcall mechanism if we're getting an ENOENT or
708 * EACCES error. The admin can re-enable it on the fly by using
709 * sysfs to set the objlayoutdriver.osd_login_prog module parameter once
710 * the problem has been fixed.
711 */
712 if (ret == -ENOENT || ret == -EACCES) {
713 printk(KERN_ERR "PNFS-OBJ: %s was not found please set "
714 "objlayoutdriver.osd_login_prog kernel parameter!\n",
715 osd_login_prog);
716 osd_login_prog[0] = '\0';
717 }
718 dprintk("%s %s return value: %d\n", __func__, osd_login_prog, ret);
719
720 return ret;
721}
722
723/* Assume dest is all zeros */
724static void __copy_nfsS_and_zero_terminate(struct nfs4_string s,
725 char *dest, int max_len,
726 const char *var_name)
727{
728 if (!s.len)
729 return;
730
731 if (s.len >= max_len) {
732 pr_warn_ratelimited(
733 "objlayout_autologin: %s: s.len(%d) >= max_len(%d)",
734 var_name, s.len, max_len);
735 s.len = max_len - 1; /* space for null terminator */
736 }
737
738 memcpy(dest, s.data, s.len);
739}
740
741/* Assume sysid is all zeros */
742static void _sysid_2_hex(struct nfs4_string s,
743 char sysid[OBJLAYOUT_MAX_SYSID_HEX_LEN])
744{
745 int i;
746 char *cur;
747
748 if (!s.len)
749 return;
750
751 if (s.len != OSD_SYSTEMID_LEN) {
752 pr_warn_ratelimited(
753 "objlayout_autologin: systemid_len(%d) != OSD_SYSTEMID_LEN",
754 s.len);
755 if (s.len > OSD_SYSTEMID_LEN)
756 s.len = OSD_SYSTEMID_LEN;
757 }
758
759 cur = sysid;
760 for (i = 0; i < s.len; i++)
761 cur = hex_byte_pack(cur, s.data[i]);
762}
763
764int objlayout_autologin(struct pnfs_osd_deviceaddr *deviceaddr)
765{
766 int rc;
767 struct __auto_login login;
768
769 if (!deviceaddr->oda_targetaddr.ota_netaddr.r_addr.len)
770 return -ENODEV;
771
772 memset(&login, 0, sizeof(login));
773 __copy_nfsS_and_zero_terminate(
774 deviceaddr->oda_targetaddr.ota_netaddr.r_addr,
775 login.uri, sizeof(login.uri), "URI");
776
777 __copy_nfsS_and_zero_terminate(
778 deviceaddr->oda_osdname,
779 login.osdname, sizeof(login.osdname), "OSDNAME");
780
781 _sysid_2_hex(deviceaddr->oda_systemid, login.systemid_hex);
782
783 rc = __objlayout_upcall(&login);
784 if (rc > 0) /* script returns positive values */
785 rc = -ENODEV;
786
787 return rc;
788}