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) |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 28 | #define FF_LAYOUTRETURN_MAXERR 20 |
| 29 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 30 | |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 31 | static struct group_info *ff_zero_group; |
| 32 | |
Trond Myklebust | 54e4a0d | 2016-11-27 15:12:39 -0500 | [diff] [blame] | 33 | static void ff_layout_read_record_layoutstats_done(struct rpc_task *task, |
| 34 | struct nfs_pgio_header *hdr); |
Trond Myklebust | 230bc96 | 2016-10-19 15:59:28 -0400 | [diff] [blame] | 35 | static int ff_layout_mirror_prepare_stats(struct pnfs_layout_hdr *lo, |
| 36 | struct nfs42_layoutstat_devinfo *devinfo, |
| 37 | int dev_limit); |
| 38 | static void ff_layout_encode_ff_layoutupdate(struct xdr_stream *xdr, |
| 39 | const struct nfs42_layoutstat_devinfo *devinfo, |
| 40 | struct nfs4_ff_layout_mirror *mirror); |
Trond Myklebust | 54e4a0d | 2016-11-27 15:12:39 -0500 | [diff] [blame] | 41 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 42 | static struct pnfs_layout_hdr * |
| 43 | ff_layout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags) |
| 44 | { |
| 45 | struct nfs4_flexfile_layout *ffl; |
| 46 | |
| 47 | ffl = kzalloc(sizeof(*ffl), gfp_flags); |
| 48 | if (ffl) { |
| 49 | INIT_LIST_HEAD(&ffl->error_list); |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 50 | INIT_LIST_HEAD(&ffl->mirrors); |
Trond Myklebust | 1c8d477 | 2016-08-14 12:47:49 -0400 | [diff] [blame] | 51 | ffl->last_report_time = ktime_get(); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 52 | return &ffl->generic_hdr; |
| 53 | } else |
| 54 | return NULL; |
| 55 | } |
| 56 | |
| 57 | static void |
| 58 | ff_layout_free_layout_hdr(struct pnfs_layout_hdr *lo) |
| 59 | { |
| 60 | struct nfs4_ff_layout_ds_err *err, *n; |
| 61 | |
| 62 | list_for_each_entry_safe(err, n, &FF_LAYOUT_FROM_HDR(lo)->error_list, |
| 63 | list) { |
| 64 | list_del(&err->list); |
| 65 | kfree(err); |
| 66 | } |
| 67 | kfree(FF_LAYOUT_FROM_HDR(lo)); |
| 68 | } |
| 69 | |
Trond Myklebust | 93b717f | 2016-05-16 17:42:43 -0400 | [diff] [blame] | 70 | static int decode_pnfs_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 71 | { |
| 72 | __be32 *p; |
| 73 | |
| 74 | p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE); |
| 75 | if (unlikely(p == NULL)) |
| 76 | return -ENOBUFS; |
Trond Myklebust | 93b717f | 2016-05-16 17:42:43 -0400 | [diff] [blame] | 77 | stateid->type = NFS4_PNFS_DS_STATEID_TYPE; |
| 78 | memcpy(stateid->data, p, NFS4_STATEID_SIZE); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 79 | dprintk("%s: stateid id= [%x%x%x%x]\n", __func__, |
| 80 | p[0], p[1], p[2], p[3]); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int decode_deviceid(struct xdr_stream *xdr, struct nfs4_deviceid *devid) |
| 85 | { |
| 86 | __be32 *p; |
| 87 | |
| 88 | p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE); |
| 89 | if (unlikely(!p)) |
| 90 | return -ENOBUFS; |
| 91 | memcpy(devid, p, NFS4_DEVICEID4_SIZE); |
| 92 | nfs4_print_deviceid(devid); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int decode_nfs_fh(struct xdr_stream *xdr, struct nfs_fh *fh) |
| 97 | { |
| 98 | __be32 *p; |
| 99 | |
| 100 | p = xdr_inline_decode(xdr, 4); |
| 101 | if (unlikely(!p)) |
| 102 | return -ENOBUFS; |
| 103 | fh->size = be32_to_cpup(p++); |
| 104 | if (fh->size > sizeof(struct nfs_fh)) { |
| 105 | printk(KERN_ERR "NFS flexfiles: Too big fh received %d\n", |
| 106 | fh->size); |
| 107 | return -EOVERFLOW; |
| 108 | } |
| 109 | /* fh.data */ |
| 110 | p = xdr_inline_decode(xdr, fh->size); |
| 111 | if (unlikely(!p)) |
| 112 | return -ENOBUFS; |
| 113 | memcpy(&fh->data, p, fh->size); |
| 114 | dprintk("%s: fh len %d\n", __func__, fh->size); |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Currently only stringified uids and gids are accepted. |
| 121 | * I.e., kerberos is not supported to the DSes, so no pricipals. |
| 122 | * |
| 123 | * That means that one common function will suffice, but when |
| 124 | * principals are added, this should be split to accomodate |
| 125 | * calls to both nfs_map_name_to_uid() and nfs_map_group_to_gid(). |
| 126 | */ |
| 127 | static int |
| 128 | decode_name(struct xdr_stream *xdr, u32 *id) |
| 129 | { |
| 130 | __be32 *p; |
| 131 | int len; |
| 132 | |
| 133 | /* opaque_length(4)*/ |
| 134 | p = xdr_inline_decode(xdr, 4); |
| 135 | if (unlikely(!p)) |
| 136 | return -ENOBUFS; |
| 137 | len = be32_to_cpup(p++); |
| 138 | if (len < 0) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | dprintk("%s: len %u\n", __func__, len); |
| 142 | |
| 143 | /* opaque body */ |
| 144 | p = xdr_inline_decode(xdr, len); |
| 145 | if (unlikely(!p)) |
| 146 | return -ENOBUFS; |
| 147 | |
| 148 | if (!nfs_map_string_to_numeric((char *)p, len, id)) |
| 149 | return -EINVAL; |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 154 | static bool ff_mirror_match_fh(const struct nfs4_ff_layout_mirror *m1, |
| 155 | const struct nfs4_ff_layout_mirror *m2) |
| 156 | { |
| 157 | int i, j; |
| 158 | |
| 159 | if (m1->fh_versions_cnt != m2->fh_versions_cnt) |
| 160 | return false; |
| 161 | for (i = 0; i < m1->fh_versions_cnt; i++) { |
| 162 | bool found_fh = false; |
Trond Myklebust | 86fb449 | 2015-12-30 10:57:01 -0500 | [diff] [blame] | 163 | for (j = 0; j < m2->fh_versions_cnt; j++) { |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 164 | if (nfs_compare_fh(&m1->fh_versions[i], |
| 165 | &m2->fh_versions[j]) == 0) { |
| 166 | found_fh = true; |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | if (!found_fh) |
| 171 | return false; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | static struct nfs4_ff_layout_mirror * |
| 177 | ff_layout_add_mirror(struct pnfs_layout_hdr *lo, |
| 178 | struct nfs4_ff_layout_mirror *mirror) |
| 179 | { |
| 180 | struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo); |
| 181 | struct nfs4_ff_layout_mirror *pos; |
| 182 | struct inode *inode = lo->plh_inode; |
| 183 | |
| 184 | spin_lock(&inode->i_lock); |
| 185 | list_for_each_entry(pos, &ff_layout->mirrors, mirrors) { |
Fred Isaman | 65990d1 | 2016-09-30 14:37:41 -0400 | [diff] [blame] | 186 | if (memcmp(&mirror->devid, &pos->devid, sizeof(pos->devid)) != 0) |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 187 | continue; |
| 188 | if (!ff_mirror_match_fh(mirror, pos)) |
| 189 | continue; |
| 190 | if (atomic_inc_not_zero(&pos->ref)) { |
| 191 | spin_unlock(&inode->i_lock); |
| 192 | return pos; |
| 193 | } |
| 194 | } |
| 195 | list_add(&mirror->mirrors, &ff_layout->mirrors); |
| 196 | mirror->layout = lo; |
| 197 | spin_unlock(&inode->i_lock); |
| 198 | return mirror; |
| 199 | } |
| 200 | |
kbuild test robot | e3b1df2 | 2015-08-25 11:19:25 +0800 | [diff] [blame] | 201 | static void |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 202 | ff_layout_remove_mirror(struct nfs4_ff_layout_mirror *mirror) |
| 203 | { |
| 204 | struct inode *inode; |
| 205 | if (mirror->layout == NULL) |
| 206 | return; |
| 207 | inode = mirror->layout->plh_inode; |
| 208 | spin_lock(&inode->i_lock); |
| 209 | list_del(&mirror->mirrors); |
| 210 | spin_unlock(&inode->i_lock); |
| 211 | mirror->layout = NULL; |
| 212 | } |
| 213 | |
Trond Myklebust | 28a0d72 | 2015-08-24 18:08:30 -0400 | [diff] [blame] | 214 | static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(gfp_t gfp_flags) |
| 215 | { |
| 216 | struct nfs4_ff_layout_mirror *mirror; |
| 217 | |
| 218 | mirror = kzalloc(sizeof(*mirror), gfp_flags); |
| 219 | if (mirror != NULL) { |
| 220 | spin_lock_init(&mirror->lock); |
| 221 | atomic_set(&mirror->ref, 1); |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 222 | INIT_LIST_HEAD(&mirror->mirrors); |
Trond Myklebust | 28a0d72 | 2015-08-24 18:08:30 -0400 | [diff] [blame] | 223 | } |
| 224 | return mirror; |
| 225 | } |
| 226 | |
| 227 | static void ff_layout_free_mirror(struct nfs4_ff_layout_mirror *mirror) |
| 228 | { |
Jeff Layton | 3064b68 | 2016-04-21 20:52:00 -0400 | [diff] [blame] | 229 | struct rpc_cred *cred; |
| 230 | |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 231 | ff_layout_remove_mirror(mirror); |
Trond Myklebust | 28a0d72 | 2015-08-24 18:08:30 -0400 | [diff] [blame] | 232 | kfree(mirror->fh_versions); |
Jeff Layton | 3064b68 | 2016-04-21 20:52:00 -0400 | [diff] [blame] | 233 | cred = rcu_access_pointer(mirror->ro_cred); |
| 234 | if (cred) |
| 235 | put_rpccred(cred); |
| 236 | cred = rcu_access_pointer(mirror->rw_cred); |
| 237 | if (cred) |
| 238 | put_rpccred(cred); |
Trond Myklebust | 28a0d72 | 2015-08-24 18:08:30 -0400 | [diff] [blame] | 239 | nfs4_ff_layout_put_deviceid(mirror->mirror_ds); |
| 240 | kfree(mirror); |
| 241 | } |
| 242 | |
| 243 | static void ff_layout_put_mirror(struct nfs4_ff_layout_mirror *mirror) |
| 244 | { |
| 245 | if (mirror != NULL && atomic_dec_and_test(&mirror->ref)) |
| 246 | ff_layout_free_mirror(mirror); |
| 247 | } |
| 248 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 249 | static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment *fls) |
| 250 | { |
| 251 | int i; |
| 252 | |
| 253 | if (fls->mirror_array) { |
| 254 | for (i = 0; i < fls->mirror_array_cnt; i++) { |
| 255 | /* normally mirror_ds is freed in |
| 256 | * .free_deviceid_node but we still do it here |
| 257 | * for .alloc_lseg error path */ |
Trond Myklebust | 28a0d72 | 2015-08-24 18:08:30 -0400 | [diff] [blame] | 258 | ff_layout_put_mirror(fls->mirror_array[i]); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 259 | } |
| 260 | kfree(fls->mirror_array); |
| 261 | fls->mirror_array = NULL; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | static int ff_layout_check_layout(struct nfs4_layoutget_res *lgr) |
| 266 | { |
| 267 | int ret = 0; |
| 268 | |
| 269 | dprintk("--> %s\n", __func__); |
| 270 | |
| 271 | /* FIXME: remove this check when layout segment support is added */ |
| 272 | if (lgr->range.offset != 0 || |
| 273 | lgr->range.length != NFS4_MAX_UINT64) { |
| 274 | dprintk("%s Only whole file layouts supported. Use MDS i/o\n", |
| 275 | __func__); |
| 276 | ret = -EINVAL; |
| 277 | } |
| 278 | |
| 279 | dprintk("--> %s returns %d\n", __func__, ret); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment *fls) |
| 284 | { |
| 285 | if (fls) { |
| 286 | ff_layout_free_mirror_array(fls); |
| 287 | kfree(fls); |
| 288 | } |
| 289 | } |
| 290 | |
Trond Myklebust | 0762ed2 | 2015-08-25 17:38:25 -0400 | [diff] [blame] | 291 | static bool |
| 292 | ff_lseg_range_is_after(const struct pnfs_layout_range *l1, |
| 293 | const struct pnfs_layout_range *l2) |
| 294 | { |
| 295 | u64 end1, end2; |
| 296 | |
| 297 | if (l1->iomode != l2->iomode) |
| 298 | return l1->iomode != IOMODE_READ; |
| 299 | end1 = pnfs_calc_offset_end(l1->offset, l1->length); |
| 300 | end2 = pnfs_calc_offset_end(l2->offset, l2->length); |
| 301 | if (end1 < l2->offset) |
| 302 | return false; |
| 303 | if (end2 < l1->offset) |
| 304 | return true; |
| 305 | return l2->offset <= l1->offset; |
| 306 | } |
| 307 | |
| 308 | static bool |
| 309 | ff_lseg_merge(struct pnfs_layout_segment *new, |
| 310 | struct pnfs_layout_segment *old) |
| 311 | { |
| 312 | u64 new_end, old_end; |
| 313 | |
Jeff Layton | ee26bdd | 2016-05-17 12:28:39 -0400 | [diff] [blame] | 314 | if (test_bit(NFS_LSEG_LAYOUTRETURN, &old->pls_flags)) |
| 315 | return false; |
Trond Myklebust | 0762ed2 | 2015-08-25 17:38:25 -0400 | [diff] [blame] | 316 | if (new->pls_range.iomode != old->pls_range.iomode) |
| 317 | return false; |
| 318 | old_end = pnfs_calc_offset_end(old->pls_range.offset, |
| 319 | old->pls_range.length); |
| 320 | if (old_end < new->pls_range.offset) |
| 321 | return false; |
| 322 | new_end = pnfs_calc_offset_end(new->pls_range.offset, |
| 323 | new->pls_range.length); |
| 324 | if (new_end < old->pls_range.offset) |
| 325 | return false; |
| 326 | |
| 327 | /* Mergeable: copy info from 'old' to 'new' */ |
| 328 | if (new_end < old_end) |
| 329 | new_end = old_end; |
| 330 | if (new->pls_range.offset < old->pls_range.offset) |
| 331 | new->pls_range.offset = old->pls_range.offset; |
| 332 | new->pls_range.length = pnfs_calc_offset_length(new->pls_range.offset, |
| 333 | new_end); |
| 334 | if (test_bit(NFS_LSEG_ROC, &old->pls_flags)) |
| 335 | set_bit(NFS_LSEG_ROC, &new->pls_flags); |
Trond Myklebust | 0762ed2 | 2015-08-25 17:38:25 -0400 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | ff_layout_add_lseg(struct pnfs_layout_hdr *lo, |
| 341 | struct pnfs_layout_segment *lseg, |
| 342 | struct list_head *free_me) |
| 343 | { |
| 344 | pnfs_generic_layout_insert_lseg(lo, lseg, |
| 345 | ff_lseg_range_is_after, |
| 346 | ff_lseg_merge, |
| 347 | free_me); |
| 348 | } |
| 349 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 350 | static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment *fls) |
| 351 | { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 352 | int i, j; |
| 353 | |
| 354 | for (i = 0; i < fls->mirror_array_cnt - 1; i++) { |
| 355 | for (j = i + 1; j < fls->mirror_array_cnt; j++) |
| 356 | if (fls->mirror_array[i]->efficiency < |
Fabian Frederick | 455b6ee | 2015-06-12 18:58:50 +0200 | [diff] [blame] | 357 | fls->mirror_array[j]->efficiency) |
| 358 | swap(fls->mirror_array[i], |
| 359 | fls->mirror_array[j]); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
| 363 | static struct pnfs_layout_segment * |
| 364 | ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh, |
| 365 | struct nfs4_layoutget_res *lgr, |
| 366 | gfp_t gfp_flags) |
| 367 | { |
| 368 | struct pnfs_layout_segment *ret; |
| 369 | struct nfs4_ff_layout_segment *fls = NULL; |
| 370 | struct xdr_stream stream; |
| 371 | struct xdr_buf buf; |
| 372 | struct page *scratch; |
| 373 | u64 stripe_unit; |
| 374 | u32 mirror_array_cnt; |
| 375 | __be32 *p; |
| 376 | int i, rc; |
| 377 | |
| 378 | dprintk("--> %s\n", __func__); |
| 379 | scratch = alloc_page(gfp_flags); |
| 380 | if (!scratch) |
| 381 | return ERR_PTR(-ENOMEM); |
| 382 | |
| 383 | xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages, |
| 384 | lgr->layoutp->len); |
| 385 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
| 386 | |
| 387 | /* stripe unit and mirror_array_cnt */ |
| 388 | rc = -EIO; |
| 389 | p = xdr_inline_decode(&stream, 8 + 4); |
| 390 | if (!p) |
| 391 | goto out_err_free; |
| 392 | |
| 393 | p = xdr_decode_hyper(p, &stripe_unit); |
| 394 | mirror_array_cnt = be32_to_cpup(p++); |
| 395 | dprintk("%s: stripe_unit=%llu mirror_array_cnt=%u\n", __func__, |
| 396 | stripe_unit, mirror_array_cnt); |
| 397 | |
| 398 | if (mirror_array_cnt > NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT || |
| 399 | mirror_array_cnt == 0) |
| 400 | goto out_err_free; |
| 401 | |
| 402 | rc = -ENOMEM; |
| 403 | fls = kzalloc(sizeof(*fls), gfp_flags); |
| 404 | if (!fls) |
| 405 | goto out_err_free; |
| 406 | |
| 407 | fls->mirror_array_cnt = mirror_array_cnt; |
| 408 | fls->stripe_unit = stripe_unit; |
| 409 | fls->mirror_array = kcalloc(fls->mirror_array_cnt, |
| 410 | sizeof(fls->mirror_array[0]), gfp_flags); |
| 411 | if (fls->mirror_array == NULL) |
| 412 | goto out_err_free; |
| 413 | |
| 414 | for (i = 0; i < fls->mirror_array_cnt; i++) { |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 415 | struct nfs4_ff_layout_mirror *mirror; |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 416 | struct auth_cred acred = { .group_info = ff_zero_group }; |
Jeff Layton | 3064b68 | 2016-04-21 20:52:00 -0400 | [diff] [blame] | 417 | struct rpc_cred __rcu *cred; |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 418 | u32 ds_count, fh_count, id; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 419 | int j; |
| 420 | |
| 421 | rc = -EIO; |
| 422 | p = xdr_inline_decode(&stream, 4); |
| 423 | if (!p) |
| 424 | goto out_err_free; |
| 425 | ds_count = be32_to_cpup(p); |
| 426 | |
| 427 | /* FIXME: allow for striping? */ |
| 428 | if (ds_count != 1) |
| 429 | goto out_err_free; |
| 430 | |
Trond Myklebust | 28a0d72 | 2015-08-24 18:08:30 -0400 | [diff] [blame] | 431 | fls->mirror_array[i] = ff_layout_alloc_mirror(gfp_flags); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 432 | if (fls->mirror_array[i] == NULL) { |
| 433 | rc = -ENOMEM; |
| 434 | goto out_err_free; |
| 435 | } |
| 436 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 437 | fls->mirror_array[i]->ds_count = ds_count; |
| 438 | |
| 439 | /* deviceid */ |
Fred Isaman | 65990d1 | 2016-09-30 14:37:41 -0400 | [diff] [blame] | 440 | rc = decode_deviceid(&stream, &fls->mirror_array[i]->devid); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 441 | if (rc) |
| 442 | goto out_err_free; |
| 443 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 444 | /* efficiency */ |
| 445 | rc = -EIO; |
| 446 | p = xdr_inline_decode(&stream, 4); |
| 447 | if (!p) |
| 448 | goto out_err_free; |
| 449 | fls->mirror_array[i]->efficiency = be32_to_cpup(p); |
| 450 | |
| 451 | /* stateid */ |
Trond Myklebust | 93b717f | 2016-05-16 17:42:43 -0400 | [diff] [blame] | 452 | rc = decode_pnfs_stateid(&stream, &fls->mirror_array[i]->stateid); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 453 | if (rc) |
| 454 | goto out_err_free; |
| 455 | |
| 456 | /* fh */ |
| 457 | p = xdr_inline_decode(&stream, 4); |
| 458 | if (!p) |
| 459 | goto out_err_free; |
| 460 | fh_count = be32_to_cpup(p); |
| 461 | |
| 462 | fls->mirror_array[i]->fh_versions = |
| 463 | kzalloc(fh_count * sizeof(struct nfs_fh), |
| 464 | gfp_flags); |
| 465 | if (fls->mirror_array[i]->fh_versions == NULL) { |
| 466 | rc = -ENOMEM; |
| 467 | goto out_err_free; |
| 468 | } |
| 469 | |
| 470 | for (j = 0; j < fh_count; j++) { |
| 471 | rc = decode_nfs_fh(&stream, |
| 472 | &fls->mirror_array[i]->fh_versions[j]); |
| 473 | if (rc) |
| 474 | goto out_err_free; |
| 475 | } |
| 476 | |
| 477 | fls->mirror_array[i]->fh_versions_cnt = fh_count; |
| 478 | |
| 479 | /* user */ |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 480 | rc = decode_name(&stream, &id); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 481 | if (rc) |
| 482 | goto out_err_free; |
| 483 | |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 484 | acred.uid = make_kuid(&init_user_ns, id); |
| 485 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 486 | /* group */ |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 487 | rc = decode_name(&stream, &id); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 488 | if (rc) |
| 489 | goto out_err_free; |
| 490 | |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 491 | acred.gid = make_kgid(&init_user_ns, id); |
| 492 | |
| 493 | /* find the cred for it */ |
Jeff Layton | 3064b68 | 2016-04-21 20:52:00 -0400 | [diff] [blame] | 494 | rcu_assign_pointer(cred, rpc_lookup_generic_cred(&acred, 0, gfp_flags)); |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 495 | if (IS_ERR(cred)) { |
| 496 | rc = PTR_ERR(cred); |
| 497 | goto out_err_free; |
| 498 | } |
| 499 | |
Jeff Layton | 3064b68 | 2016-04-21 20:52:00 -0400 | [diff] [blame] | 500 | if (lgr->range.iomode == IOMODE_READ) |
| 501 | rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred); |
| 502 | else |
| 503 | rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred); |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 504 | |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 505 | mirror = ff_layout_add_mirror(lh, fls->mirror_array[i]); |
| 506 | if (mirror != fls->mirror_array[i]) { |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 507 | /* swap cred ptrs so free_mirror will clean up old */ |
Jeff Layton | 3064b68 | 2016-04-21 20:52:00 -0400 | [diff] [blame] | 508 | if (lgr->range.iomode == IOMODE_READ) { |
| 509 | cred = xchg(&mirror->ro_cred, cred); |
| 510 | rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred); |
| 511 | } else { |
| 512 | cred = xchg(&mirror->rw_cred, cred); |
| 513 | rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred); |
| 514 | } |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 515 | ff_layout_free_mirror(fls->mirror_array[i]); |
| 516 | fls->mirror_array[i] = mirror; |
| 517 | } |
| 518 | |
Jeff Layton | 3064b68 | 2016-04-21 20:52:00 -0400 | [diff] [blame] | 519 | dprintk("%s: iomode %s uid %u gid %u\n", __func__, |
| 520 | lgr->range.iomode == IOMODE_READ ? "READ" : "RW", |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 521 | from_kuid(&init_user_ns, acred.uid), |
| 522 | from_kgid(&init_user_ns, acred.gid)); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 523 | } |
| 524 | |
Trond Myklebust | c0f5f50 | 2015-06-26 14:51:32 -0400 | [diff] [blame] | 525 | p = xdr_inline_decode(&stream, 4); |
Trond Myklebust | d0379a5 | 2015-11-16 11:26:07 -0500 | [diff] [blame] | 526 | if (!p) |
| 527 | goto out_sort_mirrors; |
| 528 | fls->flags = be32_to_cpup(p); |
Trond Myklebust | c0f5f50 | 2015-06-26 14:51:32 -0400 | [diff] [blame] | 529 | |
Trond Myklebust | d0379a5 | 2015-11-16 11:26:07 -0500 | [diff] [blame] | 530 | p = xdr_inline_decode(&stream, 4); |
| 531 | if (!p) |
| 532 | goto out_sort_mirrors; |
| 533 | for (i=0; i < fls->mirror_array_cnt; i++) |
| 534 | fls->mirror_array[i]->report_interval = be32_to_cpup(p); |
| 535 | |
| 536 | out_sort_mirrors: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 537 | ff_layout_sort_mirrors(fls); |
| 538 | rc = ff_layout_check_layout(lgr); |
| 539 | if (rc) |
| 540 | goto out_err_free; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 541 | ret = &fls->generic_hdr; |
| 542 | dprintk("<-- %s (success)\n", __func__); |
| 543 | out_free_page: |
| 544 | __free_page(scratch); |
| 545 | return ret; |
| 546 | out_err_free: |
| 547 | _ff_layout_free_lseg(fls); |
| 548 | ret = ERR_PTR(rc); |
| 549 | dprintk("<-- %s (%d)\n", __func__, rc); |
| 550 | goto out_free_page; |
| 551 | } |
| 552 | |
| 553 | static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout) |
| 554 | { |
| 555 | struct pnfs_layout_segment *lseg; |
| 556 | |
| 557 | list_for_each_entry(lseg, &layout->plh_segs, pls_list) |
| 558 | if (lseg->pls_range.iomode == IOMODE_RW) |
| 559 | return true; |
| 560 | |
| 561 | return false; |
| 562 | } |
| 563 | |
| 564 | static void |
| 565 | ff_layout_free_lseg(struct pnfs_layout_segment *lseg) |
| 566 | { |
| 567 | struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 568 | |
| 569 | dprintk("--> %s\n", __func__); |
| 570 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 571 | if (lseg->pls_range.iomode == IOMODE_RW) { |
| 572 | struct nfs4_flexfile_layout *ffl; |
| 573 | struct inode *inode; |
| 574 | |
| 575 | ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout); |
| 576 | inode = ffl->generic_hdr.plh_inode; |
| 577 | spin_lock(&inode->i_lock); |
| 578 | if (!ff_layout_has_rw_segments(lseg->pls_layout)) { |
| 579 | ffl->commit_info.nbuckets = 0; |
| 580 | kfree(ffl->commit_info.buckets); |
| 581 | ffl->commit_info.buckets = NULL; |
| 582 | } |
| 583 | spin_unlock(&inode->i_lock); |
| 584 | } |
| 585 | _ff_layout_free_lseg(fls); |
| 586 | } |
| 587 | |
| 588 | /* Return 1 until we have multiple lsegs support */ |
| 589 | static int |
| 590 | ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls) |
| 591 | { |
| 592 | return 1; |
| 593 | } |
| 594 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 595 | static void |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 596 | 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] | 597 | { |
Peng Tao | 9bbd9bb | 2015-06-26 09:45:49 +0800 | [diff] [blame] | 598 | /* first IO request? */ |
| 599 | if (atomic_inc_return(&timer->n_ops) == 1) { |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 600 | timer->start_time = now; |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | |
| 604 | static ktime_t |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 605 | 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] | 606 | { |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 607 | ktime_t start; |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 608 | |
Peng Tao | 9bbd9bb | 2015-06-26 09:45:49 +0800 | [diff] [blame] | 609 | if (atomic_dec_return(&timer->n_ops) < 0) |
| 610 | WARN_ON_ONCE(1); |
| 611 | |
Peng Tao | 9bbd9bb | 2015-06-26 09:45:49 +0800 | [diff] [blame] | 612 | start = timer->start_time; |
| 613 | timer->start_time = now; |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 614 | return ktime_sub(now, start); |
| 615 | } |
| 616 | |
Peng Tao | 97ba375 | 2015-06-23 19:52:04 +0800 | [diff] [blame] | 617 | static bool |
Peng Tao | d983803 | 2015-06-23 19:52:00 +0800 | [diff] [blame] | 618 | nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror, |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 619 | struct nfs4_ff_layoutstat *layoutstat, |
| 620 | ktime_t now) |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 621 | { |
Trond Myklebust | bbf58bf | 2015-08-24 20:39:18 -0400 | [diff] [blame] | 622 | s64 report_interval = FF_LAYOUTSTATS_REPORT_INTERVAL; |
Trond Myklebust | 1c8d477 | 2016-08-14 12:47:49 -0400 | [diff] [blame] | 623 | struct nfs4_flexfile_layout *ffl = FF_LAYOUT_FROM_HDR(mirror->layout); |
Peng Tao | d983803 | 2015-06-23 19:52:00 +0800 | [diff] [blame] | 624 | |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 625 | nfs4_ff_start_busy_timer(&layoutstat->busy_timer, now); |
Thomas Gleixner | 1f3a8e4 | 2016-12-25 12:43:07 +0100 | [diff] [blame] | 626 | if (!mirror->start_time) |
Peng Tao | 9bbd9bb | 2015-06-26 09:45:49 +0800 | [diff] [blame] | 627 | mirror->start_time = now; |
Trond Myklebust | d0379a5 | 2015-11-16 11:26:07 -0500 | [diff] [blame] | 628 | if (mirror->report_interval != 0) |
| 629 | report_interval = (s64)mirror->report_interval * 1000LL; |
| 630 | else if (layoutstats_timer != 0) |
Trond Myklebust | bbf58bf | 2015-08-24 20:39:18 -0400 | [diff] [blame] | 631 | report_interval = (s64)layoutstats_timer * 1000LL; |
Trond Myklebust | 1c8d477 | 2016-08-14 12:47:49 -0400 | [diff] [blame] | 632 | if (ktime_to_ms(ktime_sub(now, ffl->last_report_time)) >= |
Trond Myklebust | bbf58bf | 2015-08-24 20:39:18 -0400 | [diff] [blame] | 633 | report_interval) { |
Trond Myklebust | 1c8d477 | 2016-08-14 12:47:49 -0400 | [diff] [blame] | 634 | ffl->last_report_time = now; |
Peng Tao | 97ba375 | 2015-06-23 19:52:04 +0800 | [diff] [blame] | 635 | return true; |
| 636 | } |
| 637 | |
| 638 | return false; |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | static void |
| 642 | nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat, |
| 643 | __u64 requested) |
| 644 | { |
| 645 | struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat; |
| 646 | |
| 647 | iostat->ops_requested++; |
| 648 | iostat->bytes_requested += requested; |
| 649 | } |
| 650 | |
| 651 | static void |
| 652 | nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat, |
| 653 | __u64 requested, |
| 654 | __u64 completed, |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 655 | ktime_t time_completed, |
| 656 | ktime_t time_started) |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 657 | { |
| 658 | struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat; |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 659 | ktime_t completion_time = ktime_sub(time_completed, time_started); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 660 | ktime_t timer; |
| 661 | |
| 662 | iostat->ops_completed++; |
| 663 | iostat->bytes_completed += completed; |
| 664 | iostat->bytes_not_delivered += requested - completed; |
| 665 | |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 666 | timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer, time_completed); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 667 | iostat->total_busy_time = |
| 668 | ktime_add(iostat->total_busy_time, timer); |
| 669 | iostat->aggregate_completion_time = |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 670 | ktime_add(iostat->aggregate_completion_time, |
| 671 | completion_time); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static void |
Trond Myklebust | 0b7baf9 | 2015-08-24 18:22:28 -0400 | [diff] [blame] | 675 | nfs4_ff_layout_stat_io_start_read(struct inode *inode, |
| 676 | struct nfs4_ff_layout_mirror *mirror, |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 677 | __u64 requested, ktime_t now) |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 678 | { |
Peng Tao | 97ba375 | 2015-06-23 19:52:04 +0800 | [diff] [blame] | 679 | bool report; |
| 680 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 681 | spin_lock(&mirror->lock); |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 682 | report = nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat, now); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 683 | nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested); |
Trond Myklebust | 06946c6 | 2016-11-25 13:17:15 -0500 | [diff] [blame] | 684 | set_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 685 | spin_unlock(&mirror->lock); |
Peng Tao | 97ba375 | 2015-06-23 19:52:04 +0800 | [diff] [blame] | 686 | |
| 687 | if (report) |
Trond Myklebust | 0b7baf9 | 2015-08-24 18:22:28 -0400 | [diff] [blame] | 688 | pnfs_report_layoutstat(inode, GFP_KERNEL); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | static void |
| 692 | nfs4_ff_layout_stat_io_end_read(struct rpc_task *task, |
| 693 | struct nfs4_ff_layout_mirror *mirror, |
| 694 | __u64 requested, |
| 695 | __u64 completed) |
| 696 | { |
| 697 | spin_lock(&mirror->lock); |
| 698 | nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat, |
| 699 | requested, completed, |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 700 | ktime_get(), task->tk_start); |
Trond Myklebust | 06946c6 | 2016-11-25 13:17:15 -0500 | [diff] [blame] | 701 | set_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 702 | spin_unlock(&mirror->lock); |
| 703 | } |
| 704 | |
| 705 | static void |
Trond Myklebust | 0b7baf9 | 2015-08-24 18:22:28 -0400 | [diff] [blame] | 706 | nfs4_ff_layout_stat_io_start_write(struct inode *inode, |
| 707 | struct nfs4_ff_layout_mirror *mirror, |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 708 | __u64 requested, ktime_t now) |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 709 | { |
Peng Tao | 97ba375 | 2015-06-23 19:52:04 +0800 | [diff] [blame] | 710 | bool report; |
| 711 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 712 | spin_lock(&mirror->lock); |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 713 | report = nfs4_ff_layoutstat_start_io(mirror , &mirror->write_stat, now); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 714 | nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested); |
Trond Myklebust | 06946c6 | 2016-11-25 13:17:15 -0500 | [diff] [blame] | 715 | set_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 716 | spin_unlock(&mirror->lock); |
Peng Tao | 97ba375 | 2015-06-23 19:52:04 +0800 | [diff] [blame] | 717 | |
| 718 | if (report) |
Trond Myklebust | 0b7baf9 | 2015-08-24 18:22:28 -0400 | [diff] [blame] | 719 | pnfs_report_layoutstat(inode, GFP_NOIO); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | static void |
| 723 | nfs4_ff_layout_stat_io_end_write(struct rpc_task *task, |
| 724 | struct nfs4_ff_layout_mirror *mirror, |
| 725 | __u64 requested, |
| 726 | __u64 completed, |
| 727 | enum nfs3_stable_how committed) |
| 728 | { |
| 729 | if (committed == NFS_UNSTABLE) |
| 730 | requested = completed = 0; |
| 731 | |
| 732 | spin_lock(&mirror->lock); |
| 733 | nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat, |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 734 | requested, completed, ktime_get(), task->tk_start); |
Trond Myklebust | 06946c6 | 2016-11-25 13:17:15 -0500 | [diff] [blame] | 735 | set_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 736 | spin_unlock(&mirror->lock); |
| 737 | } |
| 738 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 739 | static int |
| 740 | ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg, |
| 741 | struct nfs_commit_info *cinfo, |
| 742 | gfp_t gfp_flags) |
| 743 | { |
| 744 | struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg); |
| 745 | struct pnfs_commit_bucket *buckets; |
| 746 | int size; |
| 747 | |
| 748 | if (cinfo->ds->nbuckets != 0) { |
| 749 | /* This assumes there is only one RW lseg per file. |
| 750 | * To support multiple lseg per file, we need to |
| 751 | * change struct pnfs_commit_bucket to allow dynamic |
| 752 | * increasing nbuckets. |
| 753 | */ |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg); |
| 758 | |
| 759 | buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket), |
| 760 | gfp_flags); |
| 761 | if (!buckets) |
| 762 | return -ENOMEM; |
| 763 | else { |
| 764 | int i; |
| 765 | |
Dave Wysochanski | fe238e6 | 2016-04-01 13:45:09 -0400 | [diff] [blame] | 766 | spin_lock(&cinfo->inode->i_lock); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 767 | if (cinfo->ds->nbuckets != 0) |
| 768 | kfree(buckets); |
| 769 | else { |
| 770 | cinfo->ds->buckets = buckets; |
| 771 | cinfo->ds->nbuckets = size; |
| 772 | for (i = 0; i < size; i++) { |
| 773 | INIT_LIST_HEAD(&buckets[i].written); |
| 774 | INIT_LIST_HEAD(&buckets[i].committing); |
| 775 | /* mark direct verifier as unset */ |
| 776 | buckets[i].direct_verf.committed = |
| 777 | NFS_INVALID_STABLE_HOW; |
| 778 | } |
| 779 | } |
Dave Wysochanski | fe238e6 | 2016-04-01 13:45:09 -0400 | [diff] [blame] | 780 | spin_unlock(&cinfo->inode->i_lock); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | static struct nfs4_pnfs_ds * |
Trond Myklebust | 1354441 | 2015-11-02 10:11:17 -0500 | [diff] [blame] | 786 | ff_layout_choose_best_ds_for_read(struct pnfs_layout_segment *lseg, |
| 787 | int start_idx, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 788 | int *best_idx) |
| 789 | { |
Trond Myklebust | 1354441 | 2015-11-02 10:11:17 -0500 | [diff] [blame] | 790 | struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 791 | struct nfs4_pnfs_ds *ds; |
Trond Myklebust | 3dc1473 | 2016-08-29 15:12:54 -0400 | [diff] [blame] | 792 | bool fail_return = false; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 793 | int idx; |
| 794 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 795 | /* mirrors are sorted by efficiency */ |
Trond Myklebust | 1354441 | 2015-11-02 10:11:17 -0500 | [diff] [blame] | 796 | for (idx = start_idx; idx < fls->mirror_array_cnt; idx++) { |
Trond Myklebust | 3dc1473 | 2016-08-29 15:12:54 -0400 | [diff] [blame] | 797 | if (idx+1 == fls->mirror_array_cnt) |
| 798 | fail_return = true; |
| 799 | ds = nfs4_ff_layout_prepare_ds(lseg, idx, fail_return); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 800 | if (ds) { |
| 801 | *best_idx = idx; |
| 802 | return ds; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | return NULL; |
| 807 | } |
| 808 | |
| 809 | static void |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 810 | ff_layout_pg_get_read(struct nfs_pageio_descriptor *pgio, |
| 811 | struct nfs_page *req, |
| 812 | bool strict_iomode) |
| 813 | { |
| 814 | retry_strict: |
| 815 | pnfs_put_lseg(pgio->pg_lseg); |
| 816 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 817 | req->wb_context, |
| 818 | 0, |
| 819 | NFS4_MAX_UINT64, |
| 820 | IOMODE_READ, |
| 821 | strict_iomode, |
| 822 | GFP_KERNEL); |
| 823 | if (IS_ERR(pgio->pg_lseg)) { |
| 824 | pgio->pg_error = PTR_ERR(pgio->pg_lseg); |
| 825 | pgio->pg_lseg = NULL; |
| 826 | } |
| 827 | |
| 828 | /* If we don't have checking, do get a IOMODE_RW |
| 829 | * segment, and the server wants to avoid READs |
| 830 | * there, then retry! |
| 831 | */ |
| 832 | if (pgio->pg_lseg && !strict_iomode && |
| 833 | ff_layout_avoid_read_on_rw(pgio->pg_lseg)) { |
| 834 | strict_iomode = true; |
| 835 | goto retry_strict; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | static void |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 840 | ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio, |
| 841 | struct nfs_page *req) |
| 842 | { |
| 843 | struct nfs_pgio_mirror *pgm; |
| 844 | struct nfs4_ff_layout_mirror *mirror; |
| 845 | struct nfs4_pnfs_ds *ds; |
| 846 | int ds_idx; |
| 847 | |
Trond Myklebust | 3dc1473 | 2016-08-29 15:12:54 -0400 | [diff] [blame] | 848 | retry: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 849 | /* Use full layout for now */ |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 850 | if (!pgio->pg_lseg) |
| 851 | ff_layout_pg_get_read(pgio, req, false); |
| 852 | else if (ff_layout_avoid_read_on_rw(pgio->pg_lseg)) |
| 853 | ff_layout_pg_get_read(pgio, req, true); |
Tom Haynes | 602c4cd | 2016-05-25 07:31:13 -0700 | [diff] [blame] | 854 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 855 | /* If no lseg, fall back to read through mds */ |
| 856 | if (pgio->pg_lseg == NULL) |
| 857 | goto out_mds; |
| 858 | |
Trond Myklebust | 1354441 | 2015-11-02 10:11:17 -0500 | [diff] [blame] | 859 | ds = ff_layout_choose_best_ds_for_read(pgio->pg_lseg, 0, &ds_idx); |
Tom Haynes | 446ca21 | 2016-05-17 12:28:38 -0400 | [diff] [blame] | 860 | if (!ds) { |
Trond Myklebust | 3dc1473 | 2016-08-29 15:12:54 -0400 | [diff] [blame] | 861 | if (!ff_layout_no_fallback_to_mds(pgio->pg_lseg)) |
Tom Haynes | 446ca21 | 2016-05-17 12:28:38 -0400 | [diff] [blame] | 862 | goto out_mds; |
Trond Myklebust | 3dc1473 | 2016-08-29 15:12:54 -0400 | [diff] [blame] | 863 | pnfs_put_lseg(pgio->pg_lseg); |
| 864 | pgio->pg_lseg = NULL; |
| 865 | /* Sleep for 1 second before retrying */ |
| 866 | ssleep(1); |
| 867 | goto retry; |
Tom Haynes | 446ca21 | 2016-05-17 12:28:38 -0400 | [diff] [blame] | 868 | } |
| 869 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 870 | mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx); |
| 871 | |
| 872 | pgio->pg_mirror_idx = ds_idx; |
| 873 | |
| 874 | /* read always uses only one mirror - idx 0 for pgio layer */ |
| 875 | pgm = &pgio->pg_mirrors[0]; |
| 876 | pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize; |
| 877 | |
| 878 | return; |
| 879 | out_mds: |
| 880 | pnfs_put_lseg(pgio->pg_lseg); |
| 881 | pgio->pg_lseg = NULL; |
| 882 | nfs_pageio_reset_read_mds(pgio); |
| 883 | } |
| 884 | |
| 885 | static void |
| 886 | ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio, |
| 887 | struct nfs_page *req) |
| 888 | { |
| 889 | struct nfs4_ff_layout_mirror *mirror; |
| 890 | struct nfs_pgio_mirror *pgm; |
| 891 | struct nfs_commit_info cinfo; |
| 892 | struct nfs4_pnfs_ds *ds; |
| 893 | int i; |
| 894 | int status; |
| 895 | |
Trond Myklebust | 3dc1473 | 2016-08-29 15:12:54 -0400 | [diff] [blame] | 896 | retry: |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 897 | if (!pgio->pg_lseg) { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 898 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 899 | req->wb_context, |
| 900 | 0, |
| 901 | NFS4_MAX_UINT64, |
| 902 | IOMODE_RW, |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 903 | false, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 904 | GFP_NOFS); |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 905 | if (IS_ERR(pgio->pg_lseg)) { |
| 906 | pgio->pg_error = PTR_ERR(pgio->pg_lseg); |
| 907 | pgio->pg_lseg = NULL; |
| 908 | return; |
| 909 | } |
| 910 | } |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 911 | /* If no lseg, fall back to write through mds */ |
| 912 | if (pgio->pg_lseg == NULL) |
| 913 | goto out_mds; |
| 914 | |
| 915 | nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq); |
| 916 | status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS); |
| 917 | if (status < 0) |
| 918 | goto out_mds; |
| 919 | |
| 920 | /* Use a direct mapping of ds_idx to pgio mirror_idx */ |
| 921 | if (WARN_ON_ONCE(pgio->pg_mirror_count != |
| 922 | FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg))) |
| 923 | goto out_mds; |
| 924 | |
| 925 | for (i = 0; i < pgio->pg_mirror_count; i++) { |
| 926 | ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true); |
Tom Haynes | 446ca21 | 2016-05-17 12:28:38 -0400 | [diff] [blame] | 927 | if (!ds) { |
Trond Myklebust | 3dc1473 | 2016-08-29 15:12:54 -0400 | [diff] [blame] | 928 | if (!ff_layout_no_fallback_to_mds(pgio->pg_lseg)) |
Tom Haynes | 446ca21 | 2016-05-17 12:28:38 -0400 | [diff] [blame] | 929 | goto out_mds; |
Trond Myklebust | 3dc1473 | 2016-08-29 15:12:54 -0400 | [diff] [blame] | 930 | pnfs_put_lseg(pgio->pg_lseg); |
| 931 | pgio->pg_lseg = NULL; |
| 932 | /* Sleep for 1 second before retrying */ |
| 933 | ssleep(1); |
| 934 | goto retry; |
Tom Haynes | 446ca21 | 2016-05-17 12:28:38 -0400 | [diff] [blame] | 935 | } |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 936 | pgm = &pgio->pg_mirrors[i]; |
| 937 | mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i); |
| 938 | pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize; |
| 939 | } |
| 940 | |
| 941 | return; |
| 942 | |
| 943 | out_mds: |
| 944 | pnfs_put_lseg(pgio->pg_lseg); |
| 945 | pgio->pg_lseg = NULL; |
| 946 | nfs_pageio_reset_write_mds(pgio); |
| 947 | } |
| 948 | |
| 949 | static unsigned int |
| 950 | ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio, |
| 951 | struct nfs_page *req) |
| 952 | { |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 953 | if (!pgio->pg_lseg) { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 954 | pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode, |
| 955 | req->wb_context, |
| 956 | 0, |
| 957 | NFS4_MAX_UINT64, |
| 958 | IOMODE_RW, |
Tom Haynes | c7d73af | 2016-05-25 07:31:14 -0700 | [diff] [blame] | 959 | false, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 960 | GFP_NOFS); |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 961 | if (IS_ERR(pgio->pg_lseg)) { |
| 962 | pgio->pg_error = PTR_ERR(pgio->pg_lseg); |
| 963 | pgio->pg_lseg = NULL; |
| 964 | goto out; |
| 965 | } |
| 966 | } |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 967 | if (pgio->pg_lseg) |
| 968 | return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg); |
| 969 | |
| 970 | /* 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] | 971 | nfs_pageio_reset_write_mds(pgio); |
Peng Tao | d600ad1 | 2015-12-04 02:57:48 +0800 | [diff] [blame] | 972 | out: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 973 | return 1; |
| 974 | } |
| 975 | |
| 976 | static const struct nfs_pageio_ops ff_layout_pg_read_ops = { |
| 977 | .pg_init = ff_layout_pg_init_read, |
| 978 | .pg_test = pnfs_generic_pg_test, |
| 979 | .pg_doio = pnfs_generic_pg_readpages, |
| 980 | .pg_cleanup = pnfs_generic_pg_cleanup, |
| 981 | }; |
| 982 | |
| 983 | static const struct nfs_pageio_ops ff_layout_pg_write_ops = { |
| 984 | .pg_init = ff_layout_pg_init_write, |
| 985 | .pg_test = pnfs_generic_pg_test, |
| 986 | .pg_doio = pnfs_generic_pg_writepages, |
| 987 | .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write, |
| 988 | .pg_cleanup = pnfs_generic_pg_cleanup, |
| 989 | }; |
| 990 | |
| 991 | static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs) |
| 992 | { |
| 993 | struct rpc_task *task = &hdr->task; |
| 994 | |
| 995 | pnfs_layoutcommit_inode(hdr->inode, false); |
| 996 | |
| 997 | if (retry_pnfs) { |
| 998 | dprintk("%s Reset task %5u for i/o through pNFS " |
| 999 | "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, |
| 1000 | hdr->task.tk_pid, |
| 1001 | hdr->inode->i_sb->s_id, |
| 1002 | (unsigned long long)NFS_FILEID(hdr->inode), |
| 1003 | hdr->args.count, |
| 1004 | (unsigned long long)hdr->args.offset); |
| 1005 | |
Trond Myklebust | dc602dd | 2015-12-31 11:44:06 -0500 | [diff] [blame] | 1006 | hdr->completion_ops->reschedule_io(hdr); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1007 | return; |
| 1008 | } |
| 1009 | |
| 1010 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
| 1011 | dprintk("%s Reset task %5u for i/o through MDS " |
| 1012 | "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, |
| 1013 | hdr->task.tk_pid, |
| 1014 | hdr->inode->i_sb->s_id, |
| 1015 | (unsigned long long)NFS_FILEID(hdr->inode), |
| 1016 | hdr->args.count, |
| 1017 | (unsigned long long)hdr->args.offset); |
| 1018 | |
| 1019 | task->tk_status = pnfs_write_done_resend_to_mds(hdr); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | static void ff_layout_reset_read(struct nfs_pgio_header *hdr) |
| 1024 | { |
| 1025 | struct rpc_task *task = &hdr->task; |
| 1026 | |
| 1027 | pnfs_layoutcommit_inode(hdr->inode, false); |
| 1028 | |
| 1029 | if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) { |
| 1030 | dprintk("%s Reset task %5u for i/o through MDS " |
| 1031 | "(req %s/%llu, %u bytes @ offset %llu)\n", __func__, |
| 1032 | hdr->task.tk_pid, |
| 1033 | hdr->inode->i_sb->s_id, |
| 1034 | (unsigned long long)NFS_FILEID(hdr->inode), |
| 1035 | hdr->args.count, |
| 1036 | (unsigned long long)hdr->args.offset); |
| 1037 | |
| 1038 | task->tk_status = pnfs_read_done_resend_to_mds(hdr); |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | static int ff_layout_async_handle_error_v4(struct rpc_task *task, |
| 1043 | struct nfs4_state *state, |
| 1044 | struct nfs_client *clp, |
| 1045 | struct pnfs_layout_segment *lseg, |
| 1046 | int idx) |
| 1047 | { |
| 1048 | struct pnfs_layout_hdr *lo = lseg->pls_layout; |
| 1049 | struct inode *inode = lo->plh_inode; |
| 1050 | struct nfs_server *mds_server = NFS_SERVER(inode); |
| 1051 | |
| 1052 | struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx); |
| 1053 | struct nfs_client *mds_client = mds_server->nfs_client; |
| 1054 | struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table; |
| 1055 | |
| 1056 | if (task->tk_status >= 0) |
| 1057 | return 0; |
| 1058 | |
| 1059 | switch (task->tk_status) { |
| 1060 | /* MDS state errors */ |
| 1061 | case -NFS4ERR_DELEG_REVOKED: |
| 1062 | case -NFS4ERR_ADMIN_REVOKED: |
| 1063 | case -NFS4ERR_BAD_STATEID: |
| 1064 | if (state == NULL) |
| 1065 | break; |
Trond Myklebust | 41020b6 | 2016-09-22 13:38:58 -0400 | [diff] [blame] | 1066 | nfs_remove_bad_delegation(state->inode, NULL); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1067 | case -NFS4ERR_OPENMODE: |
| 1068 | if (state == NULL) |
| 1069 | break; |
| 1070 | if (nfs4_schedule_stateid_recovery(mds_server, state) < 0) |
| 1071 | goto out_bad_stateid; |
| 1072 | goto wait_on_recovery; |
| 1073 | case -NFS4ERR_EXPIRED: |
| 1074 | if (state != NULL) { |
| 1075 | if (nfs4_schedule_stateid_recovery(mds_server, state) < 0) |
| 1076 | goto out_bad_stateid; |
| 1077 | } |
| 1078 | nfs4_schedule_lease_recovery(mds_client); |
| 1079 | goto wait_on_recovery; |
| 1080 | /* DS session errors */ |
| 1081 | case -NFS4ERR_BADSESSION: |
| 1082 | case -NFS4ERR_BADSLOT: |
| 1083 | case -NFS4ERR_BAD_HIGH_SLOT: |
| 1084 | case -NFS4ERR_DEADSESSION: |
| 1085 | case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION: |
| 1086 | case -NFS4ERR_SEQ_FALSE_RETRY: |
| 1087 | case -NFS4ERR_SEQ_MISORDERED: |
| 1088 | dprintk("%s ERROR %d, Reset session. Exchangeid " |
| 1089 | "flags 0x%x\n", __func__, task->tk_status, |
| 1090 | clp->cl_exchange_flags); |
| 1091 | nfs4_schedule_session_recovery(clp->cl_session, task->tk_status); |
| 1092 | break; |
| 1093 | case -NFS4ERR_DELAY: |
| 1094 | case -NFS4ERR_GRACE: |
| 1095 | rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX); |
| 1096 | break; |
| 1097 | case -NFS4ERR_RETRY_UNCACHED_REP: |
| 1098 | break; |
| 1099 | /* Invalidate Layout errors */ |
| 1100 | case -NFS4ERR_PNFS_NO_LAYOUT: |
| 1101 | case -ESTALE: /* mapped NFS4ERR_STALE */ |
| 1102 | case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */ |
| 1103 | case -EISDIR: /* mapped NFS4ERR_ISDIR */ |
| 1104 | case -NFS4ERR_FHEXPIRED: |
| 1105 | case -NFS4ERR_WRONG_TYPE: |
| 1106 | dprintk("%s Invalid layout error %d\n", __func__, |
| 1107 | task->tk_status); |
| 1108 | /* |
| 1109 | * Destroy layout so new i/o will get a new layout. |
| 1110 | * Layout will not be destroyed until all current lseg |
| 1111 | * references are put. Mark layout as invalid to resend failed |
| 1112 | * i/o and all i/o waiting on the slot table to the MDS until |
| 1113 | * layout is destroyed and a new valid layout is obtained. |
| 1114 | */ |
| 1115 | pnfs_destroy_layout(NFS_I(inode)); |
| 1116 | rpc_wake_up(&tbl->slot_tbl_waitq); |
| 1117 | goto reset; |
| 1118 | /* RPC connection errors */ |
| 1119 | case -ECONNREFUSED: |
| 1120 | case -EHOSTDOWN: |
| 1121 | case -EHOSTUNREACH: |
| 1122 | case -ENETUNREACH: |
| 1123 | case -EIO: |
| 1124 | case -ETIMEDOUT: |
| 1125 | case -EPIPE: |
| 1126 | dprintk("%s DS connection error %d\n", __func__, |
| 1127 | task->tk_status); |
Weston Andros Adamson | 1c48cee | 2016-12-14 16:31:55 -0500 | [diff] [blame] | 1128 | nfs4_delete_deviceid(devid->ld, devid->nfs_client, |
| 1129 | &devid->deviceid); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1130 | rpc_wake_up(&tbl->slot_tbl_waitq); |
| 1131 | /* fall through */ |
| 1132 | default: |
Tom Haynes | 3b13b4b | 2016-05-17 12:28:37 -0400 | [diff] [blame] | 1133 | if (ff_layout_avoid_mds_available_ds(lseg)) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1134 | return -NFS4ERR_RESET_TO_PNFS; |
| 1135 | reset: |
| 1136 | dprintk("%s Retry through MDS. Error %d\n", __func__, |
| 1137 | task->tk_status); |
| 1138 | return -NFS4ERR_RESET_TO_MDS; |
| 1139 | } |
| 1140 | out: |
| 1141 | task->tk_status = 0; |
| 1142 | return -EAGAIN; |
| 1143 | out_bad_stateid: |
| 1144 | task->tk_status = -EIO; |
| 1145 | return 0; |
| 1146 | wait_on_recovery: |
| 1147 | rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL); |
| 1148 | if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0) |
| 1149 | rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task); |
| 1150 | goto out; |
| 1151 | } |
| 1152 | |
| 1153 | /* Retry all errors through either pNFS or MDS except for -EJUKEBOX */ |
| 1154 | static int ff_layout_async_handle_error_v3(struct rpc_task *task, |
| 1155 | struct pnfs_layout_segment *lseg, |
| 1156 | int idx) |
| 1157 | { |
| 1158 | struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx); |
| 1159 | |
| 1160 | if (task->tk_status >= 0) |
| 1161 | return 0; |
| 1162 | |
Trond Myklebust | 90816d1 | 2015-08-30 09:53:06 -0700 | [diff] [blame] | 1163 | switch (task->tk_status) { |
| 1164 | /* File access problems. Don't mark the device as unavailable */ |
| 1165 | case -EACCES: |
| 1166 | case -ESTALE: |
| 1167 | case -EISDIR: |
| 1168 | case -EBADHANDLE: |
| 1169 | case -ELOOP: |
| 1170 | case -ENOSPC: |
| 1171 | break; |
| 1172 | case -EJUKEBOX: |
| 1173 | nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY); |
| 1174 | goto out_retry; |
| 1175 | default: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1176 | dprintk("%s DS connection error %d\n", __func__, |
| 1177 | task->tk_status); |
Weston Andros Adamson | 1c48cee | 2016-12-14 16:31:55 -0500 | [diff] [blame] | 1178 | nfs4_delete_deviceid(devid->ld, devid->nfs_client, |
| 1179 | &devid->deviceid); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1180 | } |
Trond Myklebust | 90816d1 | 2015-08-30 09:53:06 -0700 | [diff] [blame] | 1181 | /* FIXME: Need to prevent infinite looping here. */ |
| 1182 | return -NFS4ERR_RESET_TO_PNFS; |
| 1183 | out_retry: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1184 | task->tk_status = 0; |
Trond Myklebust | 7eeea16 | 2015-12-17 09:22:15 -0500 | [diff] [blame] | 1185 | rpc_restart_call_prepare(task); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1186 | rpc_delay(task, NFS_JUKEBOX_RETRY_TIME); |
| 1187 | return -EAGAIN; |
| 1188 | } |
| 1189 | |
| 1190 | static int ff_layout_async_handle_error(struct rpc_task *task, |
| 1191 | struct nfs4_state *state, |
| 1192 | struct nfs_client *clp, |
| 1193 | struct pnfs_layout_segment *lseg, |
| 1194 | int idx) |
| 1195 | { |
| 1196 | int vers = clp->cl_nfs_mod->rpc_vers->number; |
| 1197 | |
| 1198 | switch (vers) { |
| 1199 | case 3: |
| 1200 | return ff_layout_async_handle_error_v3(task, lseg, idx); |
| 1201 | case 4: |
| 1202 | return ff_layout_async_handle_error_v4(task, state, clp, |
| 1203 | lseg, idx); |
| 1204 | default: |
| 1205 | /* should never happen */ |
| 1206 | WARN_ON_ONCE(1); |
| 1207 | return 0; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg, |
| 1212 | int idx, u64 offset, u64 length, |
Trond Myklebust | dd52128 | 2015-08-20 17:59:49 -0500 | [diff] [blame] | 1213 | u32 status, int opnum, int error) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1214 | { |
| 1215 | struct nfs4_ff_layout_mirror *mirror; |
| 1216 | int err; |
| 1217 | |
Trond Myklebust | dd52128 | 2015-08-20 17:59:49 -0500 | [diff] [blame] | 1218 | if (status == 0) { |
| 1219 | switch (error) { |
| 1220 | case -ETIMEDOUT: |
| 1221 | case -EPFNOSUPPORT: |
| 1222 | case -EPROTONOSUPPORT: |
| 1223 | case -EOPNOTSUPP: |
| 1224 | case -ECONNREFUSED: |
| 1225 | case -ECONNRESET: |
| 1226 | case -EHOSTDOWN: |
| 1227 | case -EHOSTUNREACH: |
| 1228 | case -ENETUNREACH: |
| 1229 | case -EADDRINUSE: |
| 1230 | case -ENOBUFS: |
| 1231 | case -EPIPE: |
| 1232 | case -EPERM: |
| 1233 | status = NFS4ERR_NXIO; |
| 1234 | break; |
| 1235 | case -EACCES: |
| 1236 | status = NFS4ERR_ACCESS; |
| 1237 | break; |
| 1238 | default: |
| 1239 | return; |
| 1240 | } |
| 1241 | } |
| 1242 | |
Peng Tao | c22eeb8 | 2015-12-07 17:31:38 -0500 | [diff] [blame] | 1243 | switch (status) { |
| 1244 | case NFS4ERR_DELAY: |
| 1245 | case NFS4ERR_GRACE: |
| 1246 | return; |
| 1247 | default: |
| 1248 | break; |
| 1249 | } |
| 1250 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1251 | mirror = FF_LAYOUT_COMP(lseg, idx); |
| 1252 | err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout), |
| 1253 | mirror, offset, length, status, opnum, |
| 1254 | GFP_NOIO); |
Trond Myklebust | f95c03b | 2015-09-02 15:15:11 -0700 | [diff] [blame] | 1255 | pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1256 | dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status); |
| 1257 | } |
| 1258 | |
| 1259 | /* NFS_PROTO call done callback routines */ |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1260 | static int ff_layout_read_done_cb(struct rpc_task *task, |
| 1261 | struct nfs_pgio_header *hdr) |
| 1262 | { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1263 | int err; |
| 1264 | |
| 1265 | trace_nfs4_pnfs_read(hdr, task->tk_status); |
Trond Myklebust | dd52128 | 2015-08-20 17:59:49 -0500 | [diff] [blame] | 1266 | if (task->tk_status < 0) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1267 | ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx, |
| 1268 | hdr->args.offset, hdr->args.count, |
Trond Myklebust | dd52128 | 2015-08-20 17:59:49 -0500 | [diff] [blame] | 1269 | hdr->res.op_status, OP_READ, |
| 1270 | task->tk_status); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1271 | err = ff_layout_async_handle_error(task, hdr->args.context->state, |
| 1272 | hdr->ds_clp, hdr->lseg, |
| 1273 | hdr->pgio_mirror_idx); |
| 1274 | |
| 1275 | switch (err) { |
| 1276 | case -NFS4ERR_RESET_TO_PNFS: |
Trond Myklebust | 1354441 | 2015-11-02 10:11:17 -0500 | [diff] [blame] | 1277 | if (ff_layout_choose_best_ds_for_read(hdr->lseg, |
| 1278 | hdr->pgio_mirror_idx + 1, |
| 1279 | &hdr->pgio_mirror_idx)) |
| 1280 | goto out_eagain; |
Trond Myklebust | 54e4a0d | 2016-11-27 15:12:39 -0500 | [diff] [blame] | 1281 | ff_layout_read_record_layoutstats_done(task, hdr); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1282 | pnfs_read_resend_pnfs(hdr); |
| 1283 | return task->tk_status; |
| 1284 | case -NFS4ERR_RESET_TO_MDS: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1285 | ff_layout_reset_read(hdr); |
| 1286 | return task->tk_status; |
| 1287 | case -EAGAIN: |
Trond Myklebust | 1354441 | 2015-11-02 10:11:17 -0500 | [diff] [blame] | 1288 | goto out_eagain; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | return 0; |
Trond Myklebust | 1354441 | 2015-11-02 10:11:17 -0500 | [diff] [blame] | 1292 | out_eagain: |
| 1293 | rpc_restart_call_prepare(task); |
| 1294 | return -EAGAIN; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1295 | } |
| 1296 | |
Trond Myklebust | c0f5f50 | 2015-06-26 14:51:32 -0400 | [diff] [blame] | 1297 | static bool |
| 1298 | ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg) |
| 1299 | { |
| 1300 | return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT); |
| 1301 | } |
| 1302 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1303 | /* |
| 1304 | * We reference the rpc_cred of the first WRITE that triggers the need for |
| 1305 | * a LAYOUTCOMMIT, and use it to send the layoutcommit compound. |
| 1306 | * rfc5661 is not clear about which credential should be used. |
| 1307 | * |
| 1308 | * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so |
| 1309 | * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751 |
| 1310 | * we always send layoutcommit after DS writes. |
| 1311 | */ |
| 1312 | static void |
Trond Myklebust | bc28e1c | 2016-06-26 16:14:40 -0400 | [diff] [blame] | 1313 | ff_layout_set_layoutcommit(struct inode *inode, |
| 1314 | struct pnfs_layout_segment *lseg, |
| 1315 | loff_t end_offset) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1316 | { |
Trond Myklebust | bc28e1c | 2016-06-26 16:14:40 -0400 | [diff] [blame] | 1317 | if (!ff_layout_need_layoutcommit(lseg)) |
Trond Myklebust | c0f5f50 | 2015-06-26 14:51:32 -0400 | [diff] [blame] | 1318 | return; |
| 1319 | |
Trond Myklebust | bc28e1c | 2016-06-26 16:14:40 -0400 | [diff] [blame] | 1320 | pnfs_set_layoutcommit(inode, lseg, end_offset); |
| 1321 | dprintk("%s inode %lu pls_end_pos %llu\n", __func__, inode->i_ino, |
| 1322 | (unsigned long long) NFS_I(inode)->layout->plh_lwb); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1323 | } |
| 1324 | |
| 1325 | static bool |
Trond Myklebust | 75bf47e | 2016-05-17 12:28:36 -0400 | [diff] [blame] | 1326 | ff_layout_device_unavailable(struct pnfs_layout_segment *lseg, int idx) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1327 | { |
| 1328 | /* No mirroring for now */ |
| 1329 | struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx); |
| 1330 | |
| 1331 | return ff_layout_test_devid_unavailable(node); |
| 1332 | } |
| 1333 | |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1334 | static void ff_layout_read_record_layoutstats_start(struct rpc_task *task, |
| 1335 | struct nfs_pgio_header *hdr) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1336 | { |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1337 | if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags)) |
| 1338 | return; |
Trond Myklebust | 0b7baf9 | 2015-08-24 18:22:28 -0400 | [diff] [blame] | 1339 | nfs4_ff_layout_stat_io_start_read(hdr->inode, |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1340 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 1341 | hdr->args.count, |
| 1342 | task->tk_start); |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1343 | } |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1344 | |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1345 | static void ff_layout_read_record_layoutstats_done(struct rpc_task *task, |
| 1346 | struct nfs_pgio_header *hdr) |
| 1347 | { |
| 1348 | if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags)) |
| 1349 | return; |
| 1350 | nfs4_ff_layout_stat_io_end_read(task, |
| 1351 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), |
| 1352 | hdr->args.count, |
| 1353 | hdr->res.count); |
| 1354 | } |
| 1355 | |
| 1356 | static int ff_layout_read_prepare_common(struct rpc_task *task, |
| 1357 | struct nfs_pgio_header *hdr) |
| 1358 | { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1359 | if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) { |
| 1360 | rpc_exit(task, -EIO); |
| 1361 | return -EIO; |
| 1362 | } |
Trond Myklebust | 75bf47e | 2016-05-17 12:28:36 -0400 | [diff] [blame] | 1363 | if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) { |
| 1364 | rpc_exit(task, -EHOSTDOWN); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1365 | return -EAGAIN; |
| 1366 | } |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1367 | |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1368 | ff_layout_read_record_layoutstats_start(task, hdr); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | /* |
| 1373 | * Call ops for the async read/write cases |
| 1374 | * In the case of dense layouts, the offset needs to be reset to its |
| 1375 | * original value. |
| 1376 | */ |
| 1377 | static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data) |
| 1378 | { |
| 1379 | struct nfs_pgio_header *hdr = data; |
| 1380 | |
| 1381 | if (ff_layout_read_prepare_common(task, hdr)) |
| 1382 | return; |
| 1383 | |
| 1384 | rpc_call_start(task); |
| 1385 | } |
| 1386 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1387 | static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data) |
| 1388 | { |
| 1389 | struct nfs_pgio_header *hdr = data; |
| 1390 | |
Anna Schumaker | 6de7e12 | 2017-01-09 16:51:52 -0500 | [diff] [blame] | 1391 | if (nfs4_setup_sequence(hdr->ds_clp, |
| 1392 | &hdr->args.seq_args, |
| 1393 | &hdr->res.seq_res, |
| 1394 | task)) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1395 | return; |
| 1396 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1397 | if (ff_layout_read_prepare_common(task, hdr)) |
| 1398 | return; |
| 1399 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1400 | if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, |
| 1401 | hdr->args.lock_context, FMODE_READ) == -EIO) |
| 1402 | rpc_exit(task, -EIO); /* lost lock, terminate I/O */ |
| 1403 | } |
| 1404 | |
| 1405 | static void ff_layout_read_call_done(struct rpc_task *task, void *data) |
| 1406 | { |
| 1407 | struct nfs_pgio_header *hdr = data; |
| 1408 | |
| 1409 | dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status); |
| 1410 | |
| 1411 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags) && |
| 1412 | task->tk_status == 0) { |
| 1413 | nfs4_sequence_done(task, &hdr->res.seq_res); |
| 1414 | return; |
| 1415 | } |
| 1416 | |
| 1417 | /* Note this may cause RPC to be resent */ |
| 1418 | hdr->mds_ops->rpc_call_done(task, hdr); |
| 1419 | } |
| 1420 | |
| 1421 | static void ff_layout_read_count_stats(struct rpc_task *task, void *data) |
| 1422 | { |
| 1423 | struct nfs_pgio_header *hdr = data; |
| 1424 | |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1425 | ff_layout_read_record_layoutstats_done(task, hdr); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1426 | rpc_count_iostats_metrics(task, |
| 1427 | &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]); |
| 1428 | } |
| 1429 | |
Trond Myklebust | 4d0ac22 | 2015-12-22 15:32:00 -0500 | [diff] [blame] | 1430 | static void ff_layout_read_release(void *data) |
| 1431 | { |
| 1432 | struct nfs_pgio_header *hdr = data; |
| 1433 | |
| 1434 | ff_layout_read_record_layoutstats_done(&hdr->task, hdr); |
| 1435 | pnfs_generic_rw_release(data); |
| 1436 | } |
| 1437 | |
| 1438 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1439 | static int ff_layout_write_done_cb(struct rpc_task *task, |
| 1440 | struct nfs_pgio_header *hdr) |
| 1441 | { |
Trond Myklebust | 2e18d4d | 2016-06-26 18:54:58 -0400 | [diff] [blame] | 1442 | loff_t end_offs = 0; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1443 | int err; |
| 1444 | |
| 1445 | trace_nfs4_pnfs_write(hdr, task->tk_status); |
Trond Myklebust | dd52128 | 2015-08-20 17:59:49 -0500 | [diff] [blame] | 1446 | if (task->tk_status < 0) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1447 | ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx, |
| 1448 | hdr->args.offset, hdr->args.count, |
Trond Myklebust | dd52128 | 2015-08-20 17:59:49 -0500 | [diff] [blame] | 1449 | hdr->res.op_status, OP_WRITE, |
| 1450 | task->tk_status); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1451 | err = ff_layout_async_handle_error(task, hdr->args.context->state, |
| 1452 | hdr->ds_clp, hdr->lseg, |
| 1453 | hdr->pgio_mirror_idx); |
| 1454 | |
| 1455 | switch (err) { |
| 1456 | case -NFS4ERR_RESET_TO_PNFS: |
Trond Myklebust | 7cc8c5c | 2015-09-02 15:22:48 -0700 | [diff] [blame] | 1457 | ff_layout_reset_write(hdr, true); |
| 1458 | return task->tk_status; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1459 | case -NFS4ERR_RESET_TO_MDS: |
Trond Myklebust | 7cc8c5c | 2015-09-02 15:22:48 -0700 | [diff] [blame] | 1460 | ff_layout_reset_write(hdr, false); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1461 | return task->tk_status; |
| 1462 | case -EAGAIN: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1463 | return -EAGAIN; |
| 1464 | } |
| 1465 | |
| 1466 | if (hdr->res.verf->committed == NFS_FILE_SYNC || |
| 1467 | hdr->res.verf->committed == NFS_DATA_SYNC) |
Trond Myklebust | 2e18d4d | 2016-06-26 18:54:58 -0400 | [diff] [blame] | 1468 | end_offs = hdr->mds_offset + (loff_t)hdr->res.count; |
| 1469 | |
| 1470 | /* Note: if the write is unstable, don't set end_offs until commit */ |
| 1471 | ff_layout_set_layoutcommit(hdr->inode, hdr->lseg, end_offs); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1472 | |
Peng Tao | 5420401 | 2015-08-22 06:40:00 +0800 | [diff] [blame] | 1473 | /* zero out fattr since we don't care DS attr at all */ |
| 1474 | hdr->fattr.valid = 0; |
Peng Tao | 69f230d | 2015-08-20 01:52:59 +0800 | [diff] [blame] | 1475 | if (task->tk_status >= 0) |
| 1476 | nfs_writeback_update_inode(hdr); |
| 1477 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | static int ff_layout_commit_done_cb(struct rpc_task *task, |
| 1482 | struct nfs_commit_data *data) |
| 1483 | { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1484 | int err; |
| 1485 | |
| 1486 | trace_nfs4_pnfs_commit_ds(data, task->tk_status); |
Trond Myklebust | dd52128 | 2015-08-20 17:59:49 -0500 | [diff] [blame] | 1487 | if (task->tk_status < 0) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1488 | ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index, |
| 1489 | data->args.offset, data->args.count, |
Trond Myklebust | dd52128 | 2015-08-20 17:59:49 -0500 | [diff] [blame] | 1490 | data->res.op_status, OP_COMMIT, |
| 1491 | task->tk_status); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1492 | err = ff_layout_async_handle_error(task, NULL, data->ds_clp, |
| 1493 | data->lseg, data->ds_commit_index); |
| 1494 | |
| 1495 | switch (err) { |
| 1496 | case -NFS4ERR_RESET_TO_PNFS: |
Trond Myklebust | 7cc8c5c | 2015-09-02 15:22:48 -0700 | [diff] [blame] | 1497 | pnfs_generic_prepare_to_resend_writes(data); |
| 1498 | return -EAGAIN; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1499 | case -NFS4ERR_RESET_TO_MDS: |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1500 | pnfs_generic_prepare_to_resend_writes(data); |
| 1501 | return -EAGAIN; |
| 1502 | case -EAGAIN: |
| 1503 | rpc_restart_call_prepare(task); |
| 1504 | return -EAGAIN; |
| 1505 | } |
| 1506 | |
Trond Myklebust | bc28e1c | 2016-06-26 16:14:40 -0400 | [diff] [blame] | 1507 | ff_layout_set_layoutcommit(data->inode, data->lseg, data->lwb); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1508 | |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1512 | static void ff_layout_write_record_layoutstats_start(struct rpc_task *task, |
| 1513 | struct nfs_pgio_header *hdr) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1514 | { |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1515 | if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags)) |
| 1516 | return; |
Trond Myklebust | 0b7baf9 | 2015-08-24 18:22:28 -0400 | [diff] [blame] | 1517 | nfs4_ff_layout_stat_io_start_write(hdr->inode, |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1518 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), |
Trond Myklebust | e76d28d | 2015-08-20 13:12:51 -0500 | [diff] [blame] | 1519 | hdr->args.count, |
| 1520 | task->tk_start); |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1521 | } |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1522 | |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1523 | static void ff_layout_write_record_layoutstats_done(struct rpc_task *task, |
| 1524 | struct nfs_pgio_header *hdr) |
| 1525 | { |
| 1526 | if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags)) |
| 1527 | return; |
| 1528 | nfs4_ff_layout_stat_io_end_write(task, |
| 1529 | FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx), |
| 1530 | hdr->args.count, hdr->res.count, |
| 1531 | hdr->res.verf->committed); |
| 1532 | } |
| 1533 | |
| 1534 | static int ff_layout_write_prepare_common(struct rpc_task *task, |
| 1535 | struct nfs_pgio_header *hdr) |
| 1536 | { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1537 | if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) { |
| 1538 | rpc_exit(task, -EIO); |
| 1539 | return -EIO; |
| 1540 | } |
| 1541 | |
Trond Myklebust | 75bf47e | 2016-05-17 12:28:36 -0400 | [diff] [blame] | 1542 | if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) { |
| 1543 | rpc_exit(task, -EHOSTDOWN); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1544 | return -EAGAIN; |
| 1545 | } |
| 1546 | |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1547 | ff_layout_write_record_layoutstats_start(task, hdr); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1548 | return 0; |
| 1549 | } |
| 1550 | |
| 1551 | static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data) |
| 1552 | { |
| 1553 | struct nfs_pgio_header *hdr = data; |
| 1554 | |
| 1555 | if (ff_layout_write_prepare_common(task, hdr)) |
| 1556 | return; |
| 1557 | |
| 1558 | rpc_call_start(task); |
| 1559 | } |
| 1560 | |
| 1561 | static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data) |
| 1562 | { |
| 1563 | struct nfs_pgio_header *hdr = data; |
| 1564 | |
Anna Schumaker | 6de7e12 | 2017-01-09 16:51:52 -0500 | [diff] [blame] | 1565 | if (nfs4_setup_sequence(hdr->ds_clp, |
| 1566 | &hdr->args.seq_args, |
| 1567 | &hdr->res.seq_res, |
| 1568 | task)) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1569 | return; |
| 1570 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1571 | if (ff_layout_write_prepare_common(task, hdr)) |
| 1572 | return; |
| 1573 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1574 | if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context, |
| 1575 | hdr->args.lock_context, FMODE_WRITE) == -EIO) |
| 1576 | rpc_exit(task, -EIO); /* lost lock, terminate I/O */ |
| 1577 | } |
| 1578 | |
| 1579 | static void ff_layout_write_call_done(struct rpc_task *task, void *data) |
| 1580 | { |
| 1581 | struct nfs_pgio_header *hdr = data; |
| 1582 | |
| 1583 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags) && |
| 1584 | task->tk_status == 0) { |
| 1585 | nfs4_sequence_done(task, &hdr->res.seq_res); |
| 1586 | return; |
| 1587 | } |
| 1588 | |
| 1589 | /* Note this may cause RPC to be resent */ |
| 1590 | hdr->mds_ops->rpc_call_done(task, hdr); |
| 1591 | } |
| 1592 | |
| 1593 | static void ff_layout_write_count_stats(struct rpc_task *task, void *data) |
| 1594 | { |
| 1595 | struct nfs_pgio_header *hdr = data; |
| 1596 | |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1597 | ff_layout_write_record_layoutstats_done(task, hdr); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1598 | rpc_count_iostats_metrics(task, |
| 1599 | &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]); |
| 1600 | } |
| 1601 | |
Trond Myklebust | 4d0ac22 | 2015-12-22 15:32:00 -0500 | [diff] [blame] | 1602 | static void ff_layout_write_release(void *data) |
| 1603 | { |
| 1604 | struct nfs_pgio_header *hdr = data; |
| 1605 | |
| 1606 | ff_layout_write_record_layoutstats_done(&hdr->task, hdr); |
| 1607 | pnfs_generic_rw_release(data); |
| 1608 | } |
| 1609 | |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1610 | static void ff_layout_commit_record_layoutstats_start(struct rpc_task *task, |
| 1611 | struct nfs_commit_data *cdata) |
| 1612 | { |
| 1613 | if (test_and_set_bit(NFS_IOHDR_STAT, &cdata->flags)) |
| 1614 | return; |
| 1615 | nfs4_ff_layout_stat_io_start_write(cdata->inode, |
| 1616 | FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index), |
| 1617 | 0, task->tk_start); |
| 1618 | } |
| 1619 | |
| 1620 | static void ff_layout_commit_record_layoutstats_done(struct rpc_task *task, |
| 1621 | struct nfs_commit_data *cdata) |
| 1622 | { |
| 1623 | struct nfs_page *req; |
| 1624 | __u64 count = 0; |
| 1625 | |
| 1626 | if (!test_and_clear_bit(NFS_IOHDR_STAT, &cdata->flags)) |
| 1627 | return; |
| 1628 | |
| 1629 | if (task->tk_status == 0) { |
| 1630 | list_for_each_entry(req, &cdata->pages, wb_list) |
| 1631 | count += req->wb_bytes; |
| 1632 | } |
| 1633 | nfs4_ff_layout_stat_io_end_write(task, |
| 1634 | FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index), |
| 1635 | count, count, NFS_FILE_SYNC); |
| 1636 | } |
| 1637 | |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1638 | static void ff_layout_commit_prepare_common(struct rpc_task *task, |
| 1639 | struct nfs_commit_data *cdata) |
| 1640 | { |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1641 | ff_layout_commit_record_layoutstats_start(task, cdata); |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1642 | } |
| 1643 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1644 | static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data) |
| 1645 | { |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1646 | ff_layout_commit_prepare_common(task, data); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1647 | rpc_call_start(task); |
| 1648 | } |
| 1649 | |
| 1650 | static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data) |
| 1651 | { |
| 1652 | struct nfs_commit_data *wdata = data; |
| 1653 | |
Anna Schumaker | 6de7e12 | 2017-01-09 16:51:52 -0500 | [diff] [blame] | 1654 | if (nfs4_setup_sequence(wdata->ds_clp, |
| 1655 | &wdata->args.seq_args, |
| 1656 | &wdata->res.seq_res, |
| 1657 | task)) |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1658 | return; |
| 1659 | ff_layout_commit_prepare_common(task, data); |
| 1660 | } |
| 1661 | |
| 1662 | static void ff_layout_commit_done(struct rpc_task *task, void *data) |
| 1663 | { |
Peng Tao | 141b9b5 | 2015-12-07 18:33:14 -0500 | [diff] [blame] | 1664 | pnfs_generic_write_commit_done(task, data); |
| 1665 | } |
| 1666 | |
| 1667 | static void ff_layout_commit_count_stats(struct rpc_task *task, void *data) |
| 1668 | { |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1669 | struct nfs_commit_data *cdata = data; |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1670 | |
Trond Myklebust | 37e9ed2 | 2015-12-22 12:30:24 -0500 | [diff] [blame] | 1671 | ff_layout_commit_record_layoutstats_done(task, cdata); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1672 | rpc_count_iostats_metrics(task, |
| 1673 | &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]); |
| 1674 | } |
| 1675 | |
Trond Myklebust | 4d0ac22 | 2015-12-22 15:32:00 -0500 | [diff] [blame] | 1676 | static void ff_layout_commit_release(void *data) |
| 1677 | { |
| 1678 | struct nfs_commit_data *cdata = data; |
| 1679 | |
| 1680 | ff_layout_commit_record_layoutstats_done(&cdata->task, cdata); |
| 1681 | pnfs_generic_commit_release(data); |
| 1682 | } |
| 1683 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1684 | static const struct rpc_call_ops ff_layout_read_call_ops_v3 = { |
| 1685 | .rpc_call_prepare = ff_layout_read_prepare_v3, |
| 1686 | .rpc_call_done = ff_layout_read_call_done, |
| 1687 | .rpc_count_stats = ff_layout_read_count_stats, |
Trond Myklebust | 4d0ac22 | 2015-12-22 15:32:00 -0500 | [diff] [blame] | 1688 | .rpc_release = ff_layout_read_release, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1689 | }; |
| 1690 | |
| 1691 | static const struct rpc_call_ops ff_layout_read_call_ops_v4 = { |
| 1692 | .rpc_call_prepare = ff_layout_read_prepare_v4, |
| 1693 | .rpc_call_done = ff_layout_read_call_done, |
| 1694 | .rpc_count_stats = ff_layout_read_count_stats, |
Trond Myklebust | 4d0ac22 | 2015-12-22 15:32:00 -0500 | [diff] [blame] | 1695 | .rpc_release = ff_layout_read_release, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1696 | }; |
| 1697 | |
| 1698 | static const struct rpc_call_ops ff_layout_write_call_ops_v3 = { |
| 1699 | .rpc_call_prepare = ff_layout_write_prepare_v3, |
| 1700 | .rpc_call_done = ff_layout_write_call_done, |
| 1701 | .rpc_count_stats = ff_layout_write_count_stats, |
Trond Myklebust | 4d0ac22 | 2015-12-22 15:32:00 -0500 | [diff] [blame] | 1702 | .rpc_release = ff_layout_write_release, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1703 | }; |
| 1704 | |
| 1705 | static const struct rpc_call_ops ff_layout_write_call_ops_v4 = { |
| 1706 | .rpc_call_prepare = ff_layout_write_prepare_v4, |
| 1707 | .rpc_call_done = ff_layout_write_call_done, |
| 1708 | .rpc_count_stats = ff_layout_write_count_stats, |
Trond Myklebust | 4d0ac22 | 2015-12-22 15:32:00 -0500 | [diff] [blame] | 1709 | .rpc_release = ff_layout_write_release, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1710 | }; |
| 1711 | |
| 1712 | static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = { |
| 1713 | .rpc_call_prepare = ff_layout_commit_prepare_v3, |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1714 | .rpc_call_done = ff_layout_commit_done, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1715 | .rpc_count_stats = ff_layout_commit_count_stats, |
Trond Myklebust | 4d0ac22 | 2015-12-22 15:32:00 -0500 | [diff] [blame] | 1716 | .rpc_release = ff_layout_commit_release, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1717 | }; |
| 1718 | |
| 1719 | static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = { |
| 1720 | .rpc_call_prepare = ff_layout_commit_prepare_v4, |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1721 | .rpc_call_done = ff_layout_commit_done, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1722 | .rpc_count_stats = ff_layout_commit_count_stats, |
Trond Myklebust | 4d0ac22 | 2015-12-22 15:32:00 -0500 | [diff] [blame] | 1723 | .rpc_release = ff_layout_commit_release, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1724 | }; |
| 1725 | |
| 1726 | static enum pnfs_try_status |
| 1727 | ff_layout_read_pagelist(struct nfs_pgio_header *hdr) |
| 1728 | { |
| 1729 | struct pnfs_layout_segment *lseg = hdr->lseg; |
| 1730 | struct nfs4_pnfs_ds *ds; |
| 1731 | struct rpc_clnt *ds_clnt; |
| 1732 | struct rpc_cred *ds_cred; |
| 1733 | loff_t offset = hdr->args.offset; |
| 1734 | u32 idx = hdr->pgio_mirror_idx; |
| 1735 | int vers; |
| 1736 | struct nfs_fh *fh; |
| 1737 | |
| 1738 | dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n", |
| 1739 | __func__, hdr->inode->i_ino, |
| 1740 | hdr->args.pgbase, (size_t)hdr->args.count, offset); |
| 1741 | |
| 1742 | ds = nfs4_ff_layout_prepare_ds(lseg, idx, false); |
| 1743 | if (!ds) |
| 1744 | goto out_failed; |
| 1745 | |
| 1746 | ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, |
| 1747 | hdr->inode); |
| 1748 | if (IS_ERR(ds_clnt)) |
| 1749 | goto out_failed; |
| 1750 | |
| 1751 | ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred); |
Jeff Layton | 57f3f4c | 2016-04-21 20:51:58 -0400 | [diff] [blame] | 1752 | if (!ds_cred) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1753 | goto out_failed; |
| 1754 | |
| 1755 | vers = nfs4_ff_layout_ds_version(lseg, idx); |
| 1756 | |
| 1757 | dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__, |
| 1758 | ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers); |
| 1759 | |
Trond Myklebust | 75bf47e | 2016-05-17 12:28:36 -0400 | [diff] [blame] | 1760 | hdr->pgio_done_cb = ff_layout_read_done_cb; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1761 | atomic_inc(&ds->ds_clp->cl_count); |
| 1762 | hdr->ds_clp = ds->ds_clp; |
| 1763 | fh = nfs4_ff_layout_select_ds_fh(lseg, idx); |
| 1764 | if (fh) |
| 1765 | hdr->args.fh = fh; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1766 | /* |
| 1767 | * Note that if we ever decide to split across DSes, |
| 1768 | * then we may need to handle dense-like offsets. |
| 1769 | */ |
| 1770 | hdr->args.offset = offset; |
| 1771 | hdr->mds_offset = offset; |
| 1772 | |
| 1773 | /* Perform an asynchronous read to ds */ |
| 1774 | nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, |
| 1775 | vers == 3 ? &ff_layout_read_call_ops_v3 : |
| 1776 | &ff_layout_read_call_ops_v4, |
| 1777 | 0, RPC_TASK_SOFTCONN); |
Jeff Layton | 57f3f4c | 2016-04-21 20:51:58 -0400 | [diff] [blame] | 1778 | put_rpccred(ds_cred); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1779 | return PNFS_ATTEMPTED; |
| 1780 | |
| 1781 | out_failed: |
Tom Haynes | 3b13b4b | 2016-05-17 12:28:37 -0400 | [diff] [blame] | 1782 | if (ff_layout_avoid_mds_available_ds(lseg)) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1783 | return PNFS_TRY_AGAIN; |
| 1784 | return PNFS_NOT_ATTEMPTED; |
| 1785 | } |
| 1786 | |
| 1787 | /* Perform async writes. */ |
| 1788 | static enum pnfs_try_status |
| 1789 | ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync) |
| 1790 | { |
| 1791 | struct pnfs_layout_segment *lseg = hdr->lseg; |
| 1792 | struct nfs4_pnfs_ds *ds; |
| 1793 | struct rpc_clnt *ds_clnt; |
| 1794 | struct rpc_cred *ds_cred; |
| 1795 | loff_t offset = hdr->args.offset; |
| 1796 | int vers; |
| 1797 | struct nfs_fh *fh; |
| 1798 | int idx = hdr->pgio_mirror_idx; |
| 1799 | |
| 1800 | ds = nfs4_ff_layout_prepare_ds(lseg, idx, true); |
| 1801 | if (!ds) |
| 1802 | return PNFS_NOT_ATTEMPTED; |
| 1803 | |
| 1804 | ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, |
| 1805 | hdr->inode); |
| 1806 | if (IS_ERR(ds_clnt)) |
| 1807 | return PNFS_NOT_ATTEMPTED; |
| 1808 | |
| 1809 | ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred); |
Jeff Layton | 57f3f4c | 2016-04-21 20:51:58 -0400 | [diff] [blame] | 1810 | if (!ds_cred) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1811 | return PNFS_NOT_ATTEMPTED; |
| 1812 | |
| 1813 | vers = nfs4_ff_layout_ds_version(lseg, idx); |
| 1814 | |
| 1815 | dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n", |
| 1816 | __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count, |
| 1817 | offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), |
| 1818 | vers); |
| 1819 | |
| 1820 | hdr->pgio_done_cb = ff_layout_write_done_cb; |
| 1821 | atomic_inc(&ds->ds_clp->cl_count); |
| 1822 | hdr->ds_clp = ds->ds_clp; |
| 1823 | hdr->ds_commit_idx = idx; |
| 1824 | fh = nfs4_ff_layout_select_ds_fh(lseg, idx); |
| 1825 | if (fh) |
| 1826 | hdr->args.fh = fh; |
| 1827 | |
| 1828 | /* |
| 1829 | * Note that if we ever decide to split across DSes, |
| 1830 | * then we may need to handle dense-like offsets. |
| 1831 | */ |
| 1832 | hdr->args.offset = offset; |
| 1833 | |
| 1834 | /* Perform an asynchronous write */ |
| 1835 | nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops, |
| 1836 | vers == 3 ? &ff_layout_write_call_ops_v3 : |
| 1837 | &ff_layout_write_call_ops_v4, |
| 1838 | sync, RPC_TASK_SOFTCONN); |
Jeff Layton | 57f3f4c | 2016-04-21 20:51:58 -0400 | [diff] [blame] | 1839 | put_rpccred(ds_cred); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1840 | return PNFS_ATTEMPTED; |
| 1841 | } |
| 1842 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1843 | static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i) |
| 1844 | { |
| 1845 | return i; |
| 1846 | } |
| 1847 | |
| 1848 | static struct nfs_fh * |
| 1849 | select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i) |
| 1850 | { |
| 1851 | struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg); |
| 1852 | |
| 1853 | /* FIXME: Assume that there is only one NFS version available |
| 1854 | * for the DS. |
| 1855 | */ |
| 1856 | return &flseg->mirror_array[i]->fh_versions[0]; |
| 1857 | } |
| 1858 | |
| 1859 | static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how) |
| 1860 | { |
| 1861 | struct pnfs_layout_segment *lseg = data->lseg; |
| 1862 | struct nfs4_pnfs_ds *ds; |
| 1863 | struct rpc_clnt *ds_clnt; |
| 1864 | struct rpc_cred *ds_cred; |
| 1865 | u32 idx; |
Jeff Layton | 57f3f4c | 2016-04-21 20:51:58 -0400 | [diff] [blame] | 1866 | int vers, ret; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1867 | struct nfs_fh *fh; |
| 1868 | |
| 1869 | idx = calc_ds_index_from_commit(lseg, data->ds_commit_index); |
| 1870 | ds = nfs4_ff_layout_prepare_ds(lseg, idx, true); |
| 1871 | if (!ds) |
| 1872 | goto out_err; |
| 1873 | |
| 1874 | ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp, |
| 1875 | data->inode); |
| 1876 | if (IS_ERR(ds_clnt)) |
| 1877 | goto out_err; |
| 1878 | |
| 1879 | ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred); |
Jeff Layton | 57f3f4c | 2016-04-21 20:51:58 -0400 | [diff] [blame] | 1880 | if (!ds_cred) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1881 | goto out_err; |
| 1882 | |
| 1883 | vers = nfs4_ff_layout_ds_version(lseg, idx); |
| 1884 | |
| 1885 | dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__, |
| 1886 | data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count), |
| 1887 | vers); |
| 1888 | data->commit_done_cb = ff_layout_commit_done_cb; |
| 1889 | data->cred = ds_cred; |
| 1890 | atomic_inc(&ds->ds_clp->cl_count); |
| 1891 | data->ds_clp = ds->ds_clp; |
| 1892 | fh = select_ds_fh_from_commit(lseg, data->ds_commit_index); |
| 1893 | if (fh) |
| 1894 | data->args.fh = fh; |
Trond Myklebust | abcb7bf | 2015-06-23 19:51:59 +0800 | [diff] [blame] | 1895 | |
Jeff Layton | 57f3f4c | 2016-04-21 20:51:58 -0400 | [diff] [blame] | 1896 | ret = nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1897 | vers == 3 ? &ff_layout_commit_call_ops_v3 : |
| 1898 | &ff_layout_commit_call_ops_v4, |
| 1899 | how, RPC_TASK_SOFTCONN); |
Jeff Layton | 57f3f4c | 2016-04-21 20:51:58 -0400 | [diff] [blame] | 1900 | put_rpccred(ds_cred); |
| 1901 | return ret; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1902 | out_err: |
| 1903 | pnfs_generic_prepare_to_resend_writes(data); |
| 1904 | pnfs_generic_commit_release(data); |
| 1905 | return -EAGAIN; |
| 1906 | } |
| 1907 | |
| 1908 | static int |
| 1909 | ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages, |
| 1910 | int how, struct nfs_commit_info *cinfo) |
| 1911 | { |
| 1912 | return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo, |
| 1913 | ff_layout_initiate_commit); |
| 1914 | } |
| 1915 | |
| 1916 | static struct pnfs_ds_commit_info * |
| 1917 | ff_layout_get_ds_info(struct inode *inode) |
| 1918 | { |
| 1919 | struct pnfs_layout_hdr *layout = NFS_I(inode)->layout; |
| 1920 | |
| 1921 | if (layout == NULL) |
| 1922 | return NULL; |
| 1923 | |
| 1924 | return &FF_LAYOUT_FROM_HDR(layout)->commit_info; |
| 1925 | } |
| 1926 | |
| 1927 | static void |
Trond Myklebust | fc87701 | 2015-03-09 17:25:14 -0400 | [diff] [blame] | 1928 | ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1929 | { |
| 1930 | nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds, |
| 1931 | id_node)); |
| 1932 | } |
| 1933 | |
Trond Myklebust | 5ba6a09 | 2016-12-09 18:24:18 -0500 | [diff] [blame] | 1934 | static int ff_layout_encode_ioerr(struct xdr_stream *xdr, |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 1935 | const struct nfs4_layoutreturn_args *args, |
| 1936 | const struct nfs4_flexfile_layoutreturn_args *ff_args) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1937 | { |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1938 | __be32 *start; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1939 | |
| 1940 | start = xdr_reserve_space(xdr, 4); |
| 1941 | if (unlikely(!start)) |
| 1942 | return -E2BIG; |
| 1943 | |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 1944 | *start = cpu_to_be32(ff_args->num_errors); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1945 | /* This assume we always return _ALL_ layouts */ |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 1946 | return ff_layout_encode_ds_ioerr(xdr, &ff_args->errors); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1947 | } |
| 1948 | |
Trond Myklebust | 230bc96 | 2016-10-19 15:59:28 -0400 | [diff] [blame] | 1949 | static void |
| 1950 | encode_opaque_fixed(struct xdr_stream *xdr, const void *buf, size_t len) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1951 | { |
| 1952 | __be32 *p; |
| 1953 | |
Trond Myklebust | 230bc96 | 2016-10-19 15:59:28 -0400 | [diff] [blame] | 1954 | p = xdr_reserve_space(xdr, len); |
| 1955 | xdr_encode_opaque_fixed(p, buf, len); |
| 1956 | } |
| 1957 | |
| 1958 | static void |
| 1959 | ff_layout_encode_ff_iostat_head(struct xdr_stream *xdr, |
| 1960 | const nfs4_stateid *stateid, |
| 1961 | const struct nfs42_layoutstat_devinfo *devinfo) |
| 1962 | { |
| 1963 | __be32 *p; |
| 1964 | |
| 1965 | p = xdr_reserve_space(xdr, 8 + 8); |
| 1966 | p = xdr_encode_hyper(p, devinfo->offset); |
| 1967 | p = xdr_encode_hyper(p, devinfo->length); |
| 1968 | encode_opaque_fixed(xdr, stateid->data, NFS4_STATEID_SIZE); |
| 1969 | p = xdr_reserve_space(xdr, 4*8); |
| 1970 | p = xdr_encode_hyper(p, devinfo->read_count); |
| 1971 | p = xdr_encode_hyper(p, devinfo->read_bytes); |
| 1972 | p = xdr_encode_hyper(p, devinfo->write_count); |
| 1973 | p = xdr_encode_hyper(p, devinfo->write_bytes); |
| 1974 | encode_opaque_fixed(xdr, devinfo->dev_id.data, NFS4_DEVICEID4_SIZE); |
| 1975 | } |
| 1976 | |
| 1977 | static void |
| 1978 | ff_layout_encode_ff_iostat(struct xdr_stream *xdr, |
| 1979 | const nfs4_stateid *stateid, |
| 1980 | const struct nfs42_layoutstat_devinfo *devinfo) |
| 1981 | { |
| 1982 | ff_layout_encode_ff_iostat_head(xdr, stateid, devinfo); |
| 1983 | ff_layout_encode_ff_layoutupdate(xdr, devinfo, |
| 1984 | devinfo->ld_private.data); |
| 1985 | } |
| 1986 | |
| 1987 | /* report nothing for now */ |
| 1988 | static void ff_layout_encode_iostats_array(struct xdr_stream *xdr, |
| 1989 | const struct nfs4_layoutreturn_args *args, |
| 1990 | struct nfs4_flexfile_layoutreturn_args *ff_args) |
| 1991 | { |
| 1992 | __be32 *p; |
| 1993 | int i; |
| 1994 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 1995 | p = xdr_reserve_space(xdr, 4); |
Trond Myklebust | 230bc96 | 2016-10-19 15:59:28 -0400 | [diff] [blame] | 1996 | *p = cpu_to_be32(ff_args->num_dev); |
| 1997 | for (i = 0; i < ff_args->num_dev; i++) |
| 1998 | ff_layout_encode_ff_iostat(xdr, |
| 1999 | &args->layout->plh_stateid, |
| 2000 | &ff_args->devinfo[i]); |
| 2001 | } |
| 2002 | |
| 2003 | static void |
| 2004 | ff_layout_free_iostats_array(struct nfs42_layoutstat_devinfo *devinfo, |
| 2005 | unsigned int num_entries) |
| 2006 | { |
| 2007 | unsigned int i; |
| 2008 | |
| 2009 | for (i = 0; i < num_entries; i++) { |
| 2010 | if (!devinfo[i].ld_private.ops) |
| 2011 | continue; |
| 2012 | if (!devinfo[i].ld_private.ops->free) |
| 2013 | continue; |
| 2014 | devinfo[i].ld_private.ops->free(&devinfo[i].ld_private); |
| 2015 | } |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | static struct nfs4_deviceid_node * |
| 2019 | ff_layout_alloc_deviceid_node(struct nfs_server *server, |
| 2020 | struct pnfs_device *pdev, gfp_t gfp_flags) |
| 2021 | { |
| 2022 | struct nfs4_ff_layout_ds *dsaddr; |
| 2023 | |
| 2024 | dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags); |
| 2025 | if (!dsaddr) |
| 2026 | return NULL; |
| 2027 | return &dsaddr->id_node; |
| 2028 | } |
| 2029 | |
| 2030 | static void |
Trond Myklebust | 94e5c57 | 2016-09-15 18:49:52 -0400 | [diff] [blame] | 2031 | ff_layout_encode_layoutreturn(struct xdr_stream *xdr, |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2032 | const void *voidargs, |
| 2033 | const struct nfs4_xdr_opaque_data *ff_opaque) |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2034 | { |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2035 | const struct nfs4_layoutreturn_args *args = voidargs; |
Trond Myklebust | 5ba6a09 | 2016-12-09 18:24:18 -0500 | [diff] [blame] | 2036 | struct nfs4_flexfile_layoutreturn_args *ff_args = ff_opaque->data; |
Trond Myklebust | d915211 | 2016-12-09 18:07:51 -0500 | [diff] [blame] | 2037 | struct xdr_buf tmp_buf = { |
| 2038 | .head = { |
| 2039 | [0] = { |
| 2040 | .iov_base = page_address(ff_args->pages[0]), |
| 2041 | }, |
| 2042 | }, |
| 2043 | .buflen = PAGE_SIZE, |
| 2044 | }; |
| 2045 | struct xdr_stream tmp_xdr; |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2046 | __be32 *start; |
| 2047 | |
| 2048 | dprintk("%s: Begin\n", __func__); |
Trond Myklebust | d915211 | 2016-12-09 18:07:51 -0500 | [diff] [blame] | 2049 | |
| 2050 | xdr_init_encode(&tmp_xdr, &tmp_buf, NULL); |
| 2051 | |
| 2052 | ff_layout_encode_ioerr(&tmp_xdr, args, ff_args); |
| 2053 | ff_layout_encode_iostats_array(&tmp_xdr, args, ff_args); |
| 2054 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2055 | start = xdr_reserve_space(xdr, 4); |
Trond Myklebust | d915211 | 2016-12-09 18:07:51 -0500 | [diff] [blame] | 2056 | *start = cpu_to_be32(tmp_buf.len); |
| 2057 | xdr_write_pages(xdr, ff_args->pages, 0, tmp_buf.len); |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2058 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2059 | dprintk("%s: Return\n", __func__); |
| 2060 | } |
| 2061 | |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2062 | static void |
| 2063 | ff_layout_free_layoutreturn(struct nfs4_xdr_opaque_data *args) |
| 2064 | { |
| 2065 | struct nfs4_flexfile_layoutreturn_args *ff_args; |
| 2066 | |
| 2067 | if (!args->data) |
| 2068 | return; |
| 2069 | ff_args = args->data; |
| 2070 | args->data = NULL; |
| 2071 | |
| 2072 | ff_layout_free_ds_ioerr(&ff_args->errors); |
Trond Myklebust | 230bc96 | 2016-10-19 15:59:28 -0400 | [diff] [blame] | 2073 | ff_layout_free_iostats_array(ff_args->devinfo, ff_args->num_dev); |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2074 | |
Trond Myklebust | d915211 | 2016-12-09 18:07:51 -0500 | [diff] [blame] | 2075 | put_page(ff_args->pages[0]); |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2076 | kfree(ff_args); |
| 2077 | } |
| 2078 | |
Wei Yongjun | 2e54b9b | 2017-01-12 15:38:06 +0000 | [diff] [blame^] | 2079 | static const struct nfs4_xdr_opaque_ops layoutreturn_ops = { |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2080 | .encode = ff_layout_encode_layoutreturn, |
| 2081 | .free = ff_layout_free_layoutreturn, |
| 2082 | }; |
| 2083 | |
| 2084 | static int |
| 2085 | ff_layout_prepare_layoutreturn(struct nfs4_layoutreturn_args *args) |
| 2086 | { |
| 2087 | struct nfs4_flexfile_layoutreturn_args *ff_args; |
Trond Myklebust | 230bc96 | 2016-10-19 15:59:28 -0400 | [diff] [blame] | 2088 | struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(args->layout); |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2089 | |
| 2090 | ff_args = kmalloc(sizeof(*ff_args), GFP_KERNEL); |
| 2091 | if (!ff_args) |
Trond Myklebust | d915211 | 2016-12-09 18:07:51 -0500 | [diff] [blame] | 2092 | goto out_nomem; |
| 2093 | ff_args->pages[0] = alloc_page(GFP_KERNEL); |
| 2094 | if (!ff_args->pages[0]) |
| 2095 | goto out_nomem_free; |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2096 | |
| 2097 | INIT_LIST_HEAD(&ff_args->errors); |
| 2098 | ff_args->num_errors = ff_layout_fetch_ds_ioerr(args->layout, |
| 2099 | &args->range, &ff_args->errors, |
| 2100 | FF_LAYOUTRETURN_MAXERR); |
| 2101 | |
Trond Myklebust | 230bc96 | 2016-10-19 15:59:28 -0400 | [diff] [blame] | 2102 | spin_lock(&args->inode->i_lock); |
| 2103 | ff_args->num_dev = ff_layout_mirror_prepare_stats(&ff_layout->generic_hdr, |
| 2104 | &ff_args->devinfo[0], ARRAY_SIZE(ff_args->devinfo)); |
| 2105 | spin_unlock(&args->inode->i_lock); |
| 2106 | |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2107 | args->ld_private->ops = &layoutreturn_ops; |
| 2108 | args->ld_private->data = ff_args; |
| 2109 | return 0; |
Trond Myklebust | d915211 | 2016-12-09 18:07:51 -0500 | [diff] [blame] | 2110 | out_nomem_free: |
| 2111 | kfree(ff_args); |
| 2112 | out_nomem: |
| 2113 | return -ENOMEM; |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2114 | } |
| 2115 | |
Peng Tao | 27c4306 | 2015-06-23 19:52:02 +0800 | [diff] [blame] | 2116 | static int |
| 2117 | ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen) |
| 2118 | { |
| 2119 | const struct sockaddr_in *sin = (struct sockaddr_in *)sap; |
| 2120 | |
| 2121 | return snprintf(buf, buflen, "%pI4", &sin->sin_addr); |
| 2122 | } |
| 2123 | |
| 2124 | static size_t |
| 2125 | ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf, |
| 2126 | const int buflen) |
| 2127 | { |
| 2128 | const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap; |
| 2129 | const struct in6_addr *addr = &sin6->sin6_addr; |
| 2130 | |
| 2131 | /* |
| 2132 | * RFC 4291, Section 2.2.2 |
| 2133 | * |
| 2134 | * Shorthanded ANY address |
| 2135 | */ |
| 2136 | if (ipv6_addr_any(addr)) |
| 2137 | return snprintf(buf, buflen, "::"); |
| 2138 | |
| 2139 | /* |
| 2140 | * RFC 4291, Section 2.2.2 |
| 2141 | * |
| 2142 | * Shorthanded loopback address |
| 2143 | */ |
| 2144 | if (ipv6_addr_loopback(addr)) |
| 2145 | return snprintf(buf, buflen, "::1"); |
| 2146 | |
| 2147 | /* |
| 2148 | * RFC 4291, Section 2.2.3 |
| 2149 | * |
| 2150 | * Special presentation address format for mapped v4 |
| 2151 | * addresses. |
| 2152 | */ |
| 2153 | if (ipv6_addr_v4mapped(addr)) |
| 2154 | return snprintf(buf, buflen, "::ffff:%pI4", |
| 2155 | &addr->s6_addr32[3]); |
| 2156 | |
| 2157 | /* |
| 2158 | * RFC 4291, Section 2.2.1 |
| 2159 | */ |
| 2160 | return snprintf(buf, buflen, "%pI6c", addr); |
| 2161 | } |
| 2162 | |
| 2163 | /* Derived from rpc_sockaddr2uaddr */ |
| 2164 | static void |
| 2165 | ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da) |
| 2166 | { |
| 2167 | struct sockaddr *sap = (struct sockaddr *)&da->da_addr; |
| 2168 | char portbuf[RPCBIND_MAXUADDRPLEN]; |
| 2169 | char addrbuf[RPCBIND_MAXUADDRLEN]; |
| 2170 | char *netid; |
| 2171 | unsigned short port; |
| 2172 | int len, netid_len; |
| 2173 | __be32 *p; |
| 2174 | |
| 2175 | switch (sap->sa_family) { |
| 2176 | case AF_INET: |
| 2177 | if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0) |
| 2178 | return; |
| 2179 | port = ntohs(((struct sockaddr_in *)sap)->sin_port); |
| 2180 | netid = "tcp"; |
| 2181 | netid_len = 3; |
| 2182 | break; |
| 2183 | case AF_INET6: |
| 2184 | if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0) |
| 2185 | return; |
| 2186 | port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port); |
| 2187 | netid = "tcp6"; |
| 2188 | netid_len = 4; |
| 2189 | break; |
| 2190 | default: |
| 2191 | /* we only support tcp and tcp6 */ |
| 2192 | WARN_ON_ONCE(1); |
| 2193 | return; |
| 2194 | } |
| 2195 | |
| 2196 | snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff); |
| 2197 | len = strlcat(addrbuf, portbuf, sizeof(addrbuf)); |
| 2198 | |
| 2199 | p = xdr_reserve_space(xdr, 4 + netid_len); |
| 2200 | xdr_encode_opaque(p, netid, netid_len); |
| 2201 | |
| 2202 | p = xdr_reserve_space(xdr, 4 + len); |
| 2203 | xdr_encode_opaque(p, addrbuf, len); |
| 2204 | } |
| 2205 | |
| 2206 | static void |
| 2207 | ff_layout_encode_nfstime(struct xdr_stream *xdr, |
| 2208 | ktime_t t) |
| 2209 | { |
| 2210 | struct timespec64 ts; |
| 2211 | __be32 *p; |
| 2212 | |
| 2213 | p = xdr_reserve_space(xdr, 12); |
| 2214 | ts = ktime_to_timespec64(t); |
| 2215 | p = xdr_encode_hyper(p, ts.tv_sec); |
| 2216 | *p++ = cpu_to_be32(ts.tv_nsec); |
| 2217 | } |
| 2218 | |
| 2219 | static void |
| 2220 | ff_layout_encode_io_latency(struct xdr_stream *xdr, |
| 2221 | struct nfs4_ff_io_stat *stat) |
| 2222 | { |
| 2223 | __be32 *p; |
| 2224 | |
| 2225 | p = xdr_reserve_space(xdr, 5 * 8); |
| 2226 | p = xdr_encode_hyper(p, stat->ops_requested); |
| 2227 | p = xdr_encode_hyper(p, stat->bytes_requested); |
| 2228 | p = xdr_encode_hyper(p, stat->ops_completed); |
| 2229 | p = xdr_encode_hyper(p, stat->bytes_completed); |
| 2230 | p = xdr_encode_hyper(p, stat->bytes_not_delivered); |
| 2231 | ff_layout_encode_nfstime(xdr, stat->total_busy_time); |
| 2232 | ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time); |
| 2233 | } |
| 2234 | |
| 2235 | static void |
Trond Myklebust | 422c93c | 2016-10-06 17:53:20 -0400 | [diff] [blame] | 2236 | ff_layout_encode_ff_layoutupdate(struct xdr_stream *xdr, |
| 2237 | const struct nfs42_layoutstat_devinfo *devinfo, |
| 2238 | struct nfs4_ff_layout_mirror *mirror) |
Peng Tao | 27c4306 | 2015-06-23 19:52:02 +0800 | [diff] [blame] | 2239 | { |
Peng Tao | 27c4306 | 2015-06-23 19:52:02 +0800 | [diff] [blame] | 2240 | struct nfs4_pnfs_ds_addr *da; |
| 2241 | struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds; |
| 2242 | struct nfs_fh *fh = &mirror->fh_versions[0]; |
Trond Myklebust | 422c93c | 2016-10-06 17:53:20 -0400 | [diff] [blame] | 2243 | __be32 *p; |
Peng Tao | 27c4306 | 2015-06-23 19:52:02 +0800 | [diff] [blame] | 2244 | |
| 2245 | da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node); |
| 2246 | dprintk("%s: DS %s: encoding address %s\n", |
| 2247 | __func__, ds->ds_remotestr, da->da_remotestr); |
Peng Tao | 27c4306 | 2015-06-23 19:52:02 +0800 | [diff] [blame] | 2248 | /* netaddr4 */ |
| 2249 | ff_layout_encode_netaddr(xdr, da); |
| 2250 | /* nfs_fh4 */ |
| 2251 | p = xdr_reserve_space(xdr, 4 + fh->size); |
| 2252 | xdr_encode_opaque(p, fh->data, fh->size); |
| 2253 | /* ff_io_latency4 read */ |
| 2254 | spin_lock(&mirror->lock); |
| 2255 | ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat); |
| 2256 | /* ff_io_latency4 write */ |
| 2257 | ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat); |
| 2258 | spin_unlock(&mirror->lock); |
| 2259 | /* nfstime4 */ |
| 2260 | ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time)); |
| 2261 | /* bool */ |
| 2262 | p = xdr_reserve_space(xdr, 4); |
| 2263 | *p = cpu_to_be32(false); |
Trond Myklebust | 422c93c | 2016-10-06 17:53:20 -0400 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | static void |
| 2267 | ff_layout_encode_layoutstats(struct xdr_stream *xdr, const void *args, |
| 2268 | const struct nfs4_xdr_opaque_data *opaque) |
| 2269 | { |
| 2270 | struct nfs42_layoutstat_devinfo *devinfo = container_of(opaque, |
| 2271 | struct nfs42_layoutstat_devinfo, ld_private); |
| 2272 | __be32 *start; |
| 2273 | |
| 2274 | /* layoutupdate length */ |
| 2275 | start = xdr_reserve_space(xdr, 4); |
| 2276 | ff_layout_encode_ff_layoutupdate(xdr, devinfo, opaque->data); |
Peng Tao | 27c4306 | 2015-06-23 19:52:02 +0800 | [diff] [blame] | 2277 | |
| 2278 | *start = cpu_to_be32((xdr->p - start - 1) * 4); |
| 2279 | } |
| 2280 | |
Trond Myklebust | 422c93c | 2016-10-06 17:53:20 -0400 | [diff] [blame] | 2281 | static void |
| 2282 | ff_layout_free_layoutstats(struct nfs4_xdr_opaque_data *opaque) |
| 2283 | { |
| 2284 | struct nfs4_ff_layout_mirror *mirror = opaque->data; |
| 2285 | |
| 2286 | ff_layout_put_mirror(mirror); |
| 2287 | } |
| 2288 | |
| 2289 | static const struct nfs4_xdr_opaque_ops layoutstat_ops = { |
| 2290 | .encode = ff_layout_encode_layoutstats, |
| 2291 | .free = ff_layout_free_layoutstats, |
| 2292 | }; |
| 2293 | |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2294 | static int |
Trond Myklebust | 08e2e5b | 2016-09-29 09:15:27 -0700 | [diff] [blame] | 2295 | ff_layout_mirror_prepare_stats(struct pnfs_layout_hdr *lo, |
| 2296 | struct nfs42_layoutstat_devinfo *devinfo, |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2297 | int dev_limit) |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2298 | { |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2299 | struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo); |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2300 | struct nfs4_ff_layout_mirror *mirror; |
| 2301 | struct nfs4_deviceid_node *dev; |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2302 | int i = 0; |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2303 | |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2304 | list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) { |
| 2305 | if (i >= dev_limit) |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2306 | break; |
Fred Isaman | 65990d1 | 2016-09-30 14:37:41 -0400 | [diff] [blame] | 2307 | if (IS_ERR_OR_NULL(mirror->mirror_ds)) |
Peng Tao | 27c4306 | 2015-06-23 19:52:02 +0800 | [diff] [blame] | 2308 | continue; |
Trond Myklebust | 06946c6 | 2016-11-25 13:17:15 -0500 | [diff] [blame] | 2309 | if (!test_and_clear_bit(NFS4_FF_MIRROR_STAT_AVAIL, &mirror->flags)) |
| 2310 | continue; |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2311 | /* mirror refcount put in cleanup_layoutstats */ |
| 2312 | if (!atomic_inc_not_zero(&mirror->ref)) |
| 2313 | continue; |
| 2314 | dev = &mirror->mirror_ds->id_node; |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2315 | memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE); |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2316 | devinfo->offset = 0; |
| 2317 | devinfo->length = NFS4_MAX_UINT64; |
Trond Myklebust | 2f8220c | 2016-10-03 18:13:20 -0400 | [diff] [blame] | 2318 | spin_lock(&mirror->lock); |
Peng Tao | d099d7b | 2015-08-10 16:47:32 +0800 | [diff] [blame] | 2319 | devinfo->read_count = mirror->read_stat.io_stat.ops_completed; |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2320 | devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed; |
Peng Tao | d099d7b | 2015-08-10 16:47:32 +0800 | [diff] [blame] | 2321 | devinfo->write_count = mirror->write_stat.io_stat.ops_completed; |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2322 | devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed; |
Trond Myklebust | 2f8220c | 2016-10-03 18:13:20 -0400 | [diff] [blame] | 2323 | spin_unlock(&mirror->lock); |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2324 | devinfo->layout_type = LAYOUT_FLEX_FILES; |
Trond Myklebust | 422c93c | 2016-10-06 17:53:20 -0400 | [diff] [blame] | 2325 | devinfo->ld_private.ops = &layoutstat_ops; |
| 2326 | devinfo->ld_private.data = mirror; |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2327 | |
Trond Myklebust | 08e2e5b | 2016-09-29 09:15:27 -0700 | [diff] [blame] | 2328 | devinfo++; |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2329 | i++; |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2330 | } |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2331 | return i; |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | static int |
| 2335 | ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args) |
| 2336 | { |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2337 | struct nfs4_flexfile_layout *ff_layout; |
Trond Myklebust | 08e2e5b | 2016-09-29 09:15:27 -0700 | [diff] [blame] | 2338 | const int dev_count = PNFS_LAYOUTSTATS_MAXDEV; |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2339 | |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2340 | /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */ |
Trond Myklebust | 266d12d | 2015-08-24 20:03:17 -0400 | [diff] [blame] | 2341 | args->devinfo = kmalloc_array(dev_count, sizeof(*args->devinfo), GFP_NOIO); |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2342 | if (!args->devinfo) |
| 2343 | return -ENOMEM; |
| 2344 | |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2345 | spin_lock(&args->inode->i_lock); |
Trond Myklebust | 08e2e5b | 2016-09-29 09:15:27 -0700 | [diff] [blame] | 2346 | ff_layout = FF_LAYOUT_FROM_HDR(NFS_I(args->inode)->layout); |
| 2347 | args->num_dev = ff_layout_mirror_prepare_stats(&ff_layout->generic_hdr, |
| 2348 | &args->devinfo[0], dev_count); |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2349 | spin_unlock(&args->inode->i_lock); |
Trond Myklebust | 46c98c6 | 2016-11-25 13:24:09 -0500 | [diff] [blame] | 2350 | if (!args->num_dev) { |
| 2351 | kfree(args->devinfo); |
| 2352 | args->devinfo = NULL; |
| 2353 | return -ENOENT; |
| 2354 | } |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2355 | |
| 2356 | return 0; |
| 2357 | } |
| 2358 | |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2359 | static struct pnfs_layoutdriver_type flexfilelayout_type = { |
| 2360 | .id = LAYOUT_FLEX_FILES, |
| 2361 | .name = "LAYOUT_FLEX_FILES", |
| 2362 | .owner = THIS_MODULE, |
| 2363 | .alloc_layout_hdr = ff_layout_alloc_layout_hdr, |
| 2364 | .free_layout_hdr = ff_layout_free_layout_hdr, |
| 2365 | .alloc_lseg = ff_layout_alloc_lseg, |
| 2366 | .free_lseg = ff_layout_free_lseg, |
Trond Myklebust | 0762ed2 | 2015-08-25 17:38:25 -0400 | [diff] [blame] | 2367 | .add_lseg = ff_layout_add_lseg, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2368 | .pg_read_ops = &ff_layout_pg_read_ops, |
| 2369 | .pg_write_ops = &ff_layout_pg_write_ops, |
| 2370 | .get_ds_info = ff_layout_get_ds_info, |
Trond Myklebust | fc87701 | 2015-03-09 17:25:14 -0400 | [diff] [blame] | 2371 | .free_deviceid_node = ff_layout_free_deviceid_node, |
Tom Haynes | 338d00c | 2015-02-17 14:58:15 -0800 | [diff] [blame] | 2372 | .mark_request_commit = pnfs_layout_mark_request_commit, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2373 | .clear_request_commit = pnfs_generic_clear_request_commit, |
| 2374 | .scan_commit_lists = pnfs_generic_scan_commit_lists, |
| 2375 | .recover_commit_reqs = pnfs_generic_recover_commit_reqs, |
| 2376 | .commit_pagelist = ff_layout_commit_pagelist, |
| 2377 | .read_pagelist = ff_layout_read_pagelist, |
| 2378 | .write_pagelist = ff_layout_write_pagelist, |
| 2379 | .alloc_deviceid_node = ff_layout_alloc_deviceid_node, |
Trond Myklebust | 5b9b3c8 | 2016-12-02 16:15:05 -0500 | [diff] [blame] | 2380 | .prepare_layoutreturn = ff_layout_prepare_layoutreturn, |
Trond Myklebust | 5bb89b4 | 2015-03-25 14:14:42 -0400 | [diff] [blame] | 2381 | .sync = pnfs_nfs_generic_sync, |
Peng Tao | ad4dc53e | 2015-06-23 19:52:01 +0800 | [diff] [blame] | 2382 | .prepare_layoutstats = ff_layout_prepare_layoutstats, |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2383 | }; |
| 2384 | |
| 2385 | static int __init nfs4flexfilelayout_init(void) |
| 2386 | { |
| 2387 | printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n", |
| 2388 | __func__); |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 2389 | if (!ff_zero_group) { |
| 2390 | ff_zero_group = groups_alloc(0); |
| 2391 | if (!ff_zero_group) |
| 2392 | return -ENOMEM; |
| 2393 | } |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2394 | return pnfs_register_layoutdriver(&flexfilelayout_type); |
| 2395 | } |
| 2396 | |
| 2397 | static void __exit nfs4flexfilelayout_exit(void) |
| 2398 | { |
| 2399 | printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n", |
| 2400 | __func__); |
| 2401 | pnfs_unregister_layoutdriver(&flexfilelayout_type); |
Jeff Layton | 90a0be0 | 2016-04-21 20:51:59 -0400 | [diff] [blame] | 2402 | if (ff_zero_group) { |
| 2403 | put_group_info(ff_zero_group); |
| 2404 | ff_zero_group = NULL; |
| 2405 | } |
Tom Haynes | d67ae82 | 2014-12-11 17:02:04 -0500 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | MODULE_ALIAS("nfs-layouttype4-4"); |
| 2409 | |
| 2410 | MODULE_LICENSE("GPL"); |
| 2411 | MODULE_DESCRIPTION("The NFSv4 flexfile layout driver"); |
| 2412 | |
| 2413 | module_init(nfs4flexfilelayout_init); |
| 2414 | module_exit(nfs4flexfilelayout_exit); |