Boaz Harrosh | 09f5bf4 | 2011-05-22 19:50:20 +0300 | [diff] [blame] | 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 | /* |
Benny Halevy | e51b841 | 2011-05-22 19:51:48 +0300 | [diff] [blame] | 45 | * Create a objlayout layout structure for the given inode and return it. |
| 46 | */ |
| 47 | struct pnfs_layout_hdr * |
| 48 | objlayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags) |
| 49 | { |
| 50 | struct objlayout *objlay; |
| 51 | |
| 52 | objlay = kzalloc(sizeof(struct objlayout), gfp_flags); |
Boaz Harrosh | adb5853 | 2011-05-26 21:49:46 +0300 | [diff] [blame] | 53 | if (objlay) { |
| 54 | spin_lock_init(&objlay->lock); |
| 55 | INIT_LIST_HEAD(&objlay->err_list); |
| 56 | } |
Benny Halevy | e51b841 | 2011-05-22 19:51:48 +0300 | [diff] [blame] | 57 | dprintk("%s: Return %p\n", __func__, objlay); |
| 58 | return &objlay->pnfs_layout; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Free an objlayout layout structure |
| 63 | */ |
| 64 | void |
| 65 | objlayout_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 | |
Boaz Harrosh | adb5853 | 2011-05-26 21:49:46 +0300 | [diff] [blame] | 71 | WARN_ON(!list_empty(&objlay->err_list)); |
Benny Halevy | e51b841 | 2011-05-22 19:51:48 +0300 | [diff] [blame] | 72 | kfree(objlay); |
| 73 | } |
| 74 | |
| 75 | /* |
Boaz Harrosh | 09f5bf4 | 2011-05-22 19:50:20 +0300 | [diff] [blame] | 76 | * Unmarshall layout and store it in pnfslay. |
| 77 | */ |
| 78 | struct pnfs_layout_segment * |
| 79 | objlayout_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 | |
| 115 | err: |
| 116 | __free_page(scratch); |
| 117 | err_nofree: |
| 118 | dprintk("%s: Err Return=>%d\n", __func__, status); |
| 119 | return ERR_PTR(status); |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * Free a layout segement |
| 124 | */ |
| 125 | void |
| 126 | objlayout_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 | |
Boaz Harrosh | b6c05f1 | 2011-05-26 21:45:34 +0300 | [diff] [blame] | 136 | /* |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 137 | * I/O Operations |
| 138 | */ |
| 139 | static inline u64 |
| 140 | end_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 */ |
| 149 | static inline u64 |
| 150 | last_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 | |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 159 | void _fix_verify_io_params(struct pnfs_layout_segment *lseg, |
| 160 | struct page ***p_pages, unsigned *p_pgbase, |
| 161 | u64 offset, unsigned long count) |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 162 | { |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 163 | u64 lseg_end_offset; |
| 164 | |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 165 | BUG_ON(offset < lseg->pls_range.offset); |
| 166 | lseg_end_offset = end_offset(lseg->pls_range.offset, |
| 167 | lseg->pls_range.length); |
| 168 | BUG_ON(offset >= lseg_end_offset); |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 169 | WARN_ON(offset + count > lseg_end_offset); |
| 170 | |
| 171 | if (*p_pgbase > PAGE_SIZE) { |
| 172 | dprintk("%s: pgbase(0x%x) > PAGE_SIZE\n", __func__, *p_pgbase); |
| 173 | *p_pages += *p_pgbase >> PAGE_SHIFT; |
| 174 | *p_pgbase &= ~PAGE_MASK; |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 175 | } |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* |
| 179 | * I/O done common code |
| 180 | */ |
| 181 | static void |
| 182 | objlayout_iodone(struct objlayout_io_state *state) |
| 183 | { |
Boaz Harrosh | adb5853 | 2011-05-26 21:49:46 +0300 | [diff] [blame] | 184 | if (likely(state->status >= 0)) { |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 185 | objio_free_result(state); |
Boaz Harrosh | adb5853 | 2011-05-26 21:49:46 +0300 | [diff] [blame] | 186 | } else { |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 187 | struct objlayout *objlay = state->objlay; |
Boaz Harrosh | adb5853 | 2011-05-26 21:49:46 +0300 | [diff] [blame] | 188 | |
| 189 | spin_lock(&objlay->lock); |
Boaz Harrosh | a0fe8bf | 2011-05-22 19:54:13 +0300 | [diff] [blame] | 190 | objlay->delta_space_valid = OBJ_DSU_INVALID; |
Boaz Harrosh | adb5853 | 2011-05-26 21:49:46 +0300 | [diff] [blame] | 191 | list_add(&objlay->err_list, &state->err_list); |
| 192 | spin_unlock(&objlay->lock); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * objlayout_io_set_result - Set an osd_error code on a specific osd comp. |
| 198 | * |
| 199 | * The @index component IO failed (error returned from target). Register |
| 200 | * the error for later reporting at layout-return. |
| 201 | */ |
| 202 | void |
| 203 | objlayout_io_set_result(struct objlayout_io_state *state, unsigned index, |
| 204 | struct pnfs_osd_objid *pooid, int osd_error, |
| 205 | u64 offset, u64 length, bool is_write) |
| 206 | { |
| 207 | struct pnfs_osd_ioerr *ioerr = &state->ioerrs[index]; |
| 208 | |
| 209 | BUG_ON(index >= state->num_comps); |
| 210 | if (osd_error) { |
| 211 | ioerr->oer_component = *pooid; |
| 212 | ioerr->oer_comp_offset = offset; |
| 213 | ioerr->oer_comp_length = length; |
| 214 | ioerr->oer_iswrite = is_write; |
| 215 | ioerr->oer_errno = osd_error; |
| 216 | |
| 217 | dprintk("%s: err[%d]: errno=%d is_write=%d dev(%llx:%llx) " |
| 218 | "par=0x%llx obj=0x%llx offset=0x%llx length=0x%llx\n", |
| 219 | __func__, index, ioerr->oer_errno, |
| 220 | ioerr->oer_iswrite, |
| 221 | _DEVID_LO(&ioerr->oer_component.oid_device_id), |
| 222 | _DEVID_HI(&ioerr->oer_component.oid_device_id), |
| 223 | ioerr->oer_component.oid_partition_id, |
| 224 | ioerr->oer_component.oid_object_id, |
| 225 | ioerr->oer_comp_offset, |
| 226 | ioerr->oer_comp_length); |
| 227 | } else { |
| 228 | /* User need not call if no error is reported */ |
| 229 | ioerr->oer_errno = 0; |
| 230 | } |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* Function scheduled on rpc workqueue to call ->nfs_readlist_complete(). |
| 234 | * This is because the osd completion is called with ints-off from |
| 235 | * the block layer |
| 236 | */ |
| 237 | static void _rpc_read_complete(struct work_struct *work) |
| 238 | { |
| 239 | struct rpc_task *task; |
| 240 | struct nfs_read_data *rdata; |
| 241 | |
| 242 | dprintk("%s enter\n", __func__); |
| 243 | task = container_of(work, struct rpc_task, u.tk_work); |
| 244 | rdata = container_of(task, struct nfs_read_data, task); |
| 245 | |
| 246 | pnfs_ld_read_done(rdata); |
| 247 | } |
| 248 | |
| 249 | void |
| 250 | objlayout_read_done(struct objlayout_io_state *state, ssize_t status, bool sync) |
| 251 | { |
Boaz Harrosh | 4cdc685 | 2011-10-31 14:45:06 -0700 | [diff] [blame] | 252 | struct nfs_read_data *rdata = state->rpcdata; |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 253 | |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 254 | state->status = rdata->task.tk_status = status; |
Boaz Harrosh | 4cdc685 | 2011-10-31 14:45:06 -0700 | [diff] [blame] | 255 | if (status >= 0) |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 256 | rdata->res.count = status; |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 257 | objlayout_iodone(state); |
| 258 | /* must not use state after this point */ |
| 259 | |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 260 | dprintk("%s: Return status=%zd eof=%d sync=%d\n", __func__, |
| 261 | status, rdata->res.eof, sync); |
| 262 | |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 263 | if (sync) |
| 264 | pnfs_ld_read_done(rdata); |
| 265 | else { |
| 266 | INIT_WORK(&rdata->task.u.tk_work, _rpc_read_complete); |
| 267 | schedule_work(&rdata->task.u.tk_work); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * Perform sync or async reads. |
| 273 | */ |
| 274 | enum pnfs_try_status |
| 275 | objlayout_read_pagelist(struct nfs_read_data *rdata) |
| 276 | { |
| 277 | loff_t offset = rdata->args.offset; |
| 278 | size_t count = rdata->args.count; |
Boaz Harrosh | e6c40fe | 2011-10-31 14:45:46 -0700 | [diff] [blame] | 279 | int err; |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 280 | loff_t eof; |
| 281 | |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 282 | eof = i_size_read(rdata->inode); |
| 283 | if (unlikely(offset + count > eof)) { |
| 284 | if (offset >= eof) { |
Boaz Harrosh | e6c40fe | 2011-10-31 14:45:46 -0700 | [diff] [blame] | 285 | err = 0; |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 286 | rdata->res.count = 0; |
| 287 | rdata->res.eof = 1; |
Boaz Harrosh | 4cdc685 | 2011-10-31 14:45:06 -0700 | [diff] [blame] | 288 | /*FIXME: do we need to call pnfs_ld_read_done() */ |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 289 | goto out; |
| 290 | } |
| 291 | count = eof - offset; |
| 292 | } |
| 293 | |
Boaz Harrosh | 4cdc685 | 2011-10-31 14:45:06 -0700 | [diff] [blame] | 294 | rdata->res.eof = (offset + count) >= eof; |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 295 | _fix_verify_io_params(rdata->lseg, &rdata->args.pages, |
| 296 | &rdata->args.pgbase, |
| 297 | rdata->args.offset, rdata->args.count); |
Boaz Harrosh | 4cdc685 | 2011-10-31 14:45:06 -0700 | [diff] [blame] | 298 | |
Boaz Harrosh | e6c40fe | 2011-10-31 14:45:46 -0700 | [diff] [blame] | 299 | dprintk("%s: inode(%lx) offset 0x%llx count 0x%Zx eof=%d\n", |
| 300 | __func__, rdata->inode->i_ino, offset, count, rdata->res.eof); |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 301 | |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 302 | err = objio_read_pagelist(rdata); |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 303 | out: |
Boaz Harrosh | e6c40fe | 2011-10-31 14:45:46 -0700 | [diff] [blame] | 304 | if (unlikely(err)) { |
| 305 | rdata->pnfs_error = err; |
| 306 | dprintk("%s: Returned Error %d\n", __func__, err); |
| 307 | return PNFS_NOT_ATTEMPTED; |
| 308 | } |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 309 | return PNFS_ATTEMPTED; |
| 310 | } |
| 311 | |
| 312 | /* Function scheduled on rpc workqueue to call ->nfs_writelist_complete(). |
| 313 | * This is because the osd completion is called with ints-off from |
| 314 | * the block layer |
| 315 | */ |
| 316 | static void _rpc_write_complete(struct work_struct *work) |
| 317 | { |
| 318 | struct rpc_task *task; |
| 319 | struct nfs_write_data *wdata; |
| 320 | |
| 321 | dprintk("%s enter\n", __func__); |
| 322 | task = container_of(work, struct rpc_task, u.tk_work); |
| 323 | wdata = container_of(task, struct nfs_write_data, task); |
| 324 | |
| 325 | pnfs_ld_write_done(wdata); |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | objlayout_write_done(struct objlayout_io_state *state, ssize_t status, |
| 330 | bool sync) |
| 331 | { |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 332 | struct nfs_write_data *wdata = state->rpcdata; |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 333 | |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 334 | state->status = wdata->task.tk_status = status; |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 335 | if (status >= 0) { |
| 336 | wdata->res.count = status; |
| 337 | wdata->verf.committed = state->committed; |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 338 | } |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 339 | objlayout_iodone(state); |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 340 | /* must not use oir after this point */ |
| 341 | |
| 342 | dprintk("%s: Return status %zd committed %d sync=%d\n", __func__, |
| 343 | status, wdata->verf.committed, sync); |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 344 | |
| 345 | if (sync) |
| 346 | pnfs_ld_write_done(wdata); |
| 347 | else { |
| 348 | INIT_WORK(&wdata->task.u.tk_work, _rpc_write_complete); |
| 349 | schedule_work(&wdata->task.u.tk_work); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Perform sync or async writes. |
| 355 | */ |
| 356 | enum pnfs_try_status |
| 357 | objlayout_write_pagelist(struct nfs_write_data *wdata, |
| 358 | int how) |
| 359 | { |
Boaz Harrosh | e6c40fe | 2011-10-31 14:45:46 -0700 | [diff] [blame] | 360 | int err; |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 361 | |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 362 | _fix_verify_io_params(wdata->lseg, &wdata->args.pages, |
| 363 | &wdata->args.pgbase, |
| 364 | wdata->args.offset, wdata->args.count); |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 365 | |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 366 | err = objio_write_pagelist(wdata, how); |
Boaz Harrosh | e6c40fe | 2011-10-31 14:45:46 -0700 | [diff] [blame] | 367 | if (unlikely(err)) { |
| 368 | wdata->pnfs_error = err; |
| 369 | dprintk("%s: Returned Error %d\n", __func__, err); |
| 370 | return PNFS_NOT_ATTEMPTED; |
| 371 | } |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 372 | return PNFS_ATTEMPTED; |
| 373 | } |
| 374 | |
Boaz Harrosh | a0fe8bf | 2011-05-22 19:54:13 +0300 | [diff] [blame] | 375 | void |
| 376 | objlayout_encode_layoutcommit(struct pnfs_layout_hdr *pnfslay, |
| 377 | struct xdr_stream *xdr, |
| 378 | const struct nfs4_layoutcommit_args *args) |
| 379 | { |
| 380 | struct objlayout *objlay = OBJLAYOUT(pnfslay); |
| 381 | struct pnfs_osd_layoutupdate lou; |
| 382 | __be32 *start; |
| 383 | |
| 384 | dprintk("%s: Begin\n", __func__); |
| 385 | |
| 386 | spin_lock(&objlay->lock); |
| 387 | lou.dsu_valid = (objlay->delta_space_valid == OBJ_DSU_VALID); |
| 388 | lou.dsu_delta = objlay->delta_space_used; |
| 389 | objlay->delta_space_used = 0; |
| 390 | objlay->delta_space_valid = OBJ_DSU_INIT; |
| 391 | lou.olu_ioerr_flag = !list_empty(&objlay->err_list); |
| 392 | spin_unlock(&objlay->lock); |
| 393 | |
| 394 | start = xdr_reserve_space(xdr, 4); |
| 395 | |
| 396 | BUG_ON(pnfs_osd_xdr_encode_layoutupdate(xdr, &lou)); |
| 397 | |
| 398 | *start = cpu_to_be32((xdr->p - start - 1) * 4); |
| 399 | |
| 400 | dprintk("%s: Return delta_space_used %lld err %d\n", __func__, |
| 401 | lou.dsu_delta, lou.olu_ioerr_flag); |
| 402 | } |
| 403 | |
Boaz Harrosh | adb5853 | 2011-05-26 21:49:46 +0300 | [diff] [blame] | 404 | static int |
| 405 | err_prio(u32 oer_errno) |
| 406 | { |
| 407 | switch (oer_errno) { |
| 408 | case 0: |
| 409 | return 0; |
| 410 | |
| 411 | case PNFS_OSD_ERR_RESOURCE: |
| 412 | return OSD_ERR_PRI_RESOURCE; |
| 413 | case PNFS_OSD_ERR_BAD_CRED: |
| 414 | return OSD_ERR_PRI_BAD_CRED; |
| 415 | case PNFS_OSD_ERR_NO_ACCESS: |
| 416 | return OSD_ERR_PRI_NO_ACCESS; |
| 417 | case PNFS_OSD_ERR_UNREACHABLE: |
| 418 | return OSD_ERR_PRI_UNREACHABLE; |
| 419 | case PNFS_OSD_ERR_NOT_FOUND: |
| 420 | return OSD_ERR_PRI_NOT_FOUND; |
| 421 | case PNFS_OSD_ERR_NO_SPACE: |
| 422 | return OSD_ERR_PRI_NO_SPACE; |
| 423 | default: |
| 424 | WARN_ON(1); |
| 425 | /* fallthrough */ |
| 426 | case PNFS_OSD_ERR_EIO: |
| 427 | return OSD_ERR_PRI_EIO; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | static void |
| 432 | merge_ioerr(struct pnfs_osd_ioerr *dest_err, |
| 433 | const struct pnfs_osd_ioerr *src_err) |
| 434 | { |
| 435 | u64 dest_end, src_end; |
| 436 | |
| 437 | if (!dest_err->oer_errno) { |
| 438 | *dest_err = *src_err; |
| 439 | /* accumulated device must be blank */ |
| 440 | memset(&dest_err->oer_component.oid_device_id, 0, |
| 441 | sizeof(dest_err->oer_component.oid_device_id)); |
| 442 | |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | if (dest_err->oer_component.oid_partition_id != |
| 447 | src_err->oer_component.oid_partition_id) |
| 448 | dest_err->oer_component.oid_partition_id = 0; |
| 449 | |
| 450 | if (dest_err->oer_component.oid_object_id != |
| 451 | src_err->oer_component.oid_object_id) |
| 452 | dest_err->oer_component.oid_object_id = 0; |
| 453 | |
| 454 | if (dest_err->oer_comp_offset > src_err->oer_comp_offset) |
| 455 | dest_err->oer_comp_offset = src_err->oer_comp_offset; |
| 456 | |
| 457 | dest_end = end_offset(dest_err->oer_comp_offset, |
| 458 | dest_err->oer_comp_length); |
| 459 | src_end = end_offset(src_err->oer_comp_offset, |
| 460 | src_err->oer_comp_length); |
| 461 | if (dest_end < src_end) |
| 462 | dest_end = src_end; |
| 463 | |
| 464 | dest_err->oer_comp_length = dest_end - dest_err->oer_comp_offset; |
| 465 | |
| 466 | if ((src_err->oer_iswrite == dest_err->oer_iswrite) && |
| 467 | (err_prio(src_err->oer_errno) > err_prio(dest_err->oer_errno))) { |
| 468 | dest_err->oer_errno = src_err->oer_errno; |
| 469 | } else if (src_err->oer_iswrite) { |
| 470 | dest_err->oer_iswrite = true; |
| 471 | dest_err->oer_errno = src_err->oer_errno; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | static void |
| 476 | encode_accumulated_error(struct objlayout *objlay, __be32 *p) |
| 477 | { |
| 478 | struct objlayout_io_state *state, *tmp; |
| 479 | struct pnfs_osd_ioerr accumulated_err = {.oer_errno = 0}; |
| 480 | |
| 481 | list_for_each_entry_safe(state, tmp, &objlay->err_list, err_list) { |
| 482 | unsigned i; |
| 483 | |
| 484 | for (i = 0; i < state->num_comps; i++) { |
| 485 | struct pnfs_osd_ioerr *ioerr = &state->ioerrs[i]; |
| 486 | |
| 487 | if (!ioerr->oer_errno) |
| 488 | continue; |
| 489 | |
| 490 | printk(KERN_ERR "%s: err[%d]: errno=%d is_write=%d " |
| 491 | "dev(%llx:%llx) par=0x%llx obj=0x%llx " |
| 492 | "offset=0x%llx length=0x%llx\n", |
| 493 | __func__, i, ioerr->oer_errno, |
| 494 | ioerr->oer_iswrite, |
| 495 | _DEVID_LO(&ioerr->oer_component.oid_device_id), |
| 496 | _DEVID_HI(&ioerr->oer_component.oid_device_id), |
| 497 | ioerr->oer_component.oid_partition_id, |
| 498 | ioerr->oer_component.oid_object_id, |
| 499 | ioerr->oer_comp_offset, |
| 500 | ioerr->oer_comp_length); |
| 501 | |
| 502 | merge_ioerr(&accumulated_err, ioerr); |
| 503 | } |
| 504 | list_del(&state->err_list); |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 505 | objio_free_result(state); |
Boaz Harrosh | adb5853 | 2011-05-26 21:49:46 +0300 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | pnfs_osd_xdr_encode_ioerr(p, &accumulated_err); |
| 509 | } |
| 510 | |
| 511 | void |
| 512 | objlayout_encode_layoutreturn(struct pnfs_layout_hdr *pnfslay, |
| 513 | struct xdr_stream *xdr, |
| 514 | const struct nfs4_layoutreturn_args *args) |
| 515 | { |
| 516 | struct objlayout *objlay = OBJLAYOUT(pnfslay); |
| 517 | struct objlayout_io_state *state, *tmp; |
| 518 | __be32 *start; |
| 519 | |
| 520 | dprintk("%s: Begin\n", __func__); |
| 521 | start = xdr_reserve_space(xdr, 4); |
| 522 | BUG_ON(!start); |
| 523 | |
| 524 | spin_lock(&objlay->lock); |
| 525 | |
| 526 | list_for_each_entry_safe(state, tmp, &objlay->err_list, err_list) { |
| 527 | __be32 *last_xdr = NULL, *p; |
| 528 | unsigned i; |
| 529 | int res = 0; |
| 530 | |
| 531 | for (i = 0; i < state->num_comps; i++) { |
| 532 | struct pnfs_osd_ioerr *ioerr = &state->ioerrs[i]; |
| 533 | |
| 534 | if (!ioerr->oer_errno) |
| 535 | continue; |
| 536 | |
| 537 | dprintk("%s: err[%d]: errno=%d is_write=%d " |
| 538 | "dev(%llx:%llx) par=0x%llx obj=0x%llx " |
| 539 | "offset=0x%llx length=0x%llx\n", |
| 540 | __func__, i, ioerr->oer_errno, |
| 541 | ioerr->oer_iswrite, |
| 542 | _DEVID_LO(&ioerr->oer_component.oid_device_id), |
| 543 | _DEVID_HI(&ioerr->oer_component.oid_device_id), |
| 544 | ioerr->oer_component.oid_partition_id, |
| 545 | ioerr->oer_component.oid_object_id, |
| 546 | ioerr->oer_comp_offset, |
| 547 | ioerr->oer_comp_length); |
| 548 | |
| 549 | p = pnfs_osd_xdr_ioerr_reserve_space(xdr); |
| 550 | if (unlikely(!p)) { |
| 551 | res = -E2BIG; |
| 552 | break; /* accumulated_error */ |
| 553 | } |
| 554 | |
| 555 | last_xdr = p; |
| 556 | pnfs_osd_xdr_encode_ioerr(p, &state->ioerrs[i]); |
| 557 | } |
| 558 | |
| 559 | /* TODO: use xdr_write_pages */ |
| 560 | if (unlikely(res)) { |
| 561 | /* no space for even one error descriptor */ |
| 562 | BUG_ON(!last_xdr); |
| 563 | |
| 564 | /* we've encountered a situation with lots and lots of |
| 565 | * errors and no space to encode them all. Use the last |
| 566 | * available slot to report the union of all the |
| 567 | * remaining errors. |
| 568 | */ |
| 569 | encode_accumulated_error(objlay, last_xdr); |
| 570 | goto loop_done; |
| 571 | } |
| 572 | list_del(&state->err_list); |
Boaz Harrosh | 9621855 | 2011-10-31 14:47:32 -0700 | [diff] [blame^] | 573 | objio_free_result(state); |
Boaz Harrosh | adb5853 | 2011-05-26 21:49:46 +0300 | [diff] [blame] | 574 | } |
| 575 | loop_done: |
| 576 | spin_unlock(&objlay->lock); |
| 577 | |
| 578 | *start = cpu_to_be32((xdr->p - start - 1) * 4); |
| 579 | dprintk("%s: Return\n", __func__); |
| 580 | } |
| 581 | |
| 582 | |
Boaz Harrosh | 04f8345 | 2011-05-22 19:52:19 +0300 | [diff] [blame] | 583 | /* |
Boaz Harrosh | b6c05f1 | 2011-05-26 21:45:34 +0300 | [diff] [blame] | 584 | * Get Device Info API for io engines |
| 585 | */ |
| 586 | struct objlayout_deviceinfo { |
| 587 | struct page *page; |
| 588 | struct pnfs_osd_deviceaddr da; /* This must be last */ |
| 589 | }; |
| 590 | |
| 591 | /* Initialize and call nfs_getdeviceinfo, then decode and return a |
| 592 | * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo() |
| 593 | * should be called. |
| 594 | */ |
| 595 | int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay, |
| 596 | struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr, |
| 597 | gfp_t gfp_flags) |
| 598 | { |
| 599 | struct objlayout_deviceinfo *odi; |
| 600 | struct pnfs_device pd; |
| 601 | struct super_block *sb; |
| 602 | struct page *page, **pages; |
| 603 | u32 *p; |
| 604 | int err; |
| 605 | |
| 606 | page = alloc_page(gfp_flags); |
| 607 | if (!page) |
| 608 | return -ENOMEM; |
| 609 | |
| 610 | pages = &page; |
| 611 | pd.pages = pages; |
| 612 | |
| 613 | memcpy(&pd.dev_id, d_id, sizeof(*d_id)); |
| 614 | pd.layout_type = LAYOUT_OSD2_OBJECTS; |
| 615 | pd.pages = &page; |
| 616 | pd.pgbase = 0; |
| 617 | pd.pglen = PAGE_SIZE; |
| 618 | pd.mincount = 0; |
| 619 | |
| 620 | sb = pnfslay->plh_inode->i_sb; |
| 621 | err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd); |
| 622 | dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err); |
| 623 | if (err) |
| 624 | goto err_out; |
| 625 | |
| 626 | p = page_address(page); |
| 627 | odi = kzalloc(sizeof(*odi), gfp_flags); |
| 628 | if (!odi) { |
| 629 | err = -ENOMEM; |
| 630 | goto err_out; |
| 631 | } |
| 632 | pnfs_osd_xdr_decode_deviceaddr(&odi->da, p); |
| 633 | odi->page = page; |
| 634 | *deviceaddr = &odi->da; |
| 635 | return 0; |
| 636 | |
| 637 | err_out: |
| 638 | __free_page(page); |
| 639 | return err; |
| 640 | } |
| 641 | |
| 642 | void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr) |
| 643 | { |
| 644 | struct objlayout_deviceinfo *odi = container_of(deviceaddr, |
| 645 | struct objlayout_deviceinfo, |
| 646 | da); |
| 647 | |
| 648 | __free_page(odi->page); |
| 649 | kfree(odi); |
| 650 | } |