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