blob: 8d45f1c318ce40ac453b7b4a71288e71ba3c6a34 [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
261 rdata->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{
282 loff_t offset = rdata->args.offset;
283 size_t count = rdata->args.count;
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700284 int err;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300285 loff_t eof;
286
Boaz Harrosh04f83452011-05-22 19:52:19 +0300287 eof = i_size_read(rdata->inode);
288 if (unlikely(offset + count > eof)) {
289 if (offset >= eof) {
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700290 err = 0;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300291 rdata->res.count = 0;
292 rdata->res.eof = 1;
Boaz Harrosh4cdc6852011-10-31 14:45:06 -0700293 /*FIXME: do we need to call pnfs_ld_read_done() */
Boaz Harrosh04f83452011-05-22 19:52:19 +0300294 goto out;
295 }
296 count = eof - offset;
297 }
298
Boaz Harrosh4cdc6852011-10-31 14:45:06 -0700299 rdata->res.eof = (offset + count) >= eof;
Boaz Harrosh96218552011-10-31 14:47:32 -0700300 _fix_verify_io_params(rdata->lseg, &rdata->args.pages,
301 &rdata->args.pgbase,
302 rdata->args.offset, rdata->args.count);
Boaz Harrosh4cdc6852011-10-31 14:45:06 -0700303
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700304 dprintk("%s: inode(%lx) offset 0x%llx count 0x%Zx eof=%d\n",
305 __func__, rdata->inode->i_ino, offset, count, rdata->res.eof);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300306
Boaz Harrosh96218552011-10-31 14:47:32 -0700307 err = objio_read_pagelist(rdata);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300308 out:
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700309 if (unlikely(err)) {
310 rdata->pnfs_error = err;
311 dprintk("%s: Returned Error %d\n", __func__, err);
312 return PNFS_NOT_ATTEMPTED;
313 }
Boaz Harrosh04f83452011-05-22 19:52:19 +0300314 return PNFS_ATTEMPTED;
315}
316
317/* Function scheduled on rpc workqueue to call ->nfs_writelist_complete().
318 * This is because the osd completion is called with ints-off from
319 * the block layer
320 */
321static void _rpc_write_complete(struct work_struct *work)
322{
323 struct rpc_task *task;
324 struct nfs_write_data *wdata;
325
326 dprintk("%s enter\n", __func__);
327 task = container_of(work, struct rpc_task, u.tk_work);
328 wdata = container_of(task, struct nfs_write_data, task);
329
330 pnfs_ld_write_done(wdata);
331}
332
333void
Boaz Harroshe2e04352011-10-31 15:03:35 -0700334objlayout_write_done(struct objlayout_io_res *oir, ssize_t status, bool sync)
Boaz Harrosh04f83452011-05-22 19:52:19 +0300335{
Boaz Harroshe2e04352011-10-31 15:03:35 -0700336 struct nfs_write_data *wdata = oir->rpcdata;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300337
Boaz Harroshe2e04352011-10-31 15:03:35 -0700338 oir->status = wdata->task.tk_status = status;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300339 if (status >= 0) {
340 wdata->res.count = status;
Boaz Harroshe2e04352011-10-31 15:03:35 -0700341 wdata->verf.committed = oir->committed;
Boaz Harrosh5c0b4122012-01-06 09:28:12 +0200342 } else {
343 wdata->pnfs_error = status;
Boaz Harrosh96218552011-10-31 14:47:32 -0700344 }
Boaz Harroshe2e04352011-10-31 15:03:35 -0700345 objlayout_iodone(oir);
Boaz Harrosh96218552011-10-31 14:47:32 -0700346 /* must not use oir after this point */
347
348 dprintk("%s: Return status %zd committed %d sync=%d\n", __func__,
349 status, wdata->verf.committed, sync);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300350
351 if (sync)
352 pnfs_ld_write_done(wdata);
353 else {
354 INIT_WORK(&wdata->task.u.tk_work, _rpc_write_complete);
355 schedule_work(&wdata->task.u.tk_work);
356 }
357}
358
359/*
360 * Perform sync or async writes.
361 */
362enum pnfs_try_status
363objlayout_write_pagelist(struct nfs_write_data *wdata,
364 int how)
365{
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700366 int err;
Boaz Harrosh04f83452011-05-22 19:52:19 +0300367
Boaz Harrosh96218552011-10-31 14:47:32 -0700368 _fix_verify_io_params(wdata->lseg, &wdata->args.pages,
369 &wdata->args.pgbase,
370 wdata->args.offset, wdata->args.count);
Boaz Harrosh04f83452011-05-22 19:52:19 +0300371
Boaz Harrosh96218552011-10-31 14:47:32 -0700372 err = objio_write_pagelist(wdata, how);
Boaz Harroshe6c40fe2011-10-31 14:45:46 -0700373 if (unlikely(err)) {
374 wdata->pnfs_error = err;
375 dprintk("%s: Returned Error %d\n", __func__, err);
376 return PNFS_NOT_ATTEMPTED;
377 }
Boaz Harrosh04f83452011-05-22 19:52:19 +0300378 return PNFS_ATTEMPTED;
379}
380
Boaz Harrosha0fe8bf2011-05-22 19:54:13 +0300381void
382objlayout_encode_layoutcommit(struct pnfs_layout_hdr *pnfslay,
383 struct xdr_stream *xdr,
384 const struct nfs4_layoutcommit_args *args)
385{
386 struct objlayout *objlay = OBJLAYOUT(pnfslay);
387 struct pnfs_osd_layoutupdate lou;
388 __be32 *start;
389
390 dprintk("%s: Begin\n", __func__);
391
392 spin_lock(&objlay->lock);
393 lou.dsu_valid = (objlay->delta_space_valid == OBJ_DSU_VALID);
394 lou.dsu_delta = objlay->delta_space_used;
395 objlay->delta_space_used = 0;
396 objlay->delta_space_valid = OBJ_DSU_INIT;
397 lou.olu_ioerr_flag = !list_empty(&objlay->err_list);
398 spin_unlock(&objlay->lock);
399
400 start = xdr_reserve_space(xdr, 4);
401
402 BUG_ON(pnfs_osd_xdr_encode_layoutupdate(xdr, &lou));
403
404 *start = cpu_to_be32((xdr->p - start - 1) * 4);
405
406 dprintk("%s: Return delta_space_used %lld err %d\n", __func__,
407 lou.dsu_delta, lou.olu_ioerr_flag);
408}
409
Boaz Harroshadb58532011-05-26 21:49:46 +0300410static int
411err_prio(u32 oer_errno)
412{
413 switch (oer_errno) {
414 case 0:
415 return 0;
416
417 case PNFS_OSD_ERR_RESOURCE:
418 return OSD_ERR_PRI_RESOURCE;
419 case PNFS_OSD_ERR_BAD_CRED:
420 return OSD_ERR_PRI_BAD_CRED;
421 case PNFS_OSD_ERR_NO_ACCESS:
422 return OSD_ERR_PRI_NO_ACCESS;
423 case PNFS_OSD_ERR_UNREACHABLE:
424 return OSD_ERR_PRI_UNREACHABLE;
425 case PNFS_OSD_ERR_NOT_FOUND:
426 return OSD_ERR_PRI_NOT_FOUND;
427 case PNFS_OSD_ERR_NO_SPACE:
428 return OSD_ERR_PRI_NO_SPACE;
429 default:
430 WARN_ON(1);
431 /* fallthrough */
432 case PNFS_OSD_ERR_EIO:
433 return OSD_ERR_PRI_EIO;
434 }
435}
436
437static void
438merge_ioerr(struct pnfs_osd_ioerr *dest_err,
439 const struct pnfs_osd_ioerr *src_err)
440{
441 u64 dest_end, src_end;
442
443 if (!dest_err->oer_errno) {
444 *dest_err = *src_err;
445 /* accumulated device must be blank */
446 memset(&dest_err->oer_component.oid_device_id, 0,
447 sizeof(dest_err->oer_component.oid_device_id));
448
449 return;
450 }
451
452 if (dest_err->oer_component.oid_partition_id !=
453 src_err->oer_component.oid_partition_id)
454 dest_err->oer_component.oid_partition_id = 0;
455
456 if (dest_err->oer_component.oid_object_id !=
457 src_err->oer_component.oid_object_id)
458 dest_err->oer_component.oid_object_id = 0;
459
460 if (dest_err->oer_comp_offset > src_err->oer_comp_offset)
461 dest_err->oer_comp_offset = src_err->oer_comp_offset;
462
463 dest_end = end_offset(dest_err->oer_comp_offset,
464 dest_err->oer_comp_length);
465 src_end = end_offset(src_err->oer_comp_offset,
466 src_err->oer_comp_length);
467 if (dest_end < src_end)
468 dest_end = src_end;
469
470 dest_err->oer_comp_length = dest_end - dest_err->oer_comp_offset;
471
472 if ((src_err->oer_iswrite == dest_err->oer_iswrite) &&
473 (err_prio(src_err->oer_errno) > err_prio(dest_err->oer_errno))) {
474 dest_err->oer_errno = src_err->oer_errno;
475 } else if (src_err->oer_iswrite) {
476 dest_err->oer_iswrite = true;
477 dest_err->oer_errno = src_err->oer_errno;
478 }
479}
480
481static void
482encode_accumulated_error(struct objlayout *objlay, __be32 *p)
483{
Boaz Harroshe2e04352011-10-31 15:03:35 -0700484 struct objlayout_io_res *oir, *tmp;
Boaz Harroshadb58532011-05-26 21:49:46 +0300485 struct pnfs_osd_ioerr accumulated_err = {.oer_errno = 0};
486
Boaz Harroshe2e04352011-10-31 15:03:35 -0700487 list_for_each_entry_safe(oir, tmp, &objlay->err_list, err_list) {
Boaz Harroshadb58532011-05-26 21:49:46 +0300488 unsigned i;
489
Boaz Harroshe2e04352011-10-31 15:03:35 -0700490 for (i = 0; i < oir->num_comps; i++) {
491 struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[i];
Boaz Harroshadb58532011-05-26 21:49:46 +0300492
493 if (!ioerr->oer_errno)
494 continue;
495
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500496 printk(KERN_ERR "NFS: %s: err[%d]: errno=%d "
497 "is_write=%d dev(%llx:%llx) par=0x%llx "
498 "obj=0x%llx offset=0x%llx length=0x%llx\n",
Boaz Harroshadb58532011-05-26 21:49:46 +0300499 __func__, i, ioerr->oer_errno,
500 ioerr->oer_iswrite,
501 _DEVID_LO(&ioerr->oer_component.oid_device_id),
502 _DEVID_HI(&ioerr->oer_component.oid_device_id),
503 ioerr->oer_component.oid_partition_id,
504 ioerr->oer_component.oid_object_id,
505 ioerr->oer_comp_offset,
506 ioerr->oer_comp_length);
507
508 merge_ioerr(&accumulated_err, ioerr);
509 }
Boaz Harroshe2e04352011-10-31 15:03:35 -0700510 list_del(&oir->err_list);
511 objio_free_result(oir);
Boaz Harroshadb58532011-05-26 21:49:46 +0300512 }
513
514 pnfs_osd_xdr_encode_ioerr(p, &accumulated_err);
515}
516
517void
518objlayout_encode_layoutreturn(struct pnfs_layout_hdr *pnfslay,
519 struct xdr_stream *xdr,
520 const struct nfs4_layoutreturn_args *args)
521{
522 struct objlayout *objlay = OBJLAYOUT(pnfslay);
Boaz Harroshe2e04352011-10-31 15:03:35 -0700523 struct objlayout_io_res *oir, *tmp;
Boaz Harroshadb58532011-05-26 21:49:46 +0300524 __be32 *start;
525
526 dprintk("%s: Begin\n", __func__);
527 start = xdr_reserve_space(xdr, 4);
528 BUG_ON(!start);
529
530 spin_lock(&objlay->lock);
531
Boaz Harroshe2e04352011-10-31 15:03:35 -0700532 list_for_each_entry_safe(oir, tmp, &objlay->err_list, err_list) {
Boaz Harroshadb58532011-05-26 21:49:46 +0300533 __be32 *last_xdr = NULL, *p;
534 unsigned i;
535 int res = 0;
536
Boaz Harroshe2e04352011-10-31 15:03:35 -0700537 for (i = 0; i < oir->num_comps; i++) {
538 struct pnfs_osd_ioerr *ioerr = &oir->ioerrs[i];
Boaz Harroshadb58532011-05-26 21:49:46 +0300539
540 if (!ioerr->oer_errno)
541 continue;
542
543 dprintk("%s: err[%d]: errno=%d is_write=%d "
544 "dev(%llx:%llx) par=0x%llx obj=0x%llx "
545 "offset=0x%llx length=0x%llx\n",
546 __func__, i, ioerr->oer_errno,
547 ioerr->oer_iswrite,
548 _DEVID_LO(&ioerr->oer_component.oid_device_id),
549 _DEVID_HI(&ioerr->oer_component.oid_device_id),
550 ioerr->oer_component.oid_partition_id,
551 ioerr->oer_component.oid_object_id,
552 ioerr->oer_comp_offset,
553 ioerr->oer_comp_length);
554
555 p = pnfs_osd_xdr_ioerr_reserve_space(xdr);
556 if (unlikely(!p)) {
557 res = -E2BIG;
558 break; /* accumulated_error */
559 }
560
561 last_xdr = p;
Boaz Harroshe2e04352011-10-31 15:03:35 -0700562 pnfs_osd_xdr_encode_ioerr(p, &oir->ioerrs[i]);
Boaz Harroshadb58532011-05-26 21:49:46 +0300563 }
564
565 /* TODO: use xdr_write_pages */
566 if (unlikely(res)) {
567 /* no space for even one error descriptor */
568 BUG_ON(!last_xdr);
569
570 /* we've encountered a situation with lots and lots of
571 * errors and no space to encode them all. Use the last
572 * available slot to report the union of all the
573 * remaining errors.
574 */
575 encode_accumulated_error(objlay, last_xdr);
576 goto loop_done;
577 }
Boaz Harroshe2e04352011-10-31 15:03:35 -0700578 list_del(&oir->err_list);
579 objio_free_result(oir);
Boaz Harroshadb58532011-05-26 21:49:46 +0300580 }
581loop_done:
582 spin_unlock(&objlay->lock);
583
584 *start = cpu_to_be32((xdr->p - start - 1) * 4);
585 dprintk("%s: Return\n", __func__);
586}
587
588
Boaz Harrosh04f83452011-05-22 19:52:19 +0300589/*
Boaz Harroshb6c05f12011-05-26 21:45:34 +0300590 * Get Device Info API for io engines
591 */
592struct objlayout_deviceinfo {
593 struct page *page;
594 struct pnfs_osd_deviceaddr da; /* This must be last */
595};
596
597/* Initialize and call nfs_getdeviceinfo, then decode and return a
598 * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo()
599 * should be called.
600 */
601int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay,
602 struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr,
603 gfp_t gfp_flags)
604{
605 struct objlayout_deviceinfo *odi;
606 struct pnfs_device pd;
607 struct super_block *sb;
608 struct page *page, **pages;
609 u32 *p;
610 int err;
611
612 page = alloc_page(gfp_flags);
613 if (!page)
614 return -ENOMEM;
615
616 pages = &page;
617 pd.pages = pages;
618
619 memcpy(&pd.dev_id, d_id, sizeof(*d_id));
620 pd.layout_type = LAYOUT_OSD2_OBJECTS;
621 pd.pages = &page;
622 pd.pgbase = 0;
623 pd.pglen = PAGE_SIZE;
624 pd.mincount = 0;
625
626 sb = pnfslay->plh_inode->i_sb;
627 err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd);
628 dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err);
629 if (err)
630 goto err_out;
631
632 p = page_address(page);
633 odi = kzalloc(sizeof(*odi), gfp_flags);
634 if (!odi) {
635 err = -ENOMEM;
636 goto err_out;
637 }
638 pnfs_osd_xdr_decode_deviceaddr(&odi->da, p);
639 odi->page = page;
640 *deviceaddr = &odi->da;
641 return 0;
642
643err_out:
644 __free_page(page);
645 return err;
646}
647
648void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr)
649{
650 struct objlayout_deviceinfo *odi = container_of(deviceaddr,
651 struct objlayout_deviceinfo,
652 da);
653
654 __free_page(odi->page);
655 kfree(odi);
656}
Sachin Bhamare18d98f62012-03-19 20:47:58 -0700657
658enum {
659 OBJLAYOUT_MAX_URI_LEN = 256, OBJLAYOUT_MAX_OSDNAME_LEN = 64,
660 OBJLAYOUT_MAX_SYSID_HEX_LEN = OSD_SYSTEMID_LEN * 2 + 1,
661 OSD_LOGIN_UPCALL_PATHLEN = 256
662};
663
664static char osd_login_prog[OSD_LOGIN_UPCALL_PATHLEN] = "/sbin/osd_login";
665
666module_param_string(osd_login_prog, osd_login_prog, sizeof(osd_login_prog),
667 0600);
668MODULE_PARM_DESC(osd_login_prog, "Path to the osd_login upcall program");
669
670struct __auto_login {
671 char uri[OBJLAYOUT_MAX_URI_LEN];
672 char osdname[OBJLAYOUT_MAX_OSDNAME_LEN];
673 char systemid_hex[OBJLAYOUT_MAX_SYSID_HEX_LEN];
674};
675
676static int __objlayout_upcall(struct __auto_login *login)
677{
678 static char *envp[] = { "HOME=/",
679 "TERM=linux",
680 "PATH=/sbin:/usr/sbin:/bin:/usr/bin",
681 NULL
682 };
683 char *argv[8];
684 int ret;
685
686 if (unlikely(!osd_login_prog[0])) {
687 dprintk("%s: osd_login_prog is disabled\n", __func__);
688 return -EACCES;
689 }
690
691 dprintk("%s uri: %s\n", __func__, login->uri);
692 dprintk("%s osdname %s\n", __func__, login->osdname);
693 dprintk("%s systemid_hex %s\n", __func__, login->systemid_hex);
694
695 argv[0] = (char *)osd_login_prog;
696 argv[1] = "-u";
697 argv[2] = login->uri;
698 argv[3] = "-o";
699 argv[4] = login->osdname;
700 argv[5] = "-s";
701 argv[6] = login->systemid_hex;
702 argv[7] = NULL;
703
704 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
705 /*
706 * Disable the upcall mechanism if we're getting an ENOENT or
707 * EACCES error. The admin can re-enable it on the fly by using
708 * sysfs to set the objlayoutdriver.osd_login_prog module parameter once
709 * the problem has been fixed.
710 */
711 if (ret == -ENOENT || ret == -EACCES) {
712 printk(KERN_ERR "PNFS-OBJ: %s was not found please set "
713 "objlayoutdriver.osd_login_prog kernel parameter!\n",
714 osd_login_prog);
715 osd_login_prog[0] = '\0';
716 }
717 dprintk("%s %s return value: %d\n", __func__, osd_login_prog, ret);
718
719 return ret;
720}
721
722/* Assume dest is all zeros */
723static void __copy_nfsS_and_zero_terminate(struct nfs4_string s,
724 char *dest, int max_len,
725 const char *var_name)
726{
727 if (!s.len)
728 return;
729
730 if (s.len >= max_len) {
731 pr_warn_ratelimited(
732 "objlayout_autologin: %s: s.len(%d) >= max_len(%d)",
733 var_name, s.len, max_len);
734 s.len = max_len - 1; /* space for null terminator */
735 }
736
737 memcpy(dest, s.data, s.len);
738}
739
740/* Assume sysid is all zeros */
741static void _sysid_2_hex(struct nfs4_string s,
742 char sysid[OBJLAYOUT_MAX_SYSID_HEX_LEN])
743{
744 int i;
745 char *cur;
746
747 if (!s.len)
748 return;
749
750 if (s.len != OSD_SYSTEMID_LEN) {
751 pr_warn_ratelimited(
752 "objlayout_autologin: systemid_len(%d) != OSD_SYSTEMID_LEN",
753 s.len);
754 if (s.len > OSD_SYSTEMID_LEN)
755 s.len = OSD_SYSTEMID_LEN;
756 }
757
758 cur = sysid;
759 for (i = 0; i < s.len; i++)
760 cur = hex_byte_pack(cur, s.data[i]);
761}
762
763int objlayout_autologin(struct pnfs_osd_deviceaddr *deviceaddr)
764{
765 int rc;
766 struct __auto_login login;
767
768 if (!deviceaddr->oda_targetaddr.ota_netaddr.r_addr.len)
769 return -ENODEV;
770
771 memset(&login, 0, sizeof(login));
772 __copy_nfsS_and_zero_terminate(
773 deviceaddr->oda_targetaddr.ota_netaddr.r_addr,
774 login.uri, sizeof(login.uri), "URI");
775
776 __copy_nfsS_and_zero_terminate(
777 deviceaddr->oda_osdname,
778 login.osdname, sizeof(login.osdname), "OSDNAME");
779
780 _sysid_2_hex(deviceaddr->oda_systemid, login.systemid_hex);
781
782 rc = __objlayout_upcall(&login);
783 if (rc > 0) /* script returns positive values */
784 rc = -ENODEV;
785
786 return rc;
787}