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