Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Module for pnfs flexfile layout driver. |
| 3 | * |
| 4 | * Copyright (c) 2014, Primary Data, Inc. All rights reserved. |
| 5 | * |
| 6 | * Tao Peng <bergwolf@primarydata.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/nfs_fs.h> |
| 10 | #include <linux/nfs_page.h> |
| 11 | #include <linux/module.h> |
| 12 | |
| 13 | #include <linux/sunrpc/metrics.h> |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 14 | |
| 15 | #include "flexfilelayout.h" |
| 16 | #include "../nfs4session.h" |
Anna Schumaker | 40c64c2 | 2015-04-15 13:00:05 -0400 | [diff] [blame] | 17 | #include "../nfs4idmap.h" |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 18 | #include "../internal.h" |
| 19 | #include "../delegation.h" |
| 20 | #include "../nfs4trace.h" |
| 21 | #include "../iostat.h" |
| 22 | #include "../nfs.h" |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame^] | 23 | #include "../nfs42.h" |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 24 | |
| 25 | #define NFSDBG_FACILITY NFSDBG_PNFS_LD |
| 26 | |
| 27 | #define FF_LAYOUT_POLL_RETRY_MAX (15*HZ) |
| 28 | |
| 29 | static struct pnfs_layout_hdr * |
| 30 | ff_layout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags) |
| 31 | { |
| 32 | struct nfs4_flexfile_layout *ffl; |
| 33 | |
| 34 | ffl = kzalloc(sizeof(*ffl), gfp_flags); |
| 35 | if (ffl) { |
| 36 | INIT_LIST_HEAD(&ffl->error_list); |
| 37 | return &ffl->generic_hdr; |
| 38 | } else |
| 39 | return NULL; |
| 40 | } |
| 41 | |
| 42 | static void |
| 43 | ff_layout_free_layout_hdr(struct pnfs_layout_hdr *lo) |
| 44 | { |
| 45 | struct nfs4_ff_layout_ds_err *err, *n; |
| 46 | |
| 47 | list_for_each_entry_safe(err, n, &FF_LAYOUT_FROM_HDR(lo)->error_list, |
| 48 | list) { |
| 49 | list_del(&err->list); |
| 50 | kfree(err); |
| 51 | } |
| 52 | kfree(FF_LAYOUT_FROM_HDR(lo)); |
| 53 | } |
| 54 | |
| 55 | static int decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) |
| 56 | { |
| 57 | __be32 *p; |
| 58 | |
| 59 | p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE); |
| 60 | if (unlikely(p == NULL)) |
| 61 | return -ENOBUFS; |
| 62 | memcpy(stateid, p, NFS4_STATEID_SIZE); |
| 63 | dprintk("%s: stateid id= [%x%x%x%x]\n", __func__, |
| 64 | p[0], p[1], p[2], p[3]); |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | static int decode_deviceid(struct xdr_stream *xdr, struct nfs4_deviceid *devid) |
| 69 | { |
| 70 | __be32 *p; |
| 71 | |
| 72 | p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE); |
| 73 | if (unlikely(!p)) |
| 74 | return -ENOBUFS; |
| 75 | memcpy(devid, p, NFS4_DEVICEID4_SIZE); |
| 76 | nfs4_print_deviceid(devid); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int decode_nfs_fh(struct xdr_stream *xdr, struct nfs_fh *fh) |
| 81 | { |
| 82 | __be32 *p; |
| 83 | |
| 84 | p = xdr_inline_decode(xdr, 4); |
| 85 | if (unlikely(!p)) |
| 86 | return -ENOBUFS; |
| 87 | fh->size = be32_to_cpup(p++); |
| 88 | if (fh->size > sizeof(struct nfs_fh)) { |
| 89 | printk(KERN_ERR "NFS flexfiles: Too big fh received %d\n", |
| 90 | fh->size); |
| 91 | return -EOVERFLOW; |
| 92 | } |
| 93 | /* fh.data */ |
| 94 | p = xdr_inline_decode(xdr, fh->size); |
| 95 | if (unlikely(!p)) |
| 96 | return -ENOBUFS; |
| 97 | memcpy(&fh->data, p, fh->size); |
| 98 | dprintk("%s: fh len %d\n", __func__, fh->size); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Currently only stringified uids and gids are accepted. |
| 105 | * I.e., kerberos is not supported to the DSes, so no pricipals. |
| 106 | * |
| 107 | * That means that one common function will suffice, but when |
| 108 | * principals are added, this should be split to accomodate |
| 109 | * calls to both nfs_map_name_to_uid() and nfs_map_group_to_gid(). |
| 110 | */ |
| 111 | static int |
| 112 | decode_name(struct xdr_stream *xdr, u32 *id) |
| 113 | { |
| 114 | __be32 *p; |
| 115 | int len; |
| 116 | |
| 117 | /* opaque_length(4)*/ |
| 118 | p = xdr_inline_decode(xdr, 4); |
| 119 | if (unlikely(!p)) |
| 120 | return -ENOBUFS; |
| 121 | len = be32_to_cpup(p++); |
| 122 | if (len < 0) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | dprintk("%s: len %u\n", __func__, len); |
| 126 | |
| 127 | /* opaque body */ |
| 128 | p = xdr_inline_decode(xdr, len); |
| 129 | if (unlikely(!p)) |
| 130 | return -ENOBUFS; |
| 131 | |
| 132 | if (!nfs_map_string_to_numeric((char *)p, len, id)) |
| 133 | return -EINVAL; |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment *fls) |
| 139 | { |
| 140 | int i; |
| 141 | |
| 142 | if (fls->mirror_array) { |
| 143 | for (i = 0; i < fls->mirror_array_cnt; i++) { |
| 144 | /* normally mirror_ds is freed in |
| 145 | * .free_deviceid_node but we still do it here |
| 146 | * for .alloc_lseg error path */ |
| 147 | if (fls->mirror_array[i]) { |
| 148 | kfree(fls->mirror_array[i]->fh_versions); |
| 149 | nfs4_ff_layout_put_deviceid(fls->mirror_array[i]->mirror_ds); |
| 150 | kfree(fls->mirror_array[i]); |
| 151 | } |
| 152 | } |
| 153 | kfree(fls->mirror_array); |
| 154 | fls->mirror_array = NULL; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static int ff_layout_check_layout(struct nfs4_layoutget_res *lgr) |
| 159 | { |
| 160 | int ret = 0; |
| 161 | |
| 162 | dprintk("--> %s\n", __func__); |
| 163 | |
| 164 | /* FIXME: remove this check when layout segment support is added */ |
| 165 | if (lgr->range.offset != 0 || |
| 166 | lgr->range.length != NFS4_MAX_UINT64) { |
| 167 | dprintk("%s Only whole file layouts supported. Use MDS i/o\n", |
| 168 | __func__); |
| 169 | ret = -EINVAL; |
| 170 | } |
| 171 | |
| 172 | dprintk("--> %s returns %d\n", __func__, ret); |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment *fls) |
| 177 | { |
| 178 | if (fls) { |
| 179 | ff_layout_free_mirror_array(fls); |
| 180 | kfree(fls); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment *fls) |
| 185 | { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 186 | int i, j; |
| 187 | |
| 188 | for (i = 0; i < fls->mirror_array_cnt - 1; i++) { |
| 189 | for (j = i + 1; j < fls->mirror_array_cnt; j++) |
| 190 | if (fls->mirror_array[i]->efficiency < |
Fabian Frederick | 455b6ee | 2015-06-12 18:58:50 +0200 | [diff] [blame] | 191 | fls->mirror_array[j]->efficiency) |
| 192 | swap(fls->mirror_array[i], |
| 193 | fls->mirror_array[j]); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | static struct pnfs_layout_segment * |
| 198 | ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh, |
| 199 | struct nfs4_layoutget_res *lgr, |
| 200 | gfp_t gfp_flags) |
| 201 | { |
| 202 | struct pnfs_layout_segment *ret; |
| 203 | struct nfs4_ff_layout_segment *fls = NULL; |
| 204 | struct xdr_stream stream; |
| 205 | struct xdr_buf buf; |
| 206 | struct page *scratch; |
| 207 | u64 stripe_unit; |
| 208 | u32 mirror_array_cnt; |
| 209 | __be32 *p; |
| 210 | int i, rc; |
| 211 | |
| 212 | dprintk("--> %s\n", __func__); |
| 213 | scratch = alloc_page(gfp_flags); |
| 214 | if (!scratch) |
| 215 | return ERR_PTR(-ENOMEM); |
| 216 | |
| 217 | xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, |
| 218 | lgr->layoutp->len); |
| 219 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
| 220 | |
| 221 | /* stripe unit and mirror_array_cnt */ |
| 222 | rc = -EIO; |
| 223 | p = xdr_inline_decode(&stream, 8 + 4); |
| 224 | if (!p) |
| 225 | goto out_err_free; |
| 226 | |
| 227 | p = xdr_decode_hyper(p, &stripe_unit); |
| 228 | mirror_array_cnt = be32_to_cpup(p++); |
| 229 | dprintk("%s: stripe_unit=%llu mirror_array_cnt=%u\n", __func__, |
| 230 | stripe_unit, mirror_array_cnt); |
| 231 | |
| 232 | if (mirror_array_cnt > NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT || |
| 233 | mirror_array_cnt == 0) |
| 234 | goto out_err_free; |
| 235 | |
| 236 | rc = -ENOMEM; |
| 237 | fls = kzalloc(sizeof(*fls), gfp_flags); |
| 238 | if (!fls) |
| 239 | goto out_err_free; |
| 240 | |
| 241 | fls->mirror_array_cnt = mirror_array_cnt; |
| 242 | fls->stripe_unit = stripe_unit; |
| 243 | fls->mirror_array = kcalloc(fls->mirror_array_cnt, |
| 244 | sizeof(fls->mirror_array[0]), gfp_flags); |
| 245 | if (fls->mirror_array == NULL) |
| 246 | goto out_err_free; |
| 247 | |
| 248 | for (i = 0; i < fls->mirror_array_cnt; i++) { |
| 249 | struct nfs4_deviceid devid; |
| 250 | struct nfs4_deviceid_node *idnode; |
| 251 | u32 ds_count; |
| 252 | u32 fh_count; |
| 253 | int j; |
| 254 | |
| 255 | rc = -EIO; |
| 256 | p = xdr_inline_decode(&stream, 4); |
| 257 | if (!p) |
| 258 | goto out_err_free; |
| 259 | ds_count = be32_to_cpup(p); |
| 260 | |
| 261 | /* FIXME: allow for striping? */ |
| 262 | if (ds_count != 1) |
| 263 | goto out_err_free; |
| 264 | |
| 265 | fls->mirror_array[i] = |
| 266 | kzalloc(sizeof(struct nfs4_ff_layout_mirror), |
| 267 | gfp_flags); |
| 268 | if (fls->mirror_array[i] == NULL) { |
| 269 | rc = -ENOMEM; |
| 270 | goto out_err_free; |
| 271 | } |
| 272 | |
| 273 | spin_lock_init(&fls->mirror_array[i]->lock); |
| 274 | fls->mirror_array[i]->ds_count = ds_count; |
| 275 | |
| 276 | /* deviceid */ |
| 277 | rc = decode_deviceid(&stream, &devid); |
| 278 | if (rc) |
| 279 | goto out_err_free; |
| 280 | |
| 281 | idnode = nfs4_find_get_deviceid(NFS_SERVER(lh->plh_inode), |
| 282 | &devid, lh->plh_lc_cred, |
| 283 | gfp_flags); |
| 284 | /* |
| 285 | * upon success, mirror_ds is allocated by previous |
| 286 | * getdeviceinfo, or newly by .alloc_deviceid_node |
| 287 | * nfs4_find_get_deviceid failure is indeed getdeviceinfo falure |
| 288 | */ |
| 289 | if (idnode) |
| 290 | fls->mirror_array[i]->mirror_ds = |
| 291 | FF_LAYOUT_MIRROR_DS(idnode); |
| 292 | else |
| 293 | goto out_err_free; |
| 294 | |
| 295 | /* efficiency */ |
| 296 | rc = -EIO; |
| 297 | p = xdr_inline_decode(&stream, 4); |
| 298 | if (!p) |
| 299 | goto out_err_free; |
| 300 | fls->mirror_array[i]->efficiency = be32_to_cpup(p); |
| 301 | |
| 302 | /* stateid */ |
| 303 | rc = decode_stateid(&stream, &fls->mirror_array[i]->stateid); |
| 304 | if (rc) |
| 305 | goto out_err_free; |
| 306 | |
| 307 | /* fh */ |
| 308 | p = xdr_inline_decode(&stream, 4); |
| 309 | if (!p) |
| 310 | goto out_err_free; |
| 311 | fh_count = be32_to_cpup(p); |
| 312 | |
| 313 | fls->mirror_array[i]->fh_versions = |
| 314 | kzalloc(fh_count * sizeof(struct nfs_fh), |
| 315 | gfp_flags); |
| 316 | if (fls->mirror_array[i]->fh_versions == NULL) { |
| 317 | rc = -ENOMEM; |
| 318 | goto out_err_free; |
| 319 | } |
| 320 | |
| 321 | for (j = 0; j < fh_count; j++) { |
| 322 | rc = decode_nfs_fh(&stream, |
| 323 | &fls->mirror_array[i]->fh_versions[j]); |
| 324 | if (rc) |
| 325 | goto out_err_free; |
| 326 | } |
| 327 | |
| 328 | fls->mirror_array[i]->fh_versions_cnt = fh_count; |
| 329 | |
| 330 | /* user */ |
| 331 | rc = decode_name(&stream, &fls->mirror_array[i]->uid); |
| 332 | if (rc) |
| 333 | goto out_err_free; |
| 334 | |
| 335 | /* group */ |
| 336 | rc = decode_name(&stream, &fls->mirror_array[i]->gid); |
| 337 | if (rc) |
| 338 | goto out_err_free; |
| 339 | |
| 340 | dprintk("%s: uid %d gid %d\n", __func__, |
| 341 | fls->mirror_array[i]->uid, |
| 342 | fls->mirror_array[i]->gid); |
| 343 | } |
| 344 | |
| 345 | ff_layout_sort_mirrors(fls); |
| 346 | rc = ff_layout_check_layout(lgr); |
| 347 | if (rc) |
| 348 | goto out_err_free; |
| 349 | |
| 350 | ret = &fls->generic_hdr; |
| 351 | dprintk("<-- %s (success)\n", __func__); |
| 352 | out_free_page: |
| 353 | __free_page(scratch); |
| 354 | return ret; |
| 355 | out_err_free: |
| 356 | _ff_layout_free_lseg(fls); |
| 357 | ret = ERR_PTR(rc); |
| 358 | dprintk("<-- %s (%d)\n", __func__, rc); |
| 359 | goto out_free_page; |
| 360 | } |
| 361 | |
| 362 | static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout) |
| 363 | { |
| 364 | struct pnfs_layout_segment *lseg; |
| 365 | |
| 366 | list_for_each_entry(lseg, &layout->plh_segs, pls_list) |
| 367 | if (lseg->pls_range.iomode == IOMODE_RW) |
| 368 | return true; |
| 369 | |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | static void |
| 374 | ff_layout_free_lseg(struct pnfs_layout_segment *lseg) |
| 375 | { |
| 376 | struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg); |
| 377 | int i; |
| 378 | |
| 379 | dprintk("--> %s\n", __func__); |
| 380 | |
| 381 | for (i = 0; i < fls->mirror_array_cnt; i++) { |
| 382 | if (fls->mirror_array[i]) { |
| 383 | nfs4_ff_layout_put_deviceid(fls->mirror_array[i]->mirror_ds); |
| 384 | fls->mirror_array[i]->mirror_ds = NULL; |
| 385 | if (fls->mirror_array[i]->cred) { |
| 386 | put_rpccred(fls->mirror_array[i]->cred); |
| 387 | fls->mirror_array[i]->cred = NULL; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | if (lseg->pls_range.iomode == IOMODE_RW) { |
| 393 | struct nfs4_flexfile_layout *ffl; |
| 394 | struct inode *inode; |
| 395 | |
| 396 | ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout); |
| 397 | inode = ffl->generic_hdr.plh_inode; |
| 398 | spin_lock(&inode->i_lock); |
| 399 | if (!ff_layout_has_rw_segments(lseg->pls_layout)) { |
| 400 | ffl->commit_info.nbuckets = 0; |
| 401 | kfree(ffl->commit_info.buckets); |
| 402 | ffl->commit_info.buckets = NULL; |
| 403 | } |
| 404 | spin_unlock(&inode->i_lock); |
| 405 | } |
| 406 | _ff_layout_free_lseg(fls); |
| 407 | } |
| 408 | |
| 409 | /* Return 1 until we have multiple lsegs support */ |
| 410 | static int |
| 411 | ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls) |
| 412 | { |
| 413 | return 1; |
| 414 | } |
| 415 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 416 | static void |
| 417 | nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer *timer) |
| 418 | { |
| 419 | ktime_t old, new; |
| 420 | |
| 421 | /* |
| 422 | * Note: careful here! |
| 423 | * If the counter is zero, then we must not increment it until after |
| 424 | * we've set the start_time. |
| 425 | * If we were instead to use atomic_inc_return(), then another |
| 426 | * request might come in, bump, and then call end_busy_timer() |
| 427 | * before we've set the timer->start_time. |
| 428 | */ |
| 429 | old = timer->start_time; |
| 430 | if (atomic_inc_not_zero(&timer->n_ops) == 0) { |
| 431 | new = ktime_get(); |
| 432 | cmpxchg(&timer->start_time.tv64, old.tv64, new.tv64); |
| 433 | atomic_inc(&timer->n_ops); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | static ktime_t |
| 438 | nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer *timer) |
| 439 | { |
| 440 | ktime_t start, now; |
| 441 | |
| 442 | now = ktime_get(); |
| 443 | start.tv64 = xchg(&timer->start_time.tv64, now.tv64); |
| 444 | atomic_dec(&timer->n_ops); |
| 445 | return ktime_sub(now, start); |
| 446 | } |
| 447 | |
| 448 | static ktime_t |
| 449 | nfs4_ff_layout_calc_completion_time(struct rpc_task *task) |
| 450 | { |
| 451 | return ktime_sub(ktime_get(), task->tk_start); |
| 452 | } |
| 453 | |
| 454 | static void |
Peng Tao | d983803 | 2015-06-23 19:52:00 +0800 | [diff] [blame] | 455 | nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror, |
| 456 | struct nfs4_ff_layoutstat *layoutstat) |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 457 | { |
Peng Tao | d983803 | 2015-06-23 19:52:00 +0800 | [diff] [blame] | 458 | static const ktime_t notime = {0}; |
| 459 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 460 | nfs4_ff_start_busy_timer(&layoutstat->busy_timer); |
Peng Tao | d983803 | 2015-06-23 19:52:00 +0800 | [diff] [blame] | 461 | cmpxchg(&mirror->start_time, notime, ktime_get()); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static void |
| 465 | nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat, |
| 466 | __u64 requested) |
| 467 | { |
| 468 | struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat; |
| 469 | |
| 470 | iostat->ops_requested++; |
| 471 | iostat->bytes_requested += requested; |
| 472 | } |
| 473 | |
| 474 | static void |
| 475 | nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat, |
| 476 | __u64 requested, |
| 477 | __u64 completed, |
| 478 | ktime_t time_completed) |
| 479 | { |
| 480 | struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat; |
| 481 | ktime_t timer; |
| 482 | |
| 483 | iostat->ops_completed++; |
| 484 | iostat->bytes_completed += completed; |
| 485 | iostat->bytes_not_delivered += requested - completed; |
| 486 | |
| 487 | timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer); |
| 488 | iostat->total_busy_time = |
| 489 | ktime_add(iostat->total_busy_time, timer); |
| 490 | iostat->aggregate_completion_time = |
| 491 | ktime_add(iostat->aggregate_completion_time, time_completed); |
| 492 | } |
| 493 | |
| 494 | static void |
| 495 | nfs4_ff_layout_stat_io_start_read(struct nfs4_ff_layout_mirror *mirror, |
| 496 | __u64 requested) |
| 497 | { |
| 498 | spin_lock(&mirror->lock); |
Peng Tao | d983803 | 2015-06-23 19:52:00 +0800 | [diff] [blame] | 499 | nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 500 | nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested); |
| 501 | spin_unlock(&mirror->lock); |
| 502 | } |
| 503 | |
| 504 | static void |
| 505 | nfs4_ff_layout_stat_io_end_read(struct rpc_task *task, |
| 506 | struct nfs4_ff_layout_mirror *mirror, |
| 507 | __u64 requested, |
| 508 | __u64 completed) |
| 509 | { |
| 510 | spin_lock(&mirror->lock); |
| 511 | nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat, |
| 512 | requested, completed, |
| 513 | nfs4_ff_layout_calc_completion_time(task)); |
| 514 | spin_unlock(&mirror->lock); |
| 515 | } |
| 516 | |
| 517 | static void |
| 518 | nfs4_ff_layout_stat_io_start_write(struct nfs4_ff_layout_mirror *mirror, |
| 519 | __u64 requested) |
| 520 | { |
| 521 | spin_lock(&mirror->lock); |
Peng Tao | d983803 | 2015-06-23 19:52:00 +0800 | [diff] [blame] | 522 | nfs4_ff_layoutstat_start_io(mirror, &mirror->write_stat); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 523 | nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested); |
| 524 | spin_unlock(&mirror->lock); |
| 525 | } |
| 526 | |
| 527 | static void |
| 528 | nfs4_ff_layout_stat_io_end_write(struct rpc_task *task, |
| 529 | struct nfs4_ff_layout_mirror *mirror, |
| 530 | __u64 requested, |
| 531 | __u64 completed, |
| 532 | enum nfs3_stable_how committed) |
| 533 | { |
| 534 | if (committed == NFS_UNSTABLE) |
| 535 | requested = completed = 0; |
| 536 | |
| 537 | spin_lock(&mirror->lock); |
| 538 | nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat, |
| 539 | requested, completed, |
| 540 | nfs4_ff_layout_calc_completion_time(task)); |
| 541 | spin_unlock(&mirror->lock); |
| 542 | } |
| 543 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 544 | static int |
| 545 | ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg, |
| 546 | struct nfs_commit_info *cinfo, |
| 547 | gfp_t gfp_flags) |
| 548 | { |
| 549 | struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg); |
| 550 | struct pnfs_commit_bucket *buckets; |
| 551 | int size; |
| 552 | |
| 553 | if (cinfo->ds->nbuckets != 0) { |
| 554 | /* This assumes there is only one RW lseg per file. |
| 555 | * To support multiple lseg per file, we need to |
| 556 | * change struct pnfs_commit_bucket to allow dynamic |
| 557 | * increasing nbuckets. |
| 558 | */ |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg); |
| 563 | |
| 564 | buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket), |
| 565 | gfp_flags); |
| 566 | if (!buckets) |
| 567 | return -ENOMEM; |
| 568 | else { |
| 569 | int i; |
| 570 | |
| 571 | spin_lock(cinfo->lock); |
| 572 | if (cinfo->ds->nbuckets != 0) |
| 573 | kfree(buckets); |
| 574 | else { |
| 575 | cinfo->ds->buckets = buckets; |
| 576 | cinfo->ds->nbuckets = size; |
| 577 | for (i = 0; i < size; i++) { |
| 578 | INIT_LIST_HEAD(&buckets[i].written); |
| 579 | INIT_LIST_HEAD(&buckets[i].committing); |
| 580 | /* mark direct verifier as unset */ |
| 581 | buckets[i].direct_verf.committed = |
| 582 | NFS_INVALID_STABLE_HOW; |
| 583 | } |
| 584 | } |
| 585 | spin_unlock(cinfo->lock); |
| 586 | return 0; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | static struct nfs4_pnfs_ds * |
| 591 | ff_layout_choose_best_ds_for_read(struct nfs_pageio_descriptor *pgio, |
| 592 | int *best_idx) |
| 593 | { |
| 594 | struct nfs4_ff_layout_segment *fls; |
| 595 | struct nfs4_pnfs_ds *ds; |
| 596 | int idx; |
| 597 | |
| 598 | fls = FF_LAYOUT_LSEG(pgio->pg_lseg); |
| 599 | /* mirrors are sorted by efficiency */ |
| 600 | for (idx = 0; idx < fls->mirror_array_cnt; idx++) { |
| 601 | ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, idx, false); |
| 602 | if (ds) { |
| 603 | *best_idx = idx; |
| 604 | return ds; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | return NULL; |
| 609 | } |
| 610 | |
| 611 | static void |
| 612 | ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio, |
| 613 | struct nfs_page *req) |
| 614 | { |
| 615 | struct nfs_pgio_mirror *pgm; |
| 616 | struct nfs4_ff_layout_mirror *mirror; |
| 617 | struct nfs4_pnfs_ds *ds; |
| 618 | int ds_idx; |
| 619 | |
| 620 | /* Use full layout for now */ |
| 621 | if (!pgio->pg_lseg) |
| 622 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 623 | req->wb_context, |
| 624 | 0, |
| 625 | NFS4_MAX_UINT64, |
| 626 | IOMODE_READ, |
| 627 | GFP_KERNEL); |
| 628 | /* If no lseg, fall back to read through mds */ |
| 629 | if (pgio->pg_lseg == NULL) |
| 630 | goto out_mds; |
| 631 | |
| 632 | ds = ff_layout_choose_best_ds_for_read(pgio, &ds_idx); |
| 633 | if (!ds) |
| 634 | goto out_mds; |
| 635 | mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx); |
| 636 | |
| 637 | pgio->pg_mirror_idx = ds_idx; |
| 638 | |
| 639 | /* read always uses only one mirror - idx 0 for pgio layer */ |
| 640 | pgm = &pgio->pg_mirrors[0]; |
| 641 | pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize; |
| 642 | |
| 643 | return; |
| 644 | out_mds: |
| 645 | pnfs_put_lseg(pgio->pg_lseg); |
| 646 | pgio->pg_lseg = NULL; |
| 647 | nfs_pageio_reset_read_mds(pgio); |
| 648 | } |
| 649 | |
| 650 | static void |
| 651 | ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio, |
| 652 | struct nfs_page *req) |
| 653 | { |
| 654 | struct nfs4_ff_layout_mirror *mirror; |
| 655 | struct nfs_pgio_mirror *pgm; |
| 656 | struct nfs_commit_info cinfo; |
| 657 | struct nfs4_pnfs_ds *ds; |
| 658 | int i; |
| 659 | int status; |
| 660 | |
| 661 | if (!pgio->pg_lseg) |
| 662 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 663 | req->wb_context, |
| 664 | 0, |
| 665 | NFS4_MAX_UINT64, |
| 666 | IOMODE_RW, |
| 667 | GFP_NOFS); |
| 668 | /* If no lseg, fall back to write through mds */ |
| 669 | if (pgio->pg_lseg == NULL) |
| 670 | goto out_mds; |
| 671 | |
| 672 | nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq); |
| 673 | status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS); |
| 674 | if (status < 0) |
| 675 | goto out_mds; |
| 676 | |
| 677 | /* Use a direct mapping of ds_idx to pgio mirror_idx */ |
| 678 | if (WARN_ON_ONCE(pgio->pg_mirror_count != |
| 679 | FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg))) |
| 680 | goto out_mds; |
| 681 | |
| 682 | for (i = 0; i < pgio->pg_mirror_count; i++) { |
| 683 | ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true); |
| 684 | if (!ds) |
| 685 | goto out_mds; |
| 686 | pgm = &pgio->pg_mirrors[i]; |
| 687 | mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i); |
| 688 | pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize; |
| 689 | } |
| 690 | |
| 691 | return; |
| 692 | |
| 693 | out_mds: |
| 694 | pnfs_put_lseg(pgio->pg_lseg); |
| 695 | pgio->pg_lseg = NULL; |
| 696 | nfs_pageio_reset_write_mds(pgio); |
| 697 | } |
| 698 | |
| 699 | static unsigned int |
| 700 | ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio, |
| 701 | struct nfs_page *req) |
| 702 | { |
| 703 | if (!pgio->pg_lseg) |
| 704 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 705 | req->wb_context, |
| 706 | 0, |
| 707 | NFS4_MAX_UINT64, |
| 708 | IOMODE_RW, |
| 709 | GFP_NOFS); |
| 710 | if (pgio->pg_lseg) |
| 711 | return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg); |
| 712 | |
| 713 | /* no lseg means that pnfs is not in use, so no mirroring here */ |
| 714 | pnfs_put_lseg(pgio->pg_lseg); |
| 715 | pgio->pg_lseg = NULL; |
| 716 | nfs_pageio_reset_write_mds(pgio); |
| 717 | return 1; |
| 718 | } |
| 719 | |
| 720 | static const struct nfs_pageio_ops ff_layout_pg_read_ops = { |
| 721 | .pg_init = ff_layout_pg_init_read, |
| 722 | .pg_test = pnfs_generic_pg_test, |
| 723 | .pg_doio = pnfs_generic_pg_readpages, |
| 724 | .pg_cleanup = pnfs_generic_pg_cleanup, |
| 725 | }; |
| 726 | |
| 727 | static const struct nfs_pageio_ops ff_layout_pg_write_ops = { |
| 728 | .pg_init = ff_layout_pg_init_write, |
| 729 | .pg_test = pnfs_generic_pg_test, |
| 730 | .pg_doio = pnfs_generic_pg_writepages, |
| 731 | .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write, |
| 732 | .pg_cleanup = pnfs_generic_pg_cleanup, |
| 733 | }; |
| 734 | |
| 735 | static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs) |
| 736 | { |
| 737 | struct rpc_task *task = &hdr->task; |
| 738 | |
| 739 | pnfs_layoutcommit_inode(hdr->inode, false); |
| 740 | |
| 741 | if (retry_pnfs) { |
| 742 | dprintk("%s Reset task %5u for i/o through pNFS " |
| 743 | "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, |
| 744 | hdr->task.tk_pid, |
| 745 | hdr->inode->i_sb->s_id, |
| 746 | (unsigned long long)NFS_FILEID(hdr->inode), |
| 747 | hdr->args.count, |
| 748 | (unsigned long long)hdr->args.offset); |
| 749 | |
| 750 | if (!hdr->dreq) { |
| 751 | struct nfs_open_context *ctx; |
| 752 | |
| 753 | ctx = nfs_list_entry(hdr->pages.next)->wb_context; |
| 754 | set_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags); |
| 755 | hdr->completion_ops->error_cleanup(&hdr->pages); |
| 756 | } else { |
| 757 | nfs_direct_set_resched_writes(hdr->dreq); |
| 758 | /* fake unstable write to let common nfs resend pages */ |
| 759 | hdr->verf.committed = NFS_UNSTABLE; |
| 760 | hdr->good_bytes = 0; |
| 761 | } |
| 762 | return; |
| 763 | } |
| 764 | |
| 765 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
| 766 | dprintk("%s Reset task %5u for i/o through MDS " |
| 767 | "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, |
| 768 | hdr->task.tk_pid, |
| 769 | hdr->inode->i_sb->s_id, |
| 770 | (unsigned long long)NFS_FILEID(hdr->inode), |
| 771 | hdr->args.count, |
| 772 | (unsigned long long)hdr->args.offset); |
| 773 | |
| 774 | task->tk_status = pnfs_write_done_resend_to_mds(hdr); |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | static void ff_layout_reset_read(struct nfs_pgio_header *hdr) |
| 779 | { |
| 780 | struct rpc_task *task = &hdr->task; |
| 781 | |
| 782 | pnfs_layoutcommit_inode(hdr->inode, false); |
| 783 | |
| 784 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
| 785 | dprintk("%s Reset task %5u for i/o through MDS " |
| 786 | "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, |
| 787 | hdr->task.tk_pid, |
| 788 | hdr->inode->i_sb->s_id, |
| 789 | (unsigned long long)NFS_FILEID(hdr->inode), |
| 790 | hdr->args.count, |
| 791 | (unsigned long long)hdr->args.offset); |
| 792 | |
| 793 | task->tk_status = pnfs_read_done_resend_to_mds(hdr); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | static int ff_layout_async_handle_error_v4(struct rpc_task *task, |
| 798 | struct nfs4_state *state, |
| 799 | struct nfs_client *clp, |
| 800 | struct pnfs_layout_segment *lseg, |
| 801 | int idx) |
| 802 | { |
| 803 | struct pnfs_layout_hdr *lo = lseg->pls_layout; |
| 804 | struct inode *inode = lo->plh_inode; |
| 805 | struct nfs_server *mds_server = NFS_SERVER(inode); |
| 806 | |
| 807 | struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx); |
| 808 | struct nfs_client *mds_client = mds_server->nfs_client; |
| 809 | struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table; |
| 810 | |
| 811 | if (task->tk_status >= 0) |
| 812 | return 0; |
| 813 | |
| 814 | switch (task->tk_status) { |
| 815 | /* MDS state errors */ |
| 816 | case -NFS4ERR_DELEG_REVOKED: |
| 817 | case -NFS4ERR_ADMIN_REVOKED: |
| 818 | case -NFS4ERR_BAD_STATEID: |
| 819 | if (state == NULL) |
| 820 | break; |
| 821 | nfs_remove_bad_delegation(state->inode); |
| 822 | case -NFS4ERR_OPENMODE: |
| 823 | if (state == NULL) |
| 824 | break; |
| 825 | if (nfs4_schedule_stateid_recovery(mds_server, state) < 0) |
| 826 | goto out_bad_stateid; |
| 827 | goto wait_on_recovery; |
| 828 | case -NFS4ERR_EXPIRED: |
| 829 | if (state != NULL) { |
| 830 | if (nfs4_schedule_stateid_recovery(mds_server, state) < 0) |
| 831 | goto out_bad_stateid; |
| 832 | } |
| 833 | nfs4_schedule_lease_recovery(mds_client); |
| 834 | goto wait_on_recovery; |
| 835 | /* DS session errors */ |
| 836 | case -NFS4ERR_BADSESSION: |
| 837 | case -NFS4ERR_BADSLOT: |
| 838 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 839 | case -NFS4ERR_DEADSESSION: |
| 840 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 841 | case -NFS4ERR_SEQ_FALSE_RETRY: |
| 842 | case -NFS4ERR_SEQ_MISORDERED: |
| 843 | dprintk("%s ERROR %d, Reset session. Exchangeid " |
| 844 | "flags 0x%x\n", __func__, task->tk_status, |
| 845 | clp->cl_exchange_flags); |
| 846 | nfs4_schedule_session_recovery(clp->cl_session, task->tk_status); |
| 847 | break; |
| 848 | case -NFS4ERR_DELAY: |
| 849 | case -NFS4ERR_GRACE: |
| 850 | rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX); |
| 851 | break; |
| 852 | case -NFS4ERR_RETRY_UNCACHED_REP: |
| 853 | break; |
| 854 | /* Invalidate Layout errors */ |
| 855 | case -NFS4ERR_PNFS_NO_LAYOUT: |
| 856 | case -ESTALE: /* mapped NFS4ERR_STALE */ |
| 857 | case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */ |
| 858 | case -EISDIR: /* mapped NFS4ERR_ISDIR */ |
| 859 | case -NFS4ERR_FHEXPIRED: |
| 860 | case -NFS4ERR_WRONG_TYPE: |
| 861 | dprintk("%s Invalid layout error %d\n", __func__, |
| 862 | task->tk_status); |
| 863 | /* |
| 864 | * Destroy layout so new i/o will get a new layout. |
| 865 | * Layout will not be destroyed until all current lseg |
| 866 | * references are put. Mark layout as invalid to resend failed |
| 867 | * i/o and all i/o waiting on the slot table to the MDS until |
| 868 | * layout is destroyed and a new valid layout is obtained. |
| 869 | */ |
| 870 | pnfs_destroy_layout(NFS_I(inode)); |
| 871 | rpc_wake_up(&tbl->slot_tbl_waitq); |
| 872 | goto reset; |
| 873 | /* RPC connection errors */ |
| 874 | case -ECONNREFUSED: |
| 875 | case -EHOSTDOWN: |
| 876 | case -EHOSTUNREACH: |
| 877 | case -ENETUNREACH: |
| 878 | case -EIO: |
| 879 | case -ETIMEDOUT: |
| 880 | case -EPIPE: |
| 881 | dprintk("%s DS connection error %d\n", __func__, |
| 882 | task->tk_status); |
| 883 | nfs4_mark_deviceid_unavailable(devid); |
| 884 | rpc_wake_up(&tbl->slot_tbl_waitq); |
| 885 | /* fall through */ |
| 886 | default: |
| 887 | if (ff_layout_has_available_ds(lseg)) |
| 888 | return -NFS4ERR_RESET_TO_PNFS; |
| 889 | reset: |
| 890 | dprintk("%s Retry through MDS. Error %d\n", __func__, |
| 891 | task->tk_status); |
| 892 | return -NFS4ERR_RESET_TO_MDS; |
| 893 | } |
| 894 | out: |
| 895 | task->tk_status = 0; |
| 896 | return -EAGAIN; |
| 897 | out_bad_stateid: |
| 898 | task->tk_status = -EIO; |
| 899 | return 0; |
| 900 | wait_on_recovery: |
| 901 | rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL); |
| 902 | if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0) |
| 903 | rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task); |
| 904 | goto out; |
| 905 | } |
| 906 | |
| 907 | /* Retry all errors through either pNFS or MDS except for -EJUKEBOX */ |
| 908 | static int ff_layout_async_handle_error_v3(struct rpc_task *task, |
| 909 | struct pnfs_layout_segment *lseg, |
| 910 | int idx) |
| 911 | { |
| 912 | struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx); |
| 913 | |
| 914 | if (task->tk_status >= 0) |
| 915 | return 0; |
| 916 | |
| 917 | if (task->tk_status != -EJUKEBOX) { |
| 918 | dprintk("%s DS connection error %d\n", __func__, |
| 919 | task->tk_status); |
| 920 | nfs4_mark_deviceid_unavailable(devid); |
| 921 | if (ff_layout_has_available_ds(lseg)) |
| 922 | return -NFS4ERR_RESET_TO_PNFS; |
| 923 | else |
| 924 | return -NFS4ERR_RESET_TO_MDS; |
| 925 | } |
| 926 | |
| 927 | if (task->tk_status == -EJUKEBOX) |
| 928 | nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY); |
| 929 | task->tk_status = 0; |
| 930 | rpc_restart_call(task); |
| 931 | rpc_delay(task, NFS_JUKEBOX_RETRY_TIME); |
| 932 | return -EAGAIN; |
| 933 | } |
| 934 | |
| 935 | static int ff_layout_async_handle_error(struct rpc_task *task, |
| 936 | struct nfs4_state *state, |
| 937 | struct nfs_client *clp, |
| 938 | struct pnfs_layout_segment *lseg, |
| 939 | int idx) |
| 940 | { |
| 941 | int vers = clp->cl_nfs_mod->rpc_vers->number; |
| 942 | |
| 943 | switch (vers) { |
| 944 | case 3: |
| 945 | return ff_layout_async_handle_error_v3(task, lseg, idx); |
| 946 | case 4: |
| 947 | return ff_layout_async_handle_error_v4(task, state, clp, |
| 948 | lseg, idx); |
| 949 | default: |
| 950 | /* should never happen */ |
| 951 | WARN_ON_ONCE(1); |
| 952 | return 0; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg, |
| 957 | int idx, u64 offset, u64 length, |
| 958 | u32 status, int opnum) |
| 959 | { |
| 960 | struct nfs4_ff_layout_mirror *mirror; |
| 961 | int err; |
| 962 | |
| 963 | mirror = FF_LAYOUT_COMP(lseg, idx); |
| 964 | err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout), |
| 965 | mirror, offset, length, status, opnum, |
| 966 | GFP_NOIO); |
| 967 | dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status); |
| 968 | } |
| 969 | |
| 970 | /* NFS_PROTO call done callback routines */ |
| 971 | |
| 972 | static int ff_layout_read_done_cb(struct rpc_task *task, |
| 973 | struct nfs_pgio_header *hdr) |
| 974 | { |
| 975 | struct inode *inode; |
| 976 | int err; |
| 977 | |
| 978 | trace_nfs4_pnfs_read(hdr, task->tk_status); |
| 979 | if (task->tk_status == -ETIMEDOUT && !hdr->res.op_status) |
| 980 | hdr->res.op_status = NFS4ERR_NXIO; |
| 981 | if (task->tk_status < 0 && hdr->res.op_status) |
| 982 | ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx, |
| 983 | hdr->args.offset, hdr->args.count, |
| 984 | hdr->res.op_status, OP_READ); |
| 985 | err = ff_layout_async_handle_error(task, hdr->args.context->state, |
| 986 | hdr->ds_clp, hdr->lseg, |
| 987 | hdr->pgio_mirror_idx); |
| 988 | |
| 989 | switch (err) { |
| 990 | case -NFS4ERR_RESET_TO_PNFS: |
| 991 | set_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE, |
| 992 | &hdr->lseg->pls_layout->plh_flags); |
| 993 | pnfs_read_resend_pnfs(hdr); |
| 994 | return task->tk_status; |
| 995 | case -NFS4ERR_RESET_TO_MDS: |
| 996 | inode = hdr->lseg->pls_layout->plh_inode; |
| 997 | pnfs_error_mark_layout_for_return(inode, hdr->lseg); |
| 998 | ff_layout_reset_read(hdr); |
| 999 | return task->tk_status; |
| 1000 | case -EAGAIN: |
| 1001 | rpc_restart_call_prepare(task); |
| 1002 | return -EAGAIN; |
| 1003 | } |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * We reference the rpc_cred of the first WRITE that triggers the need for |
| 1010 | * a LAYOUTCOMMIT, and use it to send the layoutcommit compound. |
| 1011 | * rfc5661 is not clear about which credential should be used. |
| 1012 | * |
| 1013 | * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so |
| 1014 | * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751 |
| 1015 | * we always send layoutcommit after DS writes. |
| 1016 | */ |
| 1017 | static void |
| 1018 | ff_layout_set_layoutcommit(struct nfs_pgio_header *hdr) |
| 1019 | { |
Trond Myklebust | 67af761 | 2015-03-25 20:40:38 -0400 | [diff] [blame] | 1020 | pnfs_set_layoutcommit(hdr->inode, hdr->lseg, |
| 1021 | hdr->mds_offset + hdr->res.count); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1022 | dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino, |
| 1023 | (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb); |
| 1024 | } |
| 1025 | |
| 1026 | static bool |
| 1027 | ff_layout_reset_to_mds(struct pnfs_layout_segment *lseg, int idx) |
| 1028 | { |
| 1029 | /* No mirroring for now */ |
| 1030 | struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx); |
| 1031 | |
| 1032 | return ff_layout_test_devid_unavailable(node); |
| 1033 | } |
| 1034 | |
| 1035 | static int ff_layout_read_prepare_common(struct rpc_task *task, |
| 1036 | struct nfs_pgio_header *hdr) |
| 1037 | { |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1038 | nfs4_ff_layout_stat_io_start_read( |
| 1039 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), |
| 1040 | hdr->args.count); |
| 1041 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1042 | if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) { |
| 1043 | rpc_exit(task, -EIO); |
| 1044 | return -EIO; |
| 1045 | } |
| 1046 | if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) { |
| 1047 | dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid); |
| 1048 | if (ff_layout_has_available_ds(hdr->lseg)) |
| 1049 | pnfs_read_resend_pnfs(hdr); |
| 1050 | else |
| 1051 | ff_layout_reset_read(hdr); |
| 1052 | rpc_exit(task, 0); |
| 1053 | return -EAGAIN; |
| 1054 | } |
| 1055 | hdr->pgio_done_cb = ff_layout_read_done_cb; |
| 1056 | |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | * Call ops for the async read/write cases |
| 1062 | * In the case of dense layouts, the offset needs to be reset to its |
| 1063 | * original value. |
| 1064 | */ |
| 1065 | static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data) |
| 1066 | { |
| 1067 | struct nfs_pgio_header *hdr = data; |
| 1068 | |
| 1069 | if (ff_layout_read_prepare_common(task, hdr)) |
| 1070 | return; |
| 1071 | |
| 1072 | rpc_call_start(task); |
| 1073 | } |
| 1074 | |
| 1075 | static int ff_layout_setup_sequence(struct nfs_client *ds_clp, |
| 1076 | struct nfs4_sequence_args *args, |
| 1077 | struct nfs4_sequence_res *res, |
| 1078 | struct rpc_task *task) |
| 1079 | { |
| 1080 | if (ds_clp->cl_session) |
| 1081 | return nfs41_setup_sequence(ds_clp->cl_session, |
| 1082 | args, |
| 1083 | res, |
| 1084 | task); |
| 1085 | return nfs40_setup_sequence(ds_clp->cl_slot_tbl, |
| 1086 | args, |
| 1087 | res, |
| 1088 | task); |
| 1089 | } |
| 1090 | |
| 1091 | static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data) |
| 1092 | { |
| 1093 | struct nfs_pgio_header *hdr = data; |
| 1094 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1095 | if (ff_layout_setup_sequence(hdr->ds_clp, |
| 1096 | &hdr->args.seq_args, |
| 1097 | &hdr->res.seq_res, |
| 1098 | task)) |
| 1099 | return; |
| 1100 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1101 | if (ff_layout_read_prepare_common(task, hdr)) |
| 1102 | return; |
| 1103 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1104 | if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, |
| 1105 | hdr->args.lock_context, FMODE_READ) == -EIO) |
| 1106 | rpc_exit(task, -EIO); /* lost lock, terminate I/O */ |
| 1107 | } |
| 1108 | |
| 1109 | static void ff_layout_read_call_done(struct rpc_task *task, void *data) |
| 1110 | { |
| 1111 | struct nfs_pgio_header *hdr = data; |
| 1112 | |
| 1113 | dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status); |
| 1114 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1115 | nfs4_ff_layout_stat_io_end_read(task, |
| 1116 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), |
| 1117 | hdr->args.count, hdr->res.count); |
| 1118 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1119 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags) && |
| 1120 | task->tk_status == 0) { |
| 1121 | nfs4_sequence_done(task, &hdr->res.seq_res); |
| 1122 | return; |
| 1123 | } |
| 1124 | |
| 1125 | /* Note this may cause RPC to be resent */ |
| 1126 | hdr->mds_ops->rpc_call_done(task, hdr); |
| 1127 | } |
| 1128 | |
| 1129 | static void ff_layout_read_count_stats(struct rpc_task *task, void *data) |
| 1130 | { |
| 1131 | struct nfs_pgio_header *hdr = data; |
| 1132 | |
| 1133 | rpc_count_iostats_metrics(task, |
| 1134 | &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]); |
| 1135 | } |
| 1136 | |
| 1137 | static int ff_layout_write_done_cb(struct rpc_task *task, |
| 1138 | struct nfs_pgio_header *hdr) |
| 1139 | { |
| 1140 | struct inode *inode; |
| 1141 | int err; |
| 1142 | |
| 1143 | trace_nfs4_pnfs_write(hdr, task->tk_status); |
| 1144 | if (task->tk_status == -ETIMEDOUT && !hdr->res.op_status) |
| 1145 | hdr->res.op_status = NFS4ERR_NXIO; |
| 1146 | if (task->tk_status < 0 && hdr->res.op_status) |
| 1147 | ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx, |
| 1148 | hdr->args.offset, hdr->args.count, |
| 1149 | hdr->res.op_status, OP_WRITE); |
| 1150 | err = ff_layout_async_handle_error(task, hdr->args.context->state, |
| 1151 | hdr->ds_clp, hdr->lseg, |
| 1152 | hdr->pgio_mirror_idx); |
| 1153 | |
| 1154 | switch (err) { |
| 1155 | case -NFS4ERR_RESET_TO_PNFS: |
| 1156 | case -NFS4ERR_RESET_TO_MDS: |
| 1157 | inode = hdr->lseg->pls_layout->plh_inode; |
| 1158 | pnfs_error_mark_layout_for_return(inode, hdr->lseg); |
| 1159 | if (err == -NFS4ERR_RESET_TO_PNFS) { |
| 1160 | pnfs_set_retry_layoutget(hdr->lseg->pls_layout); |
| 1161 | ff_layout_reset_write(hdr, true); |
| 1162 | } else { |
| 1163 | pnfs_clear_retry_layoutget(hdr->lseg->pls_layout); |
| 1164 | ff_layout_reset_write(hdr, false); |
| 1165 | } |
| 1166 | return task->tk_status; |
| 1167 | case -EAGAIN: |
| 1168 | rpc_restart_call_prepare(task); |
| 1169 | return -EAGAIN; |
| 1170 | } |
| 1171 | |
| 1172 | if (hdr->res.verf->committed == NFS_FILE_SYNC || |
| 1173 | hdr->res.verf->committed == NFS_DATA_SYNC) |
| 1174 | ff_layout_set_layoutcommit(hdr); |
| 1175 | |
| 1176 | return 0; |
| 1177 | } |
| 1178 | |
| 1179 | static int ff_layout_commit_done_cb(struct rpc_task *task, |
| 1180 | struct nfs_commit_data *data) |
| 1181 | { |
| 1182 | struct inode *inode; |
| 1183 | int err; |
| 1184 | |
| 1185 | trace_nfs4_pnfs_commit_ds(data, task->tk_status); |
| 1186 | if (task->tk_status == -ETIMEDOUT && !data->res.op_status) |
| 1187 | data->res.op_status = NFS4ERR_NXIO; |
| 1188 | if (task->tk_status < 0 && data->res.op_status) |
| 1189 | ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index, |
| 1190 | data->args.offset, data->args.count, |
| 1191 | data->res.op_status, OP_COMMIT); |
| 1192 | err = ff_layout_async_handle_error(task, NULL, data->ds_clp, |
| 1193 | data->lseg, data->ds_commit_index); |
| 1194 | |
| 1195 | switch (err) { |
| 1196 | case -NFS4ERR_RESET_TO_PNFS: |
| 1197 | case -NFS4ERR_RESET_TO_MDS: |
| 1198 | inode = data->lseg->pls_layout->plh_inode; |
| 1199 | pnfs_error_mark_layout_for_return(inode, data->lseg); |
| 1200 | if (err == -NFS4ERR_RESET_TO_PNFS) |
| 1201 | pnfs_set_retry_layoutget(data->lseg->pls_layout); |
| 1202 | else |
| 1203 | pnfs_clear_retry_layoutget(data->lseg->pls_layout); |
| 1204 | pnfs_generic_prepare_to_resend_writes(data); |
| 1205 | return -EAGAIN; |
| 1206 | case -EAGAIN: |
| 1207 | rpc_restart_call_prepare(task); |
| 1208 | return -EAGAIN; |
| 1209 | } |
| 1210 | |
| 1211 | if (data->verf.committed == NFS_UNSTABLE) |
Trond Myklebust | 67af761 | 2015-03-25 20:40:38 -0400 | [diff] [blame] | 1212 | pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | static int ff_layout_write_prepare_common(struct rpc_task *task, |
| 1218 | struct nfs_pgio_header *hdr) |
| 1219 | { |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1220 | nfs4_ff_layout_stat_io_start_write( |
| 1221 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), |
| 1222 | hdr->args.count); |
| 1223 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1224 | if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) { |
| 1225 | rpc_exit(task, -EIO); |
| 1226 | return -EIO; |
| 1227 | } |
| 1228 | |
| 1229 | if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) { |
| 1230 | bool retry_pnfs; |
| 1231 | |
| 1232 | retry_pnfs = ff_layout_has_available_ds(hdr->lseg); |
| 1233 | dprintk("%s task %u reset io to %s\n", __func__, |
| 1234 | task->tk_pid, retry_pnfs ? "pNFS" : "MDS"); |
| 1235 | ff_layout_reset_write(hdr, retry_pnfs); |
| 1236 | rpc_exit(task, 0); |
| 1237 | return -EAGAIN; |
| 1238 | } |
| 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data) |
| 1244 | { |
| 1245 | struct nfs_pgio_header *hdr = data; |
| 1246 | |
| 1247 | if (ff_layout_write_prepare_common(task, hdr)) |
| 1248 | return; |
| 1249 | |
| 1250 | rpc_call_start(task); |
| 1251 | } |
| 1252 | |
| 1253 | static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data) |
| 1254 | { |
| 1255 | struct nfs_pgio_header *hdr = data; |
| 1256 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1257 | if (ff_layout_setup_sequence(hdr->ds_clp, |
| 1258 | &hdr->args.seq_args, |
| 1259 | &hdr->res.seq_res, |
| 1260 | task)) |
| 1261 | return; |
| 1262 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1263 | if (ff_layout_write_prepare_common(task, hdr)) |
| 1264 | return; |
| 1265 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1266 | if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, |
| 1267 | hdr->args.lock_context, FMODE_WRITE) == -EIO) |
| 1268 | rpc_exit(task, -EIO); /* lost lock, terminate I/O */ |
| 1269 | } |
| 1270 | |
| 1271 | static void ff_layout_write_call_done(struct rpc_task *task, void *data) |
| 1272 | { |
| 1273 | struct nfs_pgio_header *hdr = data; |
| 1274 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1275 | nfs4_ff_layout_stat_io_end_write(task, |
| 1276 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), |
| 1277 | hdr->args.count, hdr->res.count, |
| 1278 | hdr->res.verf->committed); |
| 1279 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1280 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags) && |
| 1281 | task->tk_status == 0) { |
| 1282 | nfs4_sequence_done(task, &hdr->res.seq_res); |
| 1283 | return; |
| 1284 | } |
| 1285 | |
| 1286 | /* Note this may cause RPC to be resent */ |
| 1287 | hdr->mds_ops->rpc_call_done(task, hdr); |
| 1288 | } |
| 1289 | |
| 1290 | static void ff_layout_write_count_stats(struct rpc_task *task, void *data) |
| 1291 | { |
| 1292 | struct nfs_pgio_header *hdr = data; |
| 1293 | |
| 1294 | rpc_count_iostats_metrics(task, |
| 1295 | &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]); |
| 1296 | } |
| 1297 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1298 | static void ff_layout_commit_prepare_common(struct rpc_task *task, |
| 1299 | struct nfs_commit_data *cdata) |
| 1300 | { |
| 1301 | nfs4_ff_layout_stat_io_start_write( |
| 1302 | FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index), |
| 1303 | 0); |
| 1304 | } |
| 1305 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1306 | static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data) |
| 1307 | { |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1308 | ff_layout_commit_prepare_common(task, data); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1309 | rpc_call_start(task); |
| 1310 | } |
| 1311 | |
| 1312 | static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data) |
| 1313 | { |
| 1314 | struct nfs_commit_data *wdata = data; |
| 1315 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1316 | if (ff_layout_setup_sequence(wdata->ds_clp, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1317 | &wdata->args.seq_args, |
| 1318 | &wdata->res.seq_res, |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1319 | task)) |
| 1320 | return; |
| 1321 | ff_layout_commit_prepare_common(task, data); |
| 1322 | } |
| 1323 | |
| 1324 | static void ff_layout_commit_done(struct rpc_task *task, void *data) |
| 1325 | { |
| 1326 | struct nfs_commit_data *cdata = data; |
| 1327 | struct nfs_page *req; |
| 1328 | __u64 count = 0; |
| 1329 | |
| 1330 | if (task->tk_status == 0) { |
| 1331 | list_for_each_entry(req, &cdata->pages, wb_list) |
| 1332 | count += req->wb_bytes; |
| 1333 | } |
| 1334 | |
| 1335 | nfs4_ff_layout_stat_io_end_write(task, |
| 1336 | FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index), |
| 1337 | count, count, NFS_FILE_SYNC); |
| 1338 | |
| 1339 | pnfs_generic_write_commit_done(task, data); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | static void ff_layout_commit_count_stats(struct rpc_task *task, void *data) |
| 1343 | { |
| 1344 | struct nfs_commit_data *cdata = data; |
| 1345 | |
| 1346 | rpc_count_iostats_metrics(task, |
| 1347 | &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]); |
| 1348 | } |
| 1349 | |
| 1350 | static const struct rpc_call_ops ff_layout_read_call_ops_v3 = { |
| 1351 | .rpc_call_prepare = ff_layout_read_prepare_v3, |
| 1352 | .rpc_call_done = ff_layout_read_call_done, |
| 1353 | .rpc_count_stats = ff_layout_read_count_stats, |
| 1354 | .rpc_release = pnfs_generic_rw_release, |
| 1355 | }; |
| 1356 | |
| 1357 | static const struct rpc_call_ops ff_layout_read_call_ops_v4 = { |
| 1358 | .rpc_call_prepare = ff_layout_read_prepare_v4, |
| 1359 | .rpc_call_done = ff_layout_read_call_done, |
| 1360 | .rpc_count_stats = ff_layout_read_count_stats, |
| 1361 | .rpc_release = pnfs_generic_rw_release, |
| 1362 | }; |
| 1363 | |
| 1364 | static const struct rpc_call_ops ff_layout_write_call_ops_v3 = { |
| 1365 | .rpc_call_prepare = ff_layout_write_prepare_v3, |
| 1366 | .rpc_call_done = ff_layout_write_call_done, |
| 1367 | .rpc_count_stats = ff_layout_write_count_stats, |
| 1368 | .rpc_release = pnfs_generic_rw_release, |
| 1369 | }; |
| 1370 | |
| 1371 | static const struct rpc_call_ops ff_layout_write_call_ops_v4 = { |
| 1372 | .rpc_call_prepare = ff_layout_write_prepare_v4, |
| 1373 | .rpc_call_done = ff_layout_write_call_done, |
| 1374 | .rpc_count_stats = ff_layout_write_count_stats, |
| 1375 | .rpc_release = pnfs_generic_rw_release, |
| 1376 | }; |
| 1377 | |
| 1378 | static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = { |
| 1379 | .rpc_call_prepare = ff_layout_commit_prepare_v3, |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1380 | .rpc_call_done = ff_layout_commit_done, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1381 | .rpc_count_stats = ff_layout_commit_count_stats, |
| 1382 | .rpc_release = pnfs_generic_commit_release, |
| 1383 | }; |
| 1384 | |
| 1385 | static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = { |
| 1386 | .rpc_call_prepare = ff_layout_commit_prepare_v4, |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1387 | .rpc_call_done = ff_layout_commit_done, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1388 | .rpc_count_stats = ff_layout_commit_count_stats, |
| 1389 | .rpc_release = pnfs_generic_commit_release, |
| 1390 | }; |
| 1391 | |
| 1392 | static enum pnfs_try_status |
| 1393 | ff_layout_read_pagelist(struct nfs_pgio_header *hdr) |
| 1394 | { |
| 1395 | struct pnfs_layout_segment *lseg = hdr->lseg; |
| 1396 | struct nfs4_pnfs_ds *ds; |
| 1397 | struct rpc_clnt *ds_clnt; |
| 1398 | struct rpc_cred *ds_cred; |
| 1399 | loff_t offset = hdr->args.offset; |
| 1400 | u32 idx = hdr->pgio_mirror_idx; |
| 1401 | int vers; |
| 1402 | struct nfs_fh *fh; |
| 1403 | |
| 1404 | dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n", |
| 1405 | __func__, hdr->inode->i_ino, |
| 1406 | hdr->args.pgbase, (size_t)hdr->args.count, offset); |
| 1407 | |
| 1408 | ds = nfs4_ff_layout_prepare_ds(lseg, idx, false); |
| 1409 | if (!ds) |
| 1410 | goto out_failed; |
| 1411 | |
| 1412 | ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, |
| 1413 | hdr->inode); |
| 1414 | if (IS_ERR(ds_clnt)) |
| 1415 | goto out_failed; |
| 1416 | |
| 1417 | ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred); |
| 1418 | if (IS_ERR(ds_cred)) |
| 1419 | goto out_failed; |
| 1420 | |
| 1421 | vers = nfs4_ff_layout_ds_version(lseg, idx); |
| 1422 | |
| 1423 | dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__, |
| 1424 | ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers); |
| 1425 | |
| 1426 | atomic_inc(&ds->ds_clp->cl_count); |
| 1427 | hdr->ds_clp = ds->ds_clp; |
| 1428 | fh = nfs4_ff_layout_select_ds_fh(lseg, idx); |
| 1429 | if (fh) |
| 1430 | hdr->args.fh = fh; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1431 | /* |
| 1432 | * Note that if we ever decide to split across DSes, |
| 1433 | * then we may need to handle dense-like offsets. |
| 1434 | */ |
| 1435 | hdr->args.offset = offset; |
| 1436 | hdr->mds_offset = offset; |
| 1437 | |
| 1438 | /* Perform an asynchronous read to ds */ |
| 1439 | nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, |
| 1440 | vers == 3 ? &ff_layout_read_call_ops_v3 : |
| 1441 | &ff_layout_read_call_ops_v4, |
| 1442 | 0, RPC_TASK_SOFTCONN); |
| 1443 | |
| 1444 | return PNFS_ATTEMPTED; |
| 1445 | |
| 1446 | out_failed: |
| 1447 | if (ff_layout_has_available_ds(lseg)) |
| 1448 | return PNFS_TRY_AGAIN; |
| 1449 | return PNFS_NOT_ATTEMPTED; |
| 1450 | } |
| 1451 | |
| 1452 | /* Perform async writes. */ |
| 1453 | static enum pnfs_try_status |
| 1454 | ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync) |
| 1455 | { |
| 1456 | struct pnfs_layout_segment *lseg = hdr->lseg; |
| 1457 | struct nfs4_pnfs_ds *ds; |
| 1458 | struct rpc_clnt *ds_clnt; |
| 1459 | struct rpc_cred *ds_cred; |
| 1460 | loff_t offset = hdr->args.offset; |
| 1461 | int vers; |
| 1462 | struct nfs_fh *fh; |
| 1463 | int idx = hdr->pgio_mirror_idx; |
| 1464 | |
| 1465 | ds = nfs4_ff_layout_prepare_ds(lseg, idx, true); |
| 1466 | if (!ds) |
| 1467 | return PNFS_NOT_ATTEMPTED; |
| 1468 | |
| 1469 | ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, |
| 1470 | hdr->inode); |
| 1471 | if (IS_ERR(ds_clnt)) |
| 1472 | return PNFS_NOT_ATTEMPTED; |
| 1473 | |
| 1474 | ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred); |
| 1475 | if (IS_ERR(ds_cred)) |
| 1476 | return PNFS_NOT_ATTEMPTED; |
| 1477 | |
| 1478 | vers = nfs4_ff_layout_ds_version(lseg, idx); |
| 1479 | |
| 1480 | dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n", |
| 1481 | __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count, |
| 1482 | offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), |
| 1483 | vers); |
| 1484 | |
| 1485 | hdr->pgio_done_cb = ff_layout_write_done_cb; |
| 1486 | atomic_inc(&ds->ds_clp->cl_count); |
| 1487 | hdr->ds_clp = ds->ds_clp; |
| 1488 | hdr->ds_commit_idx = idx; |
| 1489 | fh = nfs4_ff_layout_select_ds_fh(lseg, idx); |
| 1490 | if (fh) |
| 1491 | hdr->args.fh = fh; |
| 1492 | |
| 1493 | /* |
| 1494 | * Note that if we ever decide to split across DSes, |
| 1495 | * then we may need to handle dense-like offsets. |
| 1496 | */ |
| 1497 | hdr->args.offset = offset; |
| 1498 | |
| 1499 | /* Perform an asynchronous write */ |
| 1500 | nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, |
| 1501 | vers == 3 ? &ff_layout_write_call_ops_v3 : |
| 1502 | &ff_layout_write_call_ops_v4, |
| 1503 | sync, RPC_TASK_SOFTCONN); |
| 1504 | return PNFS_ATTEMPTED; |
| 1505 | } |
| 1506 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1507 | static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i) |
| 1508 | { |
| 1509 | return i; |
| 1510 | } |
| 1511 | |
| 1512 | static struct nfs_fh * |
| 1513 | select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i) |
| 1514 | { |
| 1515 | struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg); |
| 1516 | |
| 1517 | /* FIXME: Assume that there is only one NFS version available |
| 1518 | * for the DS. |
| 1519 | */ |
| 1520 | return &flseg->mirror_array[i]->fh_versions[0]; |
| 1521 | } |
| 1522 | |
| 1523 | static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how) |
| 1524 | { |
| 1525 | struct pnfs_layout_segment *lseg = data->lseg; |
| 1526 | struct nfs4_pnfs_ds *ds; |
| 1527 | struct rpc_clnt *ds_clnt; |
| 1528 | struct rpc_cred *ds_cred; |
| 1529 | u32 idx; |
| 1530 | int vers; |
| 1531 | struct nfs_fh *fh; |
| 1532 | |
| 1533 | idx = calc_ds_index_from_commit(lseg, data->ds_commit_index); |
| 1534 | ds = nfs4_ff_layout_prepare_ds(lseg, idx, true); |
| 1535 | if (!ds) |
| 1536 | goto out_err; |
| 1537 | |
| 1538 | ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, |
| 1539 | data->inode); |
| 1540 | if (IS_ERR(ds_clnt)) |
| 1541 | goto out_err; |
| 1542 | |
| 1543 | ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred); |
| 1544 | if (IS_ERR(ds_cred)) |
| 1545 | goto out_err; |
| 1546 | |
| 1547 | vers = nfs4_ff_layout_ds_version(lseg, idx); |
| 1548 | |
| 1549 | dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__, |
| 1550 | data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count), |
| 1551 | vers); |
| 1552 | data->commit_done_cb = ff_layout_commit_done_cb; |
| 1553 | data->cred = ds_cred; |
| 1554 | atomic_inc(&ds->ds_clp->cl_count); |
| 1555 | data->ds_clp = ds->ds_clp; |
| 1556 | fh = select_ds_fh_from_commit(lseg, data->ds_commit_index); |
| 1557 | if (fh) |
| 1558 | data->args.fh = fh; |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1559 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1560 | return nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops, |
| 1561 | vers == 3 ? &ff_layout_commit_call_ops_v3 : |
| 1562 | &ff_layout_commit_call_ops_v4, |
| 1563 | how, RPC_TASK_SOFTCONN); |
| 1564 | out_err: |
| 1565 | pnfs_generic_prepare_to_resend_writes(data); |
| 1566 | pnfs_generic_commit_release(data); |
| 1567 | return -EAGAIN; |
| 1568 | } |
| 1569 | |
| 1570 | static int |
| 1571 | ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages, |
| 1572 | int how, struct nfs_commit_info *cinfo) |
| 1573 | { |
| 1574 | return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo, |
| 1575 | ff_layout_initiate_commit); |
| 1576 | } |
| 1577 | |
| 1578 | static struct pnfs_ds_commit_info * |
| 1579 | ff_layout_get_ds_info(struct inode *inode) |
| 1580 | { |
| 1581 | struct pnfs_layout_hdr *layout = NFS_I(inode)->layout; |
| 1582 | |
| 1583 | if (layout == NULL) |
| 1584 | return NULL; |
| 1585 | |
| 1586 | return &FF_LAYOUT_FROM_HDR(layout)->commit_info; |
| 1587 | } |
| 1588 | |
| 1589 | static void |
Trond Myklebust | fc87701 | 2015-03-09 17:25:14 -0400 | [diff] [blame] | 1590 | ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1591 | { |
| 1592 | nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds, |
| 1593 | id_node)); |
| 1594 | } |
| 1595 | |
| 1596 | static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo, |
| 1597 | struct xdr_stream *xdr, |
| 1598 | const struct nfs4_layoutreturn_args *args) |
| 1599 | { |
| 1600 | struct pnfs_layout_hdr *hdr = &flo->generic_hdr; |
| 1601 | __be32 *start; |
| 1602 | int count = 0, ret = 0; |
| 1603 | |
| 1604 | start = xdr_reserve_space(xdr, 4); |
| 1605 | if (unlikely(!start)) |
| 1606 | return -E2BIG; |
| 1607 | |
| 1608 | /* This assume we always return _ALL_ layouts */ |
| 1609 | spin_lock(&hdr->plh_inode->i_lock); |
| 1610 | ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range); |
| 1611 | spin_unlock(&hdr->plh_inode->i_lock); |
| 1612 | |
| 1613 | *start = cpu_to_be32(count); |
| 1614 | |
| 1615 | return ret; |
| 1616 | } |
| 1617 | |
| 1618 | /* report nothing for now */ |
| 1619 | static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo, |
| 1620 | struct xdr_stream *xdr, |
| 1621 | const struct nfs4_layoutreturn_args *args) |
| 1622 | { |
| 1623 | __be32 *p; |
| 1624 | |
| 1625 | p = xdr_reserve_space(xdr, 4); |
| 1626 | if (likely(p)) |
| 1627 | *p = cpu_to_be32(0); |
| 1628 | } |
| 1629 | |
| 1630 | static struct nfs4_deviceid_node * |
| 1631 | ff_layout_alloc_deviceid_node(struct nfs_server *server, |
| 1632 | struct pnfs_device *pdev, gfp_t gfp_flags) |
| 1633 | { |
| 1634 | struct nfs4_ff_layout_ds *dsaddr; |
| 1635 | |
| 1636 | dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags); |
| 1637 | if (!dsaddr) |
| 1638 | return NULL; |
| 1639 | return &dsaddr->id_node; |
| 1640 | } |
| 1641 | |
| 1642 | static void |
| 1643 | ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo, |
| 1644 | struct xdr_stream *xdr, |
| 1645 | const struct nfs4_layoutreturn_args *args) |
| 1646 | { |
| 1647 | struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo); |
| 1648 | __be32 *start; |
| 1649 | |
| 1650 | dprintk("%s: Begin\n", __func__); |
| 1651 | start = xdr_reserve_space(xdr, 4); |
| 1652 | BUG_ON(!start); |
| 1653 | |
| 1654 | if (ff_layout_encode_ioerr(flo, xdr, args)) |
| 1655 | goto out; |
| 1656 | |
| 1657 | ff_layout_encode_iostats(flo, xdr, args); |
| 1658 | out: |
| 1659 | *start = cpu_to_be32((xdr->p - start - 1) * 4); |
| 1660 | dprintk("%s: Return\n", __func__); |
| 1661 | } |
| 1662 | |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame^] | 1663 | static bool |
| 1664 | ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args, |
| 1665 | struct pnfs_layout_segment *pls, |
| 1666 | int *dev_count, int dev_limit) |
| 1667 | { |
| 1668 | struct nfs4_ff_layout_mirror *mirror; |
| 1669 | struct nfs4_deviceid_node *dev; |
| 1670 | struct nfs42_layoutstat_devinfo *devinfo; |
| 1671 | int i; |
| 1672 | |
| 1673 | for (i = 0; i <= FF_LAYOUT_MIRROR_COUNT(pls); i++) { |
| 1674 | if (*dev_count >= dev_limit) |
| 1675 | break; |
| 1676 | mirror = FF_LAYOUT_COMP(pls, i); |
| 1677 | dev = FF_LAYOUT_DEVID_NODE(pls, i); |
| 1678 | devinfo = &args->devinfo[*dev_count]; |
| 1679 | memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE); |
| 1680 | devinfo->offset = pls->pls_range.offset; |
| 1681 | devinfo->length = pls->pls_range.length; |
| 1682 | /* well, we don't really know if IO is continuous or not! */ |
| 1683 | devinfo->read_count = mirror->read_stat.io_stat.bytes_completed; |
| 1684 | devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed; |
| 1685 | devinfo->write_count = mirror->write_stat.io_stat.bytes_completed; |
| 1686 | devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed; |
| 1687 | devinfo->layout_type = LAYOUT_FLEX_FILES; |
| 1688 | devinfo->layoutstats_encode = NULL; |
| 1689 | devinfo->layout_private = NULL; |
| 1690 | |
| 1691 | ++(*dev_count); |
| 1692 | } |
| 1693 | |
| 1694 | return *dev_count < dev_limit; |
| 1695 | } |
| 1696 | |
| 1697 | static int |
| 1698 | ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args) |
| 1699 | { |
| 1700 | struct pnfs_layout_segment *pls; |
| 1701 | int dev_count = 0; |
| 1702 | |
| 1703 | spin_lock(&args->inode->i_lock); |
| 1704 | list_for_each_entry(pls, &NFS_I(args->inode)->layout->plh_segs, pls_list) { |
| 1705 | dev_count += FF_LAYOUT_MIRROR_COUNT(pls); |
| 1706 | } |
| 1707 | spin_unlock(&args->inode->i_lock); |
| 1708 | /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */ |
| 1709 | if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) { |
| 1710 | dprintk("%s: truncating devinfo to limit (%d:%d)\n", |
| 1711 | __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV); |
| 1712 | dev_count = PNFS_LAYOUTSTATS_MAXDEV; |
| 1713 | } |
| 1714 | args->devinfo = kmalloc(dev_count * sizeof(*args->devinfo), GFP_KERNEL); |
| 1715 | if (!args->devinfo) |
| 1716 | return -ENOMEM; |
| 1717 | |
| 1718 | dev_count = 0; |
| 1719 | spin_lock(&args->inode->i_lock); |
| 1720 | list_for_each_entry(pls, &NFS_I(args->inode)->layout->plh_segs, pls_list) { |
| 1721 | if (!ff_layout_mirror_prepare_stats(args, pls, &dev_count, |
| 1722 | PNFS_LAYOUTSTATS_MAXDEV)) { |
| 1723 | break; |
| 1724 | } |
| 1725 | } |
| 1726 | spin_unlock(&args->inode->i_lock); |
| 1727 | args->num_dev = dev_count; |
| 1728 | |
| 1729 | return 0; |
| 1730 | } |
| 1731 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1732 | static struct pnfs_layoutdriver_type flexfilelayout_type = { |
| 1733 | .id = LAYOUT_FLEX_FILES, |
| 1734 | .name = "LAYOUT_FLEX_FILES", |
| 1735 | .owner = THIS_MODULE, |
| 1736 | .alloc_layout_hdr = ff_layout_alloc_layout_hdr, |
| 1737 | .free_layout_hdr = ff_layout_free_layout_hdr, |
| 1738 | .alloc_lseg = ff_layout_alloc_lseg, |
| 1739 | .free_lseg = ff_layout_free_lseg, |
| 1740 | .pg_read_ops = &ff_layout_pg_read_ops, |
| 1741 | .pg_write_ops = &ff_layout_pg_write_ops, |
| 1742 | .get_ds_info = ff_layout_get_ds_info, |
Trond Myklebust | fc87701 | 2015-03-09 17:25:14 -0400 | [diff] [blame] | 1743 | .free_deviceid_node = ff_layout_free_deviceid_node, |
Tom Haynes | 338d00c | 2015-02-17 14:58:15 -0800 | [diff] [blame] | 1744 | .mark_request_commit = pnfs_layout_mark_request_commit, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1745 | .clear_request_commit = pnfs_generic_clear_request_commit, |
| 1746 | .scan_commit_lists = pnfs_generic_scan_commit_lists, |
| 1747 | .recover_commit_reqs = pnfs_generic_recover_commit_reqs, |
| 1748 | .commit_pagelist = ff_layout_commit_pagelist, |
| 1749 | .read_pagelist = ff_layout_read_pagelist, |
| 1750 | .write_pagelist = ff_layout_write_pagelist, |
| 1751 | .alloc_deviceid_node = ff_layout_alloc_deviceid_node, |
| 1752 | .encode_layoutreturn = ff_layout_encode_layoutreturn, |
Trond Myklebust | 5bb89b4 | 2015-03-25 14:14:42 -0400 | [diff] [blame] | 1753 | .sync = pnfs_nfs_generic_sync, |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame^] | 1754 | .prepare_layoutstats = ff_layout_prepare_layoutstats, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1755 | }; |
| 1756 | |
| 1757 | static int __init nfs4flexfilelayout_init(void) |
| 1758 | { |
| 1759 | printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n", |
| 1760 | __func__); |
| 1761 | return pnfs_register_layoutdriver(&flexfilelayout_type); |
| 1762 | } |
| 1763 | |
| 1764 | static void __exit nfs4flexfilelayout_exit(void) |
| 1765 | { |
| 1766 | printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n", |
| 1767 | __func__); |
| 1768 | pnfs_unregister_layoutdriver(&flexfilelayout_type); |
| 1769 | } |
| 1770 | |
| 1771 | MODULE_ALIAS("nfs-layouttype4-4"); |
| 1772 | |
| 1773 | MODULE_LICENSE("GPL"); |
| 1774 | MODULE_DESCRIPTION("The NFSv4 flexfile layout driver"); |
| 1775 | |
| 1776 | module_init(nfs4flexfilelayout_init); |
| 1777 | module_exit(nfs4flexfilelayout_exit); |