blob: b6e0e32b60537f4c7d2cb4af8a06720cb9fa59e1 [file] [log] [blame]
Tom Haynesd67ae822014-12-11 17:02:04 -05001/*
2 * Module for pnfs flexfile layout driver.
3 *
4 * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
5 *
6 * Tao Peng <bergwolf@primarydata.com>
7 */
8
9#include <linux/nfs_fs.h>
10#include <linux/nfs_page.h>
11#include <linux/module.h>
12
13#include <linux/sunrpc/metrics.h>
Tom Haynesd67ae822014-12-11 17:02:04 -050014
15#include "flexfilelayout.h"
16#include "../nfs4session.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040017#include "../nfs4idmap.h"
Tom Haynesd67ae822014-12-11 17:02:04 -050018#include "../internal.h"
19#include "../delegation.h"
20#include "../nfs4trace.h"
21#include "../iostat.h"
22#include "../nfs.h"
Peng Taoad4dc53e2015-06-23 19:52:01 +080023#include "../nfs42.h"
Tom Haynesd67ae822014-12-11 17:02:04 -050024
25#define NFSDBG_FACILITY NFSDBG_PNFS_LD
26
27#define FF_LAYOUT_POLL_RETRY_MAX (15*HZ)
28
Jeff Layton90a0be02016-04-21 20:51:59 -040029static struct group_info *ff_zero_group;
30
Tom Haynesd67ae822014-12-11 17:02:04 -050031static struct pnfs_layout_hdr *
32ff_layout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
33{
34 struct nfs4_flexfile_layout *ffl;
35
36 ffl = kzalloc(sizeof(*ffl), gfp_flags);
37 if (ffl) {
38 INIT_LIST_HEAD(&ffl->error_list);
Trond Myklebust266d12d2015-08-24 20:03:17 -040039 INIT_LIST_HEAD(&ffl->mirrors);
Tom Haynesd67ae822014-12-11 17:02:04 -050040 return &ffl->generic_hdr;
41 } else
42 return NULL;
43}
44
45static void
46ff_layout_free_layout_hdr(struct pnfs_layout_hdr *lo)
47{
48 struct nfs4_ff_layout_ds_err *err, *n;
49
50 list_for_each_entry_safe(err, n, &FF_LAYOUT_FROM_HDR(lo)->error_list,
51 list) {
52 list_del(&err->list);
53 kfree(err);
54 }
55 kfree(FF_LAYOUT_FROM_HDR(lo));
56}
57
Trond Myklebust93b717f2016-05-16 17:42:43 -040058static int decode_pnfs_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
Tom Haynesd67ae822014-12-11 17:02:04 -050059{
60 __be32 *p;
61
62 p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE);
63 if (unlikely(p == NULL))
64 return -ENOBUFS;
Trond Myklebust93b717f2016-05-16 17:42:43 -040065 stateid->type = NFS4_PNFS_DS_STATEID_TYPE;
66 memcpy(stateid->data, p, NFS4_STATEID_SIZE);
Tom Haynesd67ae822014-12-11 17:02:04 -050067 dprintk("%s: stateid id= [%x%x%x%x]\n", __func__,
68 p[0], p[1], p[2], p[3]);
69 return 0;
70}
71
72static int decode_deviceid(struct xdr_stream *xdr, struct nfs4_deviceid *devid)
73{
74 __be32 *p;
75
76 p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE);
77 if (unlikely(!p))
78 return -ENOBUFS;
79 memcpy(devid, p, NFS4_DEVICEID4_SIZE);
80 nfs4_print_deviceid(devid);
81 return 0;
82}
83
84static int decode_nfs_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
85{
86 __be32 *p;
87
88 p = xdr_inline_decode(xdr, 4);
89 if (unlikely(!p))
90 return -ENOBUFS;
91 fh->size = be32_to_cpup(p++);
92 if (fh->size > sizeof(struct nfs_fh)) {
93 printk(KERN_ERR "NFS flexfiles: Too big fh received %d\n",
94 fh->size);
95 return -EOVERFLOW;
96 }
97 /* fh.data */
98 p = xdr_inline_decode(xdr, fh->size);
99 if (unlikely(!p))
100 return -ENOBUFS;
101 memcpy(&fh->data, p, fh->size);
102 dprintk("%s: fh len %d\n", __func__, fh->size);
103
104 return 0;
105}
106
107/*
108 * Currently only stringified uids and gids are accepted.
109 * I.e., kerberos is not supported to the DSes, so no pricipals.
110 *
111 * That means that one common function will suffice, but when
112 * principals are added, this should be split to accomodate
113 * calls to both nfs_map_name_to_uid() and nfs_map_group_to_gid().
114 */
115static int
116decode_name(struct xdr_stream *xdr, u32 *id)
117{
118 __be32 *p;
119 int len;
120
121 /* opaque_length(4)*/
122 p = xdr_inline_decode(xdr, 4);
123 if (unlikely(!p))
124 return -ENOBUFS;
125 len = be32_to_cpup(p++);
126 if (len < 0)
127 return -EINVAL;
128
129 dprintk("%s: len %u\n", __func__, len);
130
131 /* opaque body */
132 p = xdr_inline_decode(xdr, len);
133 if (unlikely(!p))
134 return -ENOBUFS;
135
136 if (!nfs_map_string_to_numeric((char *)p, len, id))
137 return -EINVAL;
138
139 return 0;
140}
141
Trond Myklebust266d12d2015-08-24 20:03:17 -0400142static bool ff_mirror_match_fh(const struct nfs4_ff_layout_mirror *m1,
143 const struct nfs4_ff_layout_mirror *m2)
144{
145 int i, j;
146
147 if (m1->fh_versions_cnt != m2->fh_versions_cnt)
148 return false;
149 for (i = 0; i < m1->fh_versions_cnt; i++) {
150 bool found_fh = false;
Trond Myklebust86fb4492015-12-30 10:57:01 -0500151 for (j = 0; j < m2->fh_versions_cnt; j++) {
Trond Myklebust266d12d2015-08-24 20:03:17 -0400152 if (nfs_compare_fh(&m1->fh_versions[i],
153 &m2->fh_versions[j]) == 0) {
154 found_fh = true;
155 break;
156 }
157 }
158 if (!found_fh)
159 return false;
160 }
161 return true;
162}
163
164static struct nfs4_ff_layout_mirror *
165ff_layout_add_mirror(struct pnfs_layout_hdr *lo,
166 struct nfs4_ff_layout_mirror *mirror)
167{
168 struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
169 struct nfs4_ff_layout_mirror *pos;
170 struct inode *inode = lo->plh_inode;
171
172 spin_lock(&inode->i_lock);
173 list_for_each_entry(pos, &ff_layout->mirrors, mirrors) {
174 if (mirror->mirror_ds != pos->mirror_ds)
175 continue;
176 if (!ff_mirror_match_fh(mirror, pos))
177 continue;
178 if (atomic_inc_not_zero(&pos->ref)) {
179 spin_unlock(&inode->i_lock);
180 return pos;
181 }
182 }
183 list_add(&mirror->mirrors, &ff_layout->mirrors);
184 mirror->layout = lo;
185 spin_unlock(&inode->i_lock);
186 return mirror;
187}
188
kbuild test robote3b1df22015-08-25 11:19:25 +0800189static void
Trond Myklebust266d12d2015-08-24 20:03:17 -0400190ff_layout_remove_mirror(struct nfs4_ff_layout_mirror *mirror)
191{
192 struct inode *inode;
193 if (mirror->layout == NULL)
194 return;
195 inode = mirror->layout->plh_inode;
196 spin_lock(&inode->i_lock);
197 list_del(&mirror->mirrors);
198 spin_unlock(&inode->i_lock);
199 mirror->layout = NULL;
200}
201
Trond Myklebust28a0d722015-08-24 18:08:30 -0400202static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(gfp_t gfp_flags)
203{
204 struct nfs4_ff_layout_mirror *mirror;
205
206 mirror = kzalloc(sizeof(*mirror), gfp_flags);
207 if (mirror != NULL) {
208 spin_lock_init(&mirror->lock);
209 atomic_set(&mirror->ref, 1);
Trond Myklebust266d12d2015-08-24 20:03:17 -0400210 INIT_LIST_HEAD(&mirror->mirrors);
Trond Myklebust28a0d722015-08-24 18:08:30 -0400211 }
212 return mirror;
213}
214
215static void ff_layout_free_mirror(struct nfs4_ff_layout_mirror *mirror)
216{
Jeff Layton3064b682016-04-21 20:52:00 -0400217 struct rpc_cred *cred;
218
Trond Myklebust266d12d2015-08-24 20:03:17 -0400219 ff_layout_remove_mirror(mirror);
Trond Myklebust28a0d722015-08-24 18:08:30 -0400220 kfree(mirror->fh_versions);
Jeff Layton3064b682016-04-21 20:52:00 -0400221 cred = rcu_access_pointer(mirror->ro_cred);
222 if (cred)
223 put_rpccred(cred);
224 cred = rcu_access_pointer(mirror->rw_cred);
225 if (cred)
226 put_rpccred(cred);
Trond Myklebust28a0d722015-08-24 18:08:30 -0400227 nfs4_ff_layout_put_deviceid(mirror->mirror_ds);
228 kfree(mirror);
229}
230
231static void ff_layout_put_mirror(struct nfs4_ff_layout_mirror *mirror)
232{
233 if (mirror != NULL && atomic_dec_and_test(&mirror->ref))
234 ff_layout_free_mirror(mirror);
235}
236
Tom Haynesd67ae822014-12-11 17:02:04 -0500237static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment *fls)
238{
239 int i;
240
241 if (fls->mirror_array) {
242 for (i = 0; i < fls->mirror_array_cnt; i++) {
243 /* normally mirror_ds is freed in
244 * .free_deviceid_node but we still do it here
245 * for .alloc_lseg error path */
Trond Myklebust28a0d722015-08-24 18:08:30 -0400246 ff_layout_put_mirror(fls->mirror_array[i]);
Tom Haynesd67ae822014-12-11 17:02:04 -0500247 }
248 kfree(fls->mirror_array);
249 fls->mirror_array = NULL;
250 }
251}
252
253static int ff_layout_check_layout(struct nfs4_layoutget_res *lgr)
254{
255 int ret = 0;
256
257 dprintk("--> %s\n", __func__);
258
259 /* FIXME: remove this check when layout segment support is added */
260 if (lgr->range.offset != 0 ||
261 lgr->range.length != NFS4_MAX_UINT64) {
262 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
263 __func__);
264 ret = -EINVAL;
265 }
266
267 dprintk("--> %s returns %d\n", __func__, ret);
268 return ret;
269}
270
271static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment *fls)
272{
273 if (fls) {
274 ff_layout_free_mirror_array(fls);
275 kfree(fls);
276 }
277}
278
Trond Myklebust0762ed22015-08-25 17:38:25 -0400279static bool
280ff_lseg_range_is_after(const struct pnfs_layout_range *l1,
281 const struct pnfs_layout_range *l2)
282{
283 u64 end1, end2;
284
285 if (l1->iomode != l2->iomode)
286 return l1->iomode != IOMODE_READ;
287 end1 = pnfs_calc_offset_end(l1->offset, l1->length);
288 end2 = pnfs_calc_offset_end(l2->offset, l2->length);
289 if (end1 < l2->offset)
290 return false;
291 if (end2 < l1->offset)
292 return true;
293 return l2->offset <= l1->offset;
294}
295
296static bool
297ff_lseg_merge(struct pnfs_layout_segment *new,
298 struct pnfs_layout_segment *old)
299{
300 u64 new_end, old_end;
301
302 if (new->pls_range.iomode != old->pls_range.iomode)
303 return false;
304 old_end = pnfs_calc_offset_end(old->pls_range.offset,
305 old->pls_range.length);
306 if (old_end < new->pls_range.offset)
307 return false;
308 new_end = pnfs_calc_offset_end(new->pls_range.offset,
309 new->pls_range.length);
310 if (new_end < old->pls_range.offset)
311 return false;
312
313 /* Mergeable: copy info from 'old' to 'new' */
314 if (new_end < old_end)
315 new_end = old_end;
316 if (new->pls_range.offset < old->pls_range.offset)
317 new->pls_range.offset = old->pls_range.offset;
318 new->pls_range.length = pnfs_calc_offset_length(new->pls_range.offset,
319 new_end);
320 if (test_bit(NFS_LSEG_ROC, &old->pls_flags))
321 set_bit(NFS_LSEG_ROC, &new->pls_flags);
322 if (test_bit(NFS_LSEG_LAYOUTRETURN, &old->pls_flags))
323 set_bit(NFS_LSEG_LAYOUTRETURN, &new->pls_flags);
324 return true;
325}
326
327static void
328ff_layout_add_lseg(struct pnfs_layout_hdr *lo,
329 struct pnfs_layout_segment *lseg,
330 struct list_head *free_me)
331{
332 pnfs_generic_layout_insert_lseg(lo, lseg,
333 ff_lseg_range_is_after,
334 ff_lseg_merge,
335 free_me);
336}
337
Tom Haynesd67ae822014-12-11 17:02:04 -0500338static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment *fls)
339{
Tom Haynesd67ae822014-12-11 17:02:04 -0500340 int i, j;
341
342 for (i = 0; i < fls->mirror_array_cnt - 1; i++) {
343 for (j = i + 1; j < fls->mirror_array_cnt; j++)
344 if (fls->mirror_array[i]->efficiency <
Fabian Frederick455b6ee2015-06-12 18:58:50 +0200345 fls->mirror_array[j]->efficiency)
346 swap(fls->mirror_array[i],
347 fls->mirror_array[j]);
Tom Haynesd67ae822014-12-11 17:02:04 -0500348 }
349}
350
Trond Myklebust260074c2015-11-02 09:59:00 -0500351static void ff_layout_mark_devices_valid(struct nfs4_ff_layout_segment *fls)
352{
353 struct nfs4_deviceid_node *node;
354 int i;
355
356 if (!(fls->flags & FF_FLAGS_NO_IO_THRU_MDS))
357 return;
358 for (i = 0; i < fls->mirror_array_cnt; i++) {
359 node = &fls->mirror_array[i]->mirror_ds->id_node;
360 clear_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags);
361 }
362}
363
Tom Haynesd67ae822014-12-11 17:02:04 -0500364static struct pnfs_layout_segment *
365ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh,
366 struct nfs4_layoutget_res *lgr,
367 gfp_t gfp_flags)
368{
369 struct pnfs_layout_segment *ret;
370 struct nfs4_ff_layout_segment *fls = NULL;
371 struct xdr_stream stream;
372 struct xdr_buf buf;
373 struct page *scratch;
374 u64 stripe_unit;
375 u32 mirror_array_cnt;
376 __be32 *p;
377 int i, rc;
378
379 dprintk("--> %s\n", __func__);
380 scratch = alloc_page(gfp_flags);
381 if (!scratch)
382 return ERR_PTR(-ENOMEM);
383
384 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages,
385 lgr->layoutp->len);
386 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
387
388 /* stripe unit and mirror_array_cnt */
389 rc = -EIO;
390 p = xdr_inline_decode(&stream, 8 + 4);
391 if (!p)
392 goto out_err_free;
393
394 p = xdr_decode_hyper(p, &stripe_unit);
395 mirror_array_cnt = be32_to_cpup(p++);
396 dprintk("%s: stripe_unit=%llu mirror_array_cnt=%u\n", __func__,
397 stripe_unit, mirror_array_cnt);
398
399 if (mirror_array_cnt > NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT ||
400 mirror_array_cnt == 0)
401 goto out_err_free;
402
403 rc = -ENOMEM;
404 fls = kzalloc(sizeof(*fls), gfp_flags);
405 if (!fls)
406 goto out_err_free;
407
408 fls->mirror_array_cnt = mirror_array_cnt;
409 fls->stripe_unit = stripe_unit;
410 fls->mirror_array = kcalloc(fls->mirror_array_cnt,
411 sizeof(fls->mirror_array[0]), gfp_flags);
412 if (fls->mirror_array == NULL)
413 goto out_err_free;
414
415 for (i = 0; i < fls->mirror_array_cnt; i++) {
Trond Myklebust266d12d2015-08-24 20:03:17 -0400416 struct nfs4_ff_layout_mirror *mirror;
Tom Haynesd67ae822014-12-11 17:02:04 -0500417 struct nfs4_deviceid devid;
418 struct nfs4_deviceid_node *idnode;
Jeff Layton90a0be02016-04-21 20:51:59 -0400419 struct auth_cred acred = { .group_info = ff_zero_group };
Jeff Layton3064b682016-04-21 20:52:00 -0400420 struct rpc_cred __rcu *cred;
Jeff Layton90a0be02016-04-21 20:51:59 -0400421 u32 ds_count, fh_count, id;
Tom Haynesd67ae822014-12-11 17:02:04 -0500422 int j;
423
424 rc = -EIO;
425 p = xdr_inline_decode(&stream, 4);
426 if (!p)
427 goto out_err_free;
428 ds_count = be32_to_cpup(p);
429
430 /* FIXME: allow for striping? */
431 if (ds_count != 1)
432 goto out_err_free;
433
Trond Myklebust28a0d722015-08-24 18:08:30 -0400434 fls->mirror_array[i] = ff_layout_alloc_mirror(gfp_flags);
Tom Haynesd67ae822014-12-11 17:02:04 -0500435 if (fls->mirror_array[i] == NULL) {
436 rc = -ENOMEM;
437 goto out_err_free;
438 }
439
Tom Haynesd67ae822014-12-11 17:02:04 -0500440 fls->mirror_array[i]->ds_count = ds_count;
441
442 /* deviceid */
443 rc = decode_deviceid(&stream, &devid);
444 if (rc)
445 goto out_err_free;
446
447 idnode = nfs4_find_get_deviceid(NFS_SERVER(lh->plh_inode),
448 &devid, lh->plh_lc_cred,
449 gfp_flags);
450 /*
451 * upon success, mirror_ds is allocated by previous
452 * getdeviceinfo, or newly by .alloc_deviceid_node
453 * nfs4_find_get_deviceid failure is indeed getdeviceinfo falure
454 */
455 if (idnode)
456 fls->mirror_array[i]->mirror_ds =
457 FF_LAYOUT_MIRROR_DS(idnode);
458 else
459 goto out_err_free;
460
461 /* efficiency */
462 rc = -EIO;
463 p = xdr_inline_decode(&stream, 4);
464 if (!p)
465 goto out_err_free;
466 fls->mirror_array[i]->efficiency = be32_to_cpup(p);
467
468 /* stateid */
Trond Myklebust93b717f2016-05-16 17:42:43 -0400469 rc = decode_pnfs_stateid(&stream, &fls->mirror_array[i]->stateid);
Tom Haynesd67ae822014-12-11 17:02:04 -0500470 if (rc)
471 goto out_err_free;
472
473 /* fh */
474 p = xdr_inline_decode(&stream, 4);
475 if (!p)
476 goto out_err_free;
477 fh_count = be32_to_cpup(p);
478
479 fls->mirror_array[i]->fh_versions =
480 kzalloc(fh_count * sizeof(struct nfs_fh),
481 gfp_flags);
482 if (fls->mirror_array[i]->fh_versions == NULL) {
483 rc = -ENOMEM;
484 goto out_err_free;
485 }
486
487 for (j = 0; j < fh_count; j++) {
488 rc = decode_nfs_fh(&stream,
489 &fls->mirror_array[i]->fh_versions[j]);
490 if (rc)
491 goto out_err_free;
492 }
493
494 fls->mirror_array[i]->fh_versions_cnt = fh_count;
495
496 /* user */
Jeff Layton90a0be02016-04-21 20:51:59 -0400497 rc = decode_name(&stream, &id);
Tom Haynesd67ae822014-12-11 17:02:04 -0500498 if (rc)
499 goto out_err_free;
500
Jeff Layton90a0be02016-04-21 20:51:59 -0400501 acred.uid = make_kuid(&init_user_ns, id);
502
Tom Haynesd67ae822014-12-11 17:02:04 -0500503 /* group */
Jeff Layton90a0be02016-04-21 20:51:59 -0400504 rc = decode_name(&stream, &id);
Tom Haynesd67ae822014-12-11 17:02:04 -0500505 if (rc)
506 goto out_err_free;
507
Jeff Layton90a0be02016-04-21 20:51:59 -0400508 acred.gid = make_kgid(&init_user_ns, id);
509
510 /* find the cred for it */
Jeff Layton3064b682016-04-21 20:52:00 -0400511 rcu_assign_pointer(cred, rpc_lookup_generic_cred(&acred, 0, gfp_flags));
Jeff Layton90a0be02016-04-21 20:51:59 -0400512 if (IS_ERR(cred)) {
513 rc = PTR_ERR(cred);
514 goto out_err_free;
515 }
516
Jeff Layton3064b682016-04-21 20:52:00 -0400517 if (lgr->range.iomode == IOMODE_READ)
518 rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred);
519 else
520 rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred);
Jeff Layton90a0be02016-04-21 20:51:59 -0400521
Trond Myklebust266d12d2015-08-24 20:03:17 -0400522 mirror = ff_layout_add_mirror(lh, fls->mirror_array[i]);
523 if (mirror != fls->mirror_array[i]) {
Jeff Layton90a0be02016-04-21 20:51:59 -0400524 /* swap cred ptrs so free_mirror will clean up old */
Jeff Layton3064b682016-04-21 20:52:00 -0400525 if (lgr->range.iomode == IOMODE_READ) {
526 cred = xchg(&mirror->ro_cred, cred);
527 rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred);
528 } else {
529 cred = xchg(&mirror->rw_cred, cred);
530 rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred);
531 }
Trond Myklebust266d12d2015-08-24 20:03:17 -0400532 ff_layout_free_mirror(fls->mirror_array[i]);
533 fls->mirror_array[i] = mirror;
534 }
535
Jeff Layton3064b682016-04-21 20:52:00 -0400536 dprintk("%s: iomode %s uid %u gid %u\n", __func__,
537 lgr->range.iomode == IOMODE_READ ? "READ" : "RW",
Jeff Layton90a0be02016-04-21 20:51:59 -0400538 from_kuid(&init_user_ns, acred.uid),
539 from_kgid(&init_user_ns, acred.gid));
Tom Haynesd67ae822014-12-11 17:02:04 -0500540 }
541
Trond Myklebustc0f5f502015-06-26 14:51:32 -0400542 p = xdr_inline_decode(&stream, 4);
Trond Myklebustd0379a52015-11-16 11:26:07 -0500543 if (!p)
544 goto out_sort_mirrors;
545 fls->flags = be32_to_cpup(p);
Trond Myklebustc0f5f502015-06-26 14:51:32 -0400546
Trond Myklebustd0379a52015-11-16 11:26:07 -0500547 p = xdr_inline_decode(&stream, 4);
548 if (!p)
549 goto out_sort_mirrors;
550 for (i=0; i < fls->mirror_array_cnt; i++)
551 fls->mirror_array[i]->report_interval = be32_to_cpup(p);
552
553out_sort_mirrors:
Tom Haynesd67ae822014-12-11 17:02:04 -0500554 ff_layout_sort_mirrors(fls);
555 rc = ff_layout_check_layout(lgr);
556 if (rc)
557 goto out_err_free;
Trond Myklebust260074c2015-11-02 09:59:00 -0500558 ff_layout_mark_devices_valid(fls);
Tom Haynesd67ae822014-12-11 17:02:04 -0500559
560 ret = &fls->generic_hdr;
561 dprintk("<-- %s (success)\n", __func__);
562out_free_page:
563 __free_page(scratch);
564 return ret;
565out_err_free:
566 _ff_layout_free_lseg(fls);
567 ret = ERR_PTR(rc);
568 dprintk("<-- %s (%d)\n", __func__, rc);
569 goto out_free_page;
570}
571
572static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout)
573{
574 struct pnfs_layout_segment *lseg;
575
576 list_for_each_entry(lseg, &layout->plh_segs, pls_list)
577 if (lseg->pls_range.iomode == IOMODE_RW)
578 return true;
579
580 return false;
581}
582
583static void
584ff_layout_free_lseg(struct pnfs_layout_segment *lseg)
585{
586 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
Tom Haynesd67ae822014-12-11 17:02:04 -0500587
588 dprintk("--> %s\n", __func__);
589
Tom Haynesd67ae822014-12-11 17:02:04 -0500590 if (lseg->pls_range.iomode == IOMODE_RW) {
591 struct nfs4_flexfile_layout *ffl;
592 struct inode *inode;
593
594 ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout);
595 inode = ffl->generic_hdr.plh_inode;
596 spin_lock(&inode->i_lock);
597 if (!ff_layout_has_rw_segments(lseg->pls_layout)) {
598 ffl->commit_info.nbuckets = 0;
599 kfree(ffl->commit_info.buckets);
600 ffl->commit_info.buckets = NULL;
601 }
602 spin_unlock(&inode->i_lock);
603 }
604 _ff_layout_free_lseg(fls);
605}
606
607/* Return 1 until we have multiple lsegs support */
608static int
609ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls)
610{
611 return 1;
612}
613
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800614static void
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500615nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800616{
Peng Tao9bbd9bb2015-06-26 09:45:49 +0800617 /* first IO request? */
618 if (atomic_inc_return(&timer->n_ops) == 1) {
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500619 timer->start_time = now;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800620 }
621}
622
623static ktime_t
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500624nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800625{
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500626 ktime_t start;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800627
Peng Tao9bbd9bb2015-06-26 09:45:49 +0800628 if (atomic_dec_return(&timer->n_ops) < 0)
629 WARN_ON_ONCE(1);
630
Peng Tao9bbd9bb2015-06-26 09:45:49 +0800631 start = timer->start_time;
632 timer->start_time = now;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800633 return ktime_sub(now, start);
634}
635
Peng Tao97ba3752015-06-23 19:52:04 +0800636static bool
Peng Taod9838032015-06-23 19:52:00 +0800637nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500638 struct nfs4_ff_layoutstat *layoutstat,
639 ktime_t now)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800640{
Peng Taod9838032015-06-23 19:52:00 +0800641 static const ktime_t notime = {0};
Trond Myklebustbbf58bf2015-08-24 20:39:18 -0400642 s64 report_interval = FF_LAYOUTSTATS_REPORT_INTERVAL;
Peng Taod9838032015-06-23 19:52:00 +0800643
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500644 nfs4_ff_start_busy_timer(&layoutstat->busy_timer, now);
Peng Tao9bbd9bb2015-06-26 09:45:49 +0800645 if (ktime_equal(mirror->start_time, notime))
646 mirror->start_time = now;
647 if (ktime_equal(mirror->last_report_time, notime))
648 mirror->last_report_time = now;
Trond Myklebustd0379a52015-11-16 11:26:07 -0500649 if (mirror->report_interval != 0)
650 report_interval = (s64)mirror->report_interval * 1000LL;
651 else if (layoutstats_timer != 0)
Trond Myklebustbbf58bf2015-08-24 20:39:18 -0400652 report_interval = (s64)layoutstats_timer * 1000LL;
Peng Tao97ba3752015-06-23 19:52:04 +0800653 if (ktime_to_ms(ktime_sub(now, mirror->last_report_time)) >=
Trond Myklebustbbf58bf2015-08-24 20:39:18 -0400654 report_interval) {
Peng Tao97ba3752015-06-23 19:52:04 +0800655 mirror->last_report_time = now;
656 return true;
657 }
658
659 return false;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800660}
661
662static void
663nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat,
664 __u64 requested)
665{
666 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
667
668 iostat->ops_requested++;
669 iostat->bytes_requested += requested;
670}
671
672static void
673nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat,
674 __u64 requested,
675 __u64 completed,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500676 ktime_t time_completed,
677 ktime_t time_started)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800678{
679 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500680 ktime_t completion_time = ktime_sub(time_completed, time_started);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800681 ktime_t timer;
682
683 iostat->ops_completed++;
684 iostat->bytes_completed += completed;
685 iostat->bytes_not_delivered += requested - completed;
686
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500687 timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer, time_completed);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800688 iostat->total_busy_time =
689 ktime_add(iostat->total_busy_time, timer);
690 iostat->aggregate_completion_time =
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500691 ktime_add(iostat->aggregate_completion_time,
692 completion_time);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800693}
694
695static void
Trond Myklebust0b7baf92015-08-24 18:22:28 -0400696nfs4_ff_layout_stat_io_start_read(struct inode *inode,
697 struct nfs4_ff_layout_mirror *mirror,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500698 __u64 requested, ktime_t now)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800699{
Peng Tao97ba3752015-06-23 19:52:04 +0800700 bool report;
701
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800702 spin_lock(&mirror->lock);
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500703 report = nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat, now);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800704 nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested);
705 spin_unlock(&mirror->lock);
Peng Tao97ba3752015-06-23 19:52:04 +0800706
707 if (report)
Trond Myklebust0b7baf92015-08-24 18:22:28 -0400708 pnfs_report_layoutstat(inode, GFP_KERNEL);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800709}
710
711static void
712nfs4_ff_layout_stat_io_end_read(struct rpc_task *task,
713 struct nfs4_ff_layout_mirror *mirror,
714 __u64 requested,
715 __u64 completed)
716{
717 spin_lock(&mirror->lock);
718 nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat,
719 requested, completed,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500720 ktime_get(), task->tk_start);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800721 spin_unlock(&mirror->lock);
722}
723
724static void
Trond Myklebust0b7baf92015-08-24 18:22:28 -0400725nfs4_ff_layout_stat_io_start_write(struct inode *inode,
726 struct nfs4_ff_layout_mirror *mirror,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500727 __u64 requested, ktime_t now)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800728{
Peng Tao97ba3752015-06-23 19:52:04 +0800729 bool report;
730
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800731 spin_lock(&mirror->lock);
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500732 report = nfs4_ff_layoutstat_start_io(mirror , &mirror->write_stat, now);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800733 nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested);
734 spin_unlock(&mirror->lock);
Peng Tao97ba3752015-06-23 19:52:04 +0800735
736 if (report)
Trond Myklebust0b7baf92015-08-24 18:22:28 -0400737 pnfs_report_layoutstat(inode, GFP_NOIO);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800738}
739
740static void
741nfs4_ff_layout_stat_io_end_write(struct rpc_task *task,
742 struct nfs4_ff_layout_mirror *mirror,
743 __u64 requested,
744 __u64 completed,
745 enum nfs3_stable_how committed)
746{
747 if (committed == NFS_UNSTABLE)
748 requested = completed = 0;
749
750 spin_lock(&mirror->lock);
751 nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500752 requested, completed, ktime_get(), task->tk_start);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800753 spin_unlock(&mirror->lock);
754}
755
Tom Haynesd67ae822014-12-11 17:02:04 -0500756static int
757ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg,
758 struct nfs_commit_info *cinfo,
759 gfp_t gfp_flags)
760{
761 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
762 struct pnfs_commit_bucket *buckets;
763 int size;
764
765 if (cinfo->ds->nbuckets != 0) {
766 /* This assumes there is only one RW lseg per file.
767 * To support multiple lseg per file, we need to
768 * change struct pnfs_commit_bucket to allow dynamic
769 * increasing nbuckets.
770 */
771 return 0;
772 }
773
774 size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg);
775
776 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
777 gfp_flags);
778 if (!buckets)
779 return -ENOMEM;
780 else {
781 int i;
782
Dave Wysochanskife238e62016-04-01 13:45:09 -0400783 spin_lock(&cinfo->inode->i_lock);
Tom Haynesd67ae822014-12-11 17:02:04 -0500784 if (cinfo->ds->nbuckets != 0)
785 kfree(buckets);
786 else {
787 cinfo->ds->buckets = buckets;
788 cinfo->ds->nbuckets = size;
789 for (i = 0; i < size; i++) {
790 INIT_LIST_HEAD(&buckets[i].written);
791 INIT_LIST_HEAD(&buckets[i].committing);
792 /* mark direct verifier as unset */
793 buckets[i].direct_verf.committed =
794 NFS_INVALID_STABLE_HOW;
795 }
796 }
Dave Wysochanskife238e62016-04-01 13:45:09 -0400797 spin_unlock(&cinfo->inode->i_lock);
Tom Haynesd67ae822014-12-11 17:02:04 -0500798 return 0;
799 }
800}
801
802static struct nfs4_pnfs_ds *
Trond Myklebust13544412015-11-02 10:11:17 -0500803ff_layout_choose_best_ds_for_read(struct pnfs_layout_segment *lseg,
804 int start_idx,
Tom Haynesd67ae822014-12-11 17:02:04 -0500805 int *best_idx)
806{
Trond Myklebust13544412015-11-02 10:11:17 -0500807 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
Tom Haynesd67ae822014-12-11 17:02:04 -0500808 struct nfs4_pnfs_ds *ds;
809 int idx;
810
Tom Haynesd67ae822014-12-11 17:02:04 -0500811 /* mirrors are sorted by efficiency */
Trond Myklebust13544412015-11-02 10:11:17 -0500812 for (idx = start_idx; idx < fls->mirror_array_cnt; idx++) {
813 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
Tom Haynesd67ae822014-12-11 17:02:04 -0500814 if (ds) {
815 *best_idx = idx;
816 return ds;
817 }
818 }
819
820 return NULL;
821}
822
823static void
824ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
825 struct nfs_page *req)
826{
827 struct nfs_pgio_mirror *pgm;
828 struct nfs4_ff_layout_mirror *mirror;
829 struct nfs4_pnfs_ds *ds;
830 int ds_idx;
831
832 /* Use full layout for now */
Peng Taod600ad12015-12-04 02:57:48 +0800833 if (!pgio->pg_lseg) {
Tom Haynesd67ae822014-12-11 17:02:04 -0500834 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
835 req->wb_context,
836 0,
837 NFS4_MAX_UINT64,
838 IOMODE_READ,
839 GFP_KERNEL);
Peng Taod600ad12015-12-04 02:57:48 +0800840 if (IS_ERR(pgio->pg_lseg)) {
841 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
842 pgio->pg_lseg = NULL;
843 return;
844 }
845 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500846 /* If no lseg, fall back to read through mds */
847 if (pgio->pg_lseg == NULL)
848 goto out_mds;
849
Trond Myklebust13544412015-11-02 10:11:17 -0500850 ds = ff_layout_choose_best_ds_for_read(pgio->pg_lseg, 0, &ds_idx);
Tom Haynesd67ae822014-12-11 17:02:04 -0500851 if (!ds)
852 goto out_mds;
853 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx);
854
855 pgio->pg_mirror_idx = ds_idx;
856
857 /* read always uses only one mirror - idx 0 for pgio layer */
858 pgm = &pgio->pg_mirrors[0];
859 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize;
860
861 return;
862out_mds:
863 pnfs_put_lseg(pgio->pg_lseg);
864 pgio->pg_lseg = NULL;
865 nfs_pageio_reset_read_mds(pgio);
866}
867
868static void
869ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
870 struct nfs_page *req)
871{
872 struct nfs4_ff_layout_mirror *mirror;
873 struct nfs_pgio_mirror *pgm;
874 struct nfs_commit_info cinfo;
875 struct nfs4_pnfs_ds *ds;
876 int i;
877 int status;
878
Peng Taod600ad12015-12-04 02:57:48 +0800879 if (!pgio->pg_lseg) {
Tom Haynesd67ae822014-12-11 17:02:04 -0500880 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
881 req->wb_context,
882 0,
883 NFS4_MAX_UINT64,
884 IOMODE_RW,
885 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +0800886 if (IS_ERR(pgio->pg_lseg)) {
887 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
888 pgio->pg_lseg = NULL;
889 return;
890 }
891 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500892 /* If no lseg, fall back to write through mds */
893 if (pgio->pg_lseg == NULL)
894 goto out_mds;
895
896 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
897 status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
898 if (status < 0)
899 goto out_mds;
900
901 /* Use a direct mapping of ds_idx to pgio mirror_idx */
902 if (WARN_ON_ONCE(pgio->pg_mirror_count !=
903 FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg)))
904 goto out_mds;
905
906 for (i = 0; i < pgio->pg_mirror_count; i++) {
907 ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true);
908 if (!ds)
909 goto out_mds;
910 pgm = &pgio->pg_mirrors[i];
911 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i);
912 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize;
913 }
914
915 return;
916
917out_mds:
918 pnfs_put_lseg(pgio->pg_lseg);
919 pgio->pg_lseg = NULL;
920 nfs_pageio_reset_write_mds(pgio);
921}
922
923static unsigned int
924ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
925 struct nfs_page *req)
926{
Peng Taod600ad12015-12-04 02:57:48 +0800927 if (!pgio->pg_lseg) {
Tom Haynesd67ae822014-12-11 17:02:04 -0500928 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
929 req->wb_context,
930 0,
931 NFS4_MAX_UINT64,
932 IOMODE_RW,
933 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +0800934 if (IS_ERR(pgio->pg_lseg)) {
935 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
936 pgio->pg_lseg = NULL;
937 goto out;
938 }
939 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500940 if (pgio->pg_lseg)
941 return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
942
943 /* no lseg means that pnfs is not in use, so no mirroring here */
Tom Haynesd67ae822014-12-11 17:02:04 -0500944 nfs_pageio_reset_write_mds(pgio);
Peng Taod600ad12015-12-04 02:57:48 +0800945out:
Tom Haynesd67ae822014-12-11 17:02:04 -0500946 return 1;
947}
948
949static const struct nfs_pageio_ops ff_layout_pg_read_ops = {
950 .pg_init = ff_layout_pg_init_read,
951 .pg_test = pnfs_generic_pg_test,
952 .pg_doio = pnfs_generic_pg_readpages,
953 .pg_cleanup = pnfs_generic_pg_cleanup,
954};
955
956static const struct nfs_pageio_ops ff_layout_pg_write_ops = {
957 .pg_init = ff_layout_pg_init_write,
958 .pg_test = pnfs_generic_pg_test,
959 .pg_doio = pnfs_generic_pg_writepages,
960 .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write,
961 .pg_cleanup = pnfs_generic_pg_cleanup,
962};
963
964static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs)
965{
966 struct rpc_task *task = &hdr->task;
967
968 pnfs_layoutcommit_inode(hdr->inode, false);
969
970 if (retry_pnfs) {
971 dprintk("%s Reset task %5u for i/o through pNFS "
972 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
973 hdr->task.tk_pid,
974 hdr->inode->i_sb->s_id,
975 (unsigned long long)NFS_FILEID(hdr->inode),
976 hdr->args.count,
977 (unsigned long long)hdr->args.offset);
978
Trond Myklebustdc602dd2015-12-31 11:44:06 -0500979 hdr->completion_ops->reschedule_io(hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -0500980 return;
981 }
982
983 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
984 dprintk("%s Reset task %5u for i/o through MDS "
985 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
986 hdr->task.tk_pid,
987 hdr->inode->i_sb->s_id,
988 (unsigned long long)NFS_FILEID(hdr->inode),
989 hdr->args.count,
990 (unsigned long long)hdr->args.offset);
991
992 task->tk_status = pnfs_write_done_resend_to_mds(hdr);
993 }
994}
995
996static void ff_layout_reset_read(struct nfs_pgio_header *hdr)
997{
998 struct rpc_task *task = &hdr->task;
999
1000 pnfs_layoutcommit_inode(hdr->inode, false);
1001
1002 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1003 dprintk("%s Reset task %5u for i/o through MDS "
1004 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1005 hdr->task.tk_pid,
1006 hdr->inode->i_sb->s_id,
1007 (unsigned long long)NFS_FILEID(hdr->inode),
1008 hdr->args.count,
1009 (unsigned long long)hdr->args.offset);
1010
1011 task->tk_status = pnfs_read_done_resend_to_mds(hdr);
1012 }
1013}
1014
1015static int ff_layout_async_handle_error_v4(struct rpc_task *task,
1016 struct nfs4_state *state,
1017 struct nfs_client *clp,
1018 struct pnfs_layout_segment *lseg,
1019 int idx)
1020{
1021 struct pnfs_layout_hdr *lo = lseg->pls_layout;
1022 struct inode *inode = lo->plh_inode;
1023 struct nfs_server *mds_server = NFS_SERVER(inode);
1024
1025 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1026 struct nfs_client *mds_client = mds_server->nfs_client;
1027 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
1028
1029 if (task->tk_status >= 0)
1030 return 0;
1031
1032 switch (task->tk_status) {
1033 /* MDS state errors */
1034 case -NFS4ERR_DELEG_REVOKED:
1035 case -NFS4ERR_ADMIN_REVOKED:
1036 case -NFS4ERR_BAD_STATEID:
1037 if (state == NULL)
1038 break;
1039 nfs_remove_bad_delegation(state->inode);
1040 case -NFS4ERR_OPENMODE:
1041 if (state == NULL)
1042 break;
1043 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
1044 goto out_bad_stateid;
1045 goto wait_on_recovery;
1046 case -NFS4ERR_EXPIRED:
1047 if (state != NULL) {
1048 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
1049 goto out_bad_stateid;
1050 }
1051 nfs4_schedule_lease_recovery(mds_client);
1052 goto wait_on_recovery;
1053 /* DS session errors */
1054 case -NFS4ERR_BADSESSION:
1055 case -NFS4ERR_BADSLOT:
1056 case -NFS4ERR_BAD_HIGH_SLOT:
1057 case -NFS4ERR_DEADSESSION:
1058 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1059 case -NFS4ERR_SEQ_FALSE_RETRY:
1060 case -NFS4ERR_SEQ_MISORDERED:
1061 dprintk("%s ERROR %d, Reset session. Exchangeid "
1062 "flags 0x%x\n", __func__, task->tk_status,
1063 clp->cl_exchange_flags);
1064 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
1065 break;
1066 case -NFS4ERR_DELAY:
1067 case -NFS4ERR_GRACE:
1068 rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX);
1069 break;
1070 case -NFS4ERR_RETRY_UNCACHED_REP:
1071 break;
1072 /* Invalidate Layout errors */
1073 case -NFS4ERR_PNFS_NO_LAYOUT:
1074 case -ESTALE: /* mapped NFS4ERR_STALE */
1075 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
1076 case -EISDIR: /* mapped NFS4ERR_ISDIR */
1077 case -NFS4ERR_FHEXPIRED:
1078 case -NFS4ERR_WRONG_TYPE:
1079 dprintk("%s Invalid layout error %d\n", __func__,
1080 task->tk_status);
1081 /*
1082 * Destroy layout so new i/o will get a new layout.
1083 * Layout will not be destroyed until all current lseg
1084 * references are put. Mark layout as invalid to resend failed
1085 * i/o and all i/o waiting on the slot table to the MDS until
1086 * layout is destroyed and a new valid layout is obtained.
1087 */
1088 pnfs_destroy_layout(NFS_I(inode));
1089 rpc_wake_up(&tbl->slot_tbl_waitq);
1090 goto reset;
1091 /* RPC connection errors */
1092 case -ECONNREFUSED:
1093 case -EHOSTDOWN:
1094 case -EHOSTUNREACH:
1095 case -ENETUNREACH:
1096 case -EIO:
1097 case -ETIMEDOUT:
1098 case -EPIPE:
1099 dprintk("%s DS connection error %d\n", __func__,
1100 task->tk_status);
1101 nfs4_mark_deviceid_unavailable(devid);
1102 rpc_wake_up(&tbl->slot_tbl_waitq);
1103 /* fall through */
1104 default:
Trond Myklebust260074c2015-11-02 09:59:00 -05001105 if (ff_layout_no_fallback_to_mds(lseg) ||
1106 ff_layout_has_available_ds(lseg))
Tom Haynesd67ae822014-12-11 17:02:04 -05001107 return -NFS4ERR_RESET_TO_PNFS;
1108reset:
1109 dprintk("%s Retry through MDS. Error %d\n", __func__,
1110 task->tk_status);
1111 return -NFS4ERR_RESET_TO_MDS;
1112 }
1113out:
1114 task->tk_status = 0;
1115 return -EAGAIN;
1116out_bad_stateid:
1117 task->tk_status = -EIO;
1118 return 0;
1119wait_on_recovery:
1120 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
1121 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
1122 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
1123 goto out;
1124}
1125
1126/* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
1127static int ff_layout_async_handle_error_v3(struct rpc_task *task,
1128 struct pnfs_layout_segment *lseg,
1129 int idx)
1130{
1131 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1132
1133 if (task->tk_status >= 0)
1134 return 0;
1135
Trond Myklebust90816d12015-08-30 09:53:06 -07001136 switch (task->tk_status) {
1137 /* File access problems. Don't mark the device as unavailable */
1138 case -EACCES:
1139 case -ESTALE:
1140 case -EISDIR:
1141 case -EBADHANDLE:
1142 case -ELOOP:
1143 case -ENOSPC:
1144 break;
1145 case -EJUKEBOX:
1146 nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY);
1147 goto out_retry;
1148 default:
Tom Haynesd67ae822014-12-11 17:02:04 -05001149 dprintk("%s DS connection error %d\n", __func__,
1150 task->tk_status);
1151 nfs4_mark_deviceid_unavailable(devid);
Tom Haynesd67ae822014-12-11 17:02:04 -05001152 }
Trond Myklebust90816d12015-08-30 09:53:06 -07001153 /* FIXME: Need to prevent infinite looping here. */
1154 return -NFS4ERR_RESET_TO_PNFS;
1155out_retry:
Tom Haynesd67ae822014-12-11 17:02:04 -05001156 task->tk_status = 0;
Trond Myklebust7eeea162015-12-17 09:22:15 -05001157 rpc_restart_call_prepare(task);
Tom Haynesd67ae822014-12-11 17:02:04 -05001158 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
1159 return -EAGAIN;
1160}
1161
1162static int ff_layout_async_handle_error(struct rpc_task *task,
1163 struct nfs4_state *state,
1164 struct nfs_client *clp,
1165 struct pnfs_layout_segment *lseg,
1166 int idx)
1167{
1168 int vers = clp->cl_nfs_mod->rpc_vers->number;
1169
1170 switch (vers) {
1171 case 3:
1172 return ff_layout_async_handle_error_v3(task, lseg, idx);
1173 case 4:
1174 return ff_layout_async_handle_error_v4(task, state, clp,
1175 lseg, idx);
1176 default:
1177 /* should never happen */
1178 WARN_ON_ONCE(1);
1179 return 0;
1180 }
1181}
1182
1183static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
1184 int idx, u64 offset, u64 length,
Trond Myklebustdd521282015-08-20 17:59:49 -05001185 u32 status, int opnum, int error)
Tom Haynesd67ae822014-12-11 17:02:04 -05001186{
1187 struct nfs4_ff_layout_mirror *mirror;
1188 int err;
1189
Trond Myklebustdd521282015-08-20 17:59:49 -05001190 if (status == 0) {
1191 switch (error) {
1192 case -ETIMEDOUT:
1193 case -EPFNOSUPPORT:
1194 case -EPROTONOSUPPORT:
1195 case -EOPNOTSUPP:
1196 case -ECONNREFUSED:
1197 case -ECONNRESET:
1198 case -EHOSTDOWN:
1199 case -EHOSTUNREACH:
1200 case -ENETUNREACH:
1201 case -EADDRINUSE:
1202 case -ENOBUFS:
1203 case -EPIPE:
1204 case -EPERM:
1205 status = NFS4ERR_NXIO;
1206 break;
1207 case -EACCES:
1208 status = NFS4ERR_ACCESS;
1209 break;
1210 default:
1211 return;
1212 }
1213 }
1214
Peng Taoc22eeb82015-12-07 17:31:38 -05001215 switch (status) {
1216 case NFS4ERR_DELAY:
1217 case NFS4ERR_GRACE:
1218 return;
1219 default:
1220 break;
1221 }
1222
Tom Haynesd67ae822014-12-11 17:02:04 -05001223 mirror = FF_LAYOUT_COMP(lseg, idx);
1224 err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
1225 mirror, offset, length, status, opnum,
1226 GFP_NOIO);
Trond Myklebustf95c03b2015-09-02 15:15:11 -07001227 pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg);
Tom Haynesd67ae822014-12-11 17:02:04 -05001228 dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status);
1229}
1230
1231/* NFS_PROTO call done callback routines */
Tom Haynesd67ae822014-12-11 17:02:04 -05001232static int ff_layout_read_done_cb(struct rpc_task *task,
1233 struct nfs_pgio_header *hdr)
1234{
Tom Haynesd67ae822014-12-11 17:02:04 -05001235 int err;
1236
1237 trace_nfs4_pnfs_read(hdr, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001238 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001239 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1240 hdr->args.offset, hdr->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001241 hdr->res.op_status, OP_READ,
1242 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001243 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1244 hdr->ds_clp, hdr->lseg,
1245 hdr->pgio_mirror_idx);
1246
1247 switch (err) {
1248 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust13544412015-11-02 10:11:17 -05001249 if (ff_layout_choose_best_ds_for_read(hdr->lseg,
1250 hdr->pgio_mirror_idx + 1,
1251 &hdr->pgio_mirror_idx))
1252 goto out_eagain;
Trond Myklebust2370abd2016-01-27 20:32:50 -05001253 set_bit(NFS_LAYOUT_RETURN_REQUESTED,
Tom Haynesd67ae822014-12-11 17:02:04 -05001254 &hdr->lseg->pls_layout->plh_flags);
1255 pnfs_read_resend_pnfs(hdr);
1256 return task->tk_status;
1257 case -NFS4ERR_RESET_TO_MDS:
Tom Haynesd67ae822014-12-11 17:02:04 -05001258 ff_layout_reset_read(hdr);
1259 return task->tk_status;
1260 case -EAGAIN:
Trond Myklebust13544412015-11-02 10:11:17 -05001261 goto out_eagain;
Tom Haynesd67ae822014-12-11 17:02:04 -05001262 }
1263
1264 return 0;
Trond Myklebust13544412015-11-02 10:11:17 -05001265out_eagain:
1266 rpc_restart_call_prepare(task);
1267 return -EAGAIN;
Tom Haynesd67ae822014-12-11 17:02:04 -05001268}
1269
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001270static bool
1271ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg)
1272{
1273 return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT);
1274}
1275
Tom Haynesd67ae822014-12-11 17:02:04 -05001276/*
1277 * We reference the rpc_cred of the first WRITE that triggers the need for
1278 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
1279 * rfc5661 is not clear about which credential should be used.
1280 *
1281 * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
1282 * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
1283 * we always send layoutcommit after DS writes.
1284 */
1285static void
1286ff_layout_set_layoutcommit(struct nfs_pgio_header *hdr)
1287{
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001288 if (!ff_layout_need_layoutcommit(hdr->lseg))
1289 return;
1290
Trond Myklebust67af7612015-03-25 20:40:38 -04001291 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
1292 hdr->mds_offset + hdr->res.count);
Tom Haynesd67ae822014-12-11 17:02:04 -05001293 dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
1294 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
1295}
1296
1297static bool
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001298ff_layout_device_unavailable(struct pnfs_layout_segment *lseg, int idx)
Tom Haynesd67ae822014-12-11 17:02:04 -05001299{
1300 /* No mirroring for now */
1301 struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx);
1302
1303 return ff_layout_test_devid_unavailable(node);
1304}
1305
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001306static void ff_layout_read_record_layoutstats_start(struct rpc_task *task,
1307 struct nfs_pgio_header *hdr)
Tom Haynesd67ae822014-12-11 17:02:04 -05001308{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001309 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1310 return;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04001311 nfs4_ff_layout_stat_io_start_read(hdr->inode,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001312 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
Trond Myklebuste76d28d2015-08-20 13:12:51 -05001313 hdr->args.count,
1314 task->tk_start);
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001315}
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001316
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001317static void ff_layout_read_record_layoutstats_done(struct rpc_task *task,
1318 struct nfs_pgio_header *hdr)
1319{
1320 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1321 return;
1322 nfs4_ff_layout_stat_io_end_read(task,
1323 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1324 hdr->args.count,
1325 hdr->res.count);
1326}
1327
1328static int ff_layout_read_prepare_common(struct rpc_task *task,
1329 struct nfs_pgio_header *hdr)
1330{
Tom Haynesd67ae822014-12-11 17:02:04 -05001331 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1332 rpc_exit(task, -EIO);
1333 return -EIO;
1334 }
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001335 if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) {
1336 rpc_exit(task, -EHOSTDOWN);
Tom Haynesd67ae822014-12-11 17:02:04 -05001337 return -EAGAIN;
1338 }
Tom Haynesd67ae822014-12-11 17:02:04 -05001339
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001340 ff_layout_read_record_layoutstats_start(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001341 return 0;
1342}
1343
1344/*
1345 * Call ops for the async read/write cases
1346 * In the case of dense layouts, the offset needs to be reset to its
1347 * original value.
1348 */
1349static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data)
1350{
1351 struct nfs_pgio_header *hdr = data;
1352
1353 if (ff_layout_read_prepare_common(task, hdr))
1354 return;
1355
1356 rpc_call_start(task);
1357}
1358
1359static int ff_layout_setup_sequence(struct nfs_client *ds_clp,
1360 struct nfs4_sequence_args *args,
1361 struct nfs4_sequence_res *res,
1362 struct rpc_task *task)
1363{
1364 if (ds_clp->cl_session)
1365 return nfs41_setup_sequence(ds_clp->cl_session,
1366 args,
1367 res,
1368 task);
1369 return nfs40_setup_sequence(ds_clp->cl_slot_tbl,
1370 args,
1371 res,
1372 task);
1373}
1374
1375static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data)
1376{
1377 struct nfs_pgio_header *hdr = data;
1378
Tom Haynesd67ae822014-12-11 17:02:04 -05001379 if (ff_layout_setup_sequence(hdr->ds_clp,
1380 &hdr->args.seq_args,
1381 &hdr->res.seq_res,
1382 task))
1383 return;
1384
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001385 if (ff_layout_read_prepare_common(task, hdr))
1386 return;
1387
Tom Haynesd67ae822014-12-11 17:02:04 -05001388 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1389 hdr->args.lock_context, FMODE_READ) == -EIO)
1390 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1391}
1392
1393static void ff_layout_read_call_done(struct rpc_task *task, void *data)
1394{
1395 struct nfs_pgio_header *hdr = data;
1396
1397 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
1398
1399 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1400 task->tk_status == 0) {
1401 nfs4_sequence_done(task, &hdr->res.seq_res);
1402 return;
1403 }
1404
1405 /* Note this may cause RPC to be resent */
1406 hdr->mds_ops->rpc_call_done(task, hdr);
1407}
1408
1409static void ff_layout_read_count_stats(struct rpc_task *task, void *data)
1410{
1411 struct nfs_pgio_header *hdr = data;
1412
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001413 ff_layout_read_record_layoutstats_done(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001414 rpc_count_iostats_metrics(task,
1415 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]);
1416}
1417
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001418static void ff_layout_read_release(void *data)
1419{
1420 struct nfs_pgio_header *hdr = data;
1421
1422 ff_layout_read_record_layoutstats_done(&hdr->task, hdr);
1423 pnfs_generic_rw_release(data);
1424}
1425
1426
Tom Haynesd67ae822014-12-11 17:02:04 -05001427static int ff_layout_write_done_cb(struct rpc_task *task,
1428 struct nfs_pgio_header *hdr)
1429{
Tom Haynesd67ae822014-12-11 17:02:04 -05001430 int err;
1431
1432 trace_nfs4_pnfs_write(hdr, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001433 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001434 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1435 hdr->args.offset, hdr->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001436 hdr->res.op_status, OP_WRITE,
1437 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001438 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1439 hdr->ds_clp, hdr->lseg,
1440 hdr->pgio_mirror_idx);
1441
1442 switch (err) {
1443 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001444 ff_layout_reset_write(hdr, true);
1445 return task->tk_status;
Tom Haynesd67ae822014-12-11 17:02:04 -05001446 case -NFS4ERR_RESET_TO_MDS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001447 ff_layout_reset_write(hdr, false);
Tom Haynesd67ae822014-12-11 17:02:04 -05001448 return task->tk_status;
1449 case -EAGAIN:
Tom Haynesd67ae822014-12-11 17:02:04 -05001450 return -EAGAIN;
1451 }
1452
1453 if (hdr->res.verf->committed == NFS_FILE_SYNC ||
1454 hdr->res.verf->committed == NFS_DATA_SYNC)
1455 ff_layout_set_layoutcommit(hdr);
1456
Peng Tao54204012015-08-22 06:40:00 +08001457 /* zero out fattr since we don't care DS attr at all */
1458 hdr->fattr.valid = 0;
Peng Tao69f230d2015-08-20 01:52:59 +08001459 if (task->tk_status >= 0)
1460 nfs_writeback_update_inode(hdr);
1461
Tom Haynesd67ae822014-12-11 17:02:04 -05001462 return 0;
1463}
1464
1465static int ff_layout_commit_done_cb(struct rpc_task *task,
1466 struct nfs_commit_data *data)
1467{
Tom Haynesd67ae822014-12-11 17:02:04 -05001468 int err;
1469
1470 trace_nfs4_pnfs_commit_ds(data, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001471 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001472 ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index,
1473 data->args.offset, data->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001474 data->res.op_status, OP_COMMIT,
1475 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001476 err = ff_layout_async_handle_error(task, NULL, data->ds_clp,
1477 data->lseg, data->ds_commit_index);
1478
1479 switch (err) {
1480 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001481 pnfs_generic_prepare_to_resend_writes(data);
1482 return -EAGAIN;
Tom Haynesd67ae822014-12-11 17:02:04 -05001483 case -NFS4ERR_RESET_TO_MDS:
Tom Haynesd67ae822014-12-11 17:02:04 -05001484 pnfs_generic_prepare_to_resend_writes(data);
1485 return -EAGAIN;
1486 case -EAGAIN:
1487 rpc_restart_call_prepare(task);
1488 return -EAGAIN;
1489 }
1490
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001491 if (data->verf.committed == NFS_UNSTABLE
1492 && ff_layout_need_layoutcommit(data->lseg))
Trond Myklebust67af7612015-03-25 20:40:38 -04001493 pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb);
Tom Haynesd67ae822014-12-11 17:02:04 -05001494
1495 return 0;
1496}
1497
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001498static void ff_layout_write_record_layoutstats_start(struct rpc_task *task,
1499 struct nfs_pgio_header *hdr)
Tom Haynesd67ae822014-12-11 17:02:04 -05001500{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001501 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1502 return;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04001503 nfs4_ff_layout_stat_io_start_write(hdr->inode,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001504 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
Trond Myklebuste76d28d2015-08-20 13:12:51 -05001505 hdr->args.count,
1506 task->tk_start);
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001507}
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001508
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001509static void ff_layout_write_record_layoutstats_done(struct rpc_task *task,
1510 struct nfs_pgio_header *hdr)
1511{
1512 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1513 return;
1514 nfs4_ff_layout_stat_io_end_write(task,
1515 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1516 hdr->args.count, hdr->res.count,
1517 hdr->res.verf->committed);
1518}
1519
1520static int ff_layout_write_prepare_common(struct rpc_task *task,
1521 struct nfs_pgio_header *hdr)
1522{
Tom Haynesd67ae822014-12-11 17:02:04 -05001523 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1524 rpc_exit(task, -EIO);
1525 return -EIO;
1526 }
1527
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001528 if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) {
1529 rpc_exit(task, -EHOSTDOWN);
Tom Haynesd67ae822014-12-11 17:02:04 -05001530 return -EAGAIN;
1531 }
1532
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001533 ff_layout_write_record_layoutstats_start(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001534 return 0;
1535}
1536
1537static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data)
1538{
1539 struct nfs_pgio_header *hdr = data;
1540
1541 if (ff_layout_write_prepare_common(task, hdr))
1542 return;
1543
1544 rpc_call_start(task);
1545}
1546
1547static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data)
1548{
1549 struct nfs_pgio_header *hdr = data;
1550
Tom Haynesd67ae822014-12-11 17:02:04 -05001551 if (ff_layout_setup_sequence(hdr->ds_clp,
1552 &hdr->args.seq_args,
1553 &hdr->res.seq_res,
1554 task))
1555 return;
1556
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001557 if (ff_layout_write_prepare_common(task, hdr))
1558 return;
1559
Tom Haynesd67ae822014-12-11 17:02:04 -05001560 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1561 hdr->args.lock_context, FMODE_WRITE) == -EIO)
1562 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1563}
1564
1565static void ff_layout_write_call_done(struct rpc_task *task, void *data)
1566{
1567 struct nfs_pgio_header *hdr = data;
1568
1569 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1570 task->tk_status == 0) {
1571 nfs4_sequence_done(task, &hdr->res.seq_res);
1572 return;
1573 }
1574
1575 /* Note this may cause RPC to be resent */
1576 hdr->mds_ops->rpc_call_done(task, hdr);
1577}
1578
1579static void ff_layout_write_count_stats(struct rpc_task *task, void *data)
1580{
1581 struct nfs_pgio_header *hdr = data;
1582
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001583 ff_layout_write_record_layoutstats_done(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001584 rpc_count_iostats_metrics(task,
1585 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]);
1586}
1587
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001588static void ff_layout_write_release(void *data)
1589{
1590 struct nfs_pgio_header *hdr = data;
1591
1592 ff_layout_write_record_layoutstats_done(&hdr->task, hdr);
1593 pnfs_generic_rw_release(data);
1594}
1595
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001596static void ff_layout_commit_record_layoutstats_start(struct rpc_task *task,
1597 struct nfs_commit_data *cdata)
1598{
1599 if (test_and_set_bit(NFS_IOHDR_STAT, &cdata->flags))
1600 return;
1601 nfs4_ff_layout_stat_io_start_write(cdata->inode,
1602 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1603 0, task->tk_start);
1604}
1605
1606static void ff_layout_commit_record_layoutstats_done(struct rpc_task *task,
1607 struct nfs_commit_data *cdata)
1608{
1609 struct nfs_page *req;
1610 __u64 count = 0;
1611
1612 if (!test_and_clear_bit(NFS_IOHDR_STAT, &cdata->flags))
1613 return;
1614
1615 if (task->tk_status == 0) {
1616 list_for_each_entry(req, &cdata->pages, wb_list)
1617 count += req->wb_bytes;
1618 }
1619 nfs4_ff_layout_stat_io_end_write(task,
1620 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1621 count, count, NFS_FILE_SYNC);
1622}
1623
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001624static void ff_layout_commit_prepare_common(struct rpc_task *task,
1625 struct nfs_commit_data *cdata)
1626{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001627 ff_layout_commit_record_layoutstats_start(task, cdata);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001628}
1629
Tom Haynesd67ae822014-12-11 17:02:04 -05001630static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data)
1631{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001632 ff_layout_commit_prepare_common(task, data);
Tom Haynesd67ae822014-12-11 17:02:04 -05001633 rpc_call_start(task);
1634}
1635
1636static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data)
1637{
1638 struct nfs_commit_data *wdata = data;
1639
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001640 if (ff_layout_setup_sequence(wdata->ds_clp,
Tom Haynesd67ae822014-12-11 17:02:04 -05001641 &wdata->args.seq_args,
1642 &wdata->res.seq_res,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001643 task))
1644 return;
1645 ff_layout_commit_prepare_common(task, data);
1646}
1647
1648static void ff_layout_commit_done(struct rpc_task *task, void *data)
1649{
Peng Tao141b9b52015-12-07 18:33:14 -05001650 pnfs_generic_write_commit_done(task, data);
1651}
1652
1653static void ff_layout_commit_count_stats(struct rpc_task *task, void *data)
1654{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001655 struct nfs_commit_data *cdata = data;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001656
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001657 ff_layout_commit_record_layoutstats_done(task, cdata);
Tom Haynesd67ae822014-12-11 17:02:04 -05001658 rpc_count_iostats_metrics(task,
1659 &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]);
1660}
1661
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001662static void ff_layout_commit_release(void *data)
1663{
1664 struct nfs_commit_data *cdata = data;
1665
1666 ff_layout_commit_record_layoutstats_done(&cdata->task, cdata);
1667 pnfs_generic_commit_release(data);
1668}
1669
Tom Haynesd67ae822014-12-11 17:02:04 -05001670static const struct rpc_call_ops ff_layout_read_call_ops_v3 = {
1671 .rpc_call_prepare = ff_layout_read_prepare_v3,
1672 .rpc_call_done = ff_layout_read_call_done,
1673 .rpc_count_stats = ff_layout_read_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001674 .rpc_release = ff_layout_read_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001675};
1676
1677static const struct rpc_call_ops ff_layout_read_call_ops_v4 = {
1678 .rpc_call_prepare = ff_layout_read_prepare_v4,
1679 .rpc_call_done = ff_layout_read_call_done,
1680 .rpc_count_stats = ff_layout_read_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001681 .rpc_release = ff_layout_read_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001682};
1683
1684static const struct rpc_call_ops ff_layout_write_call_ops_v3 = {
1685 .rpc_call_prepare = ff_layout_write_prepare_v3,
1686 .rpc_call_done = ff_layout_write_call_done,
1687 .rpc_count_stats = ff_layout_write_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001688 .rpc_release = ff_layout_write_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001689};
1690
1691static const struct rpc_call_ops ff_layout_write_call_ops_v4 = {
1692 .rpc_call_prepare = ff_layout_write_prepare_v4,
1693 .rpc_call_done = ff_layout_write_call_done,
1694 .rpc_count_stats = ff_layout_write_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001695 .rpc_release = ff_layout_write_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001696};
1697
1698static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = {
1699 .rpc_call_prepare = ff_layout_commit_prepare_v3,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001700 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001701 .rpc_count_stats = ff_layout_commit_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001702 .rpc_release = ff_layout_commit_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001703};
1704
1705static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = {
1706 .rpc_call_prepare = ff_layout_commit_prepare_v4,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001707 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001708 .rpc_count_stats = ff_layout_commit_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001709 .rpc_release = ff_layout_commit_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001710};
1711
1712static enum pnfs_try_status
1713ff_layout_read_pagelist(struct nfs_pgio_header *hdr)
1714{
1715 struct pnfs_layout_segment *lseg = hdr->lseg;
1716 struct nfs4_pnfs_ds *ds;
1717 struct rpc_clnt *ds_clnt;
1718 struct rpc_cred *ds_cred;
1719 loff_t offset = hdr->args.offset;
1720 u32 idx = hdr->pgio_mirror_idx;
1721 int vers;
1722 struct nfs_fh *fh;
1723
1724 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
1725 __func__, hdr->inode->i_ino,
1726 hdr->args.pgbase, (size_t)hdr->args.count, offset);
1727
1728 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
1729 if (!ds)
1730 goto out_failed;
1731
1732 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1733 hdr->inode);
1734 if (IS_ERR(ds_clnt))
1735 goto out_failed;
1736
1737 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001738 if (!ds_cred)
Tom Haynesd67ae822014-12-11 17:02:04 -05001739 goto out_failed;
1740
1741 vers = nfs4_ff_layout_ds_version(lseg, idx);
1742
1743 dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__,
1744 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers);
1745
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001746 hdr->pgio_done_cb = ff_layout_read_done_cb;
Tom Haynesd67ae822014-12-11 17:02:04 -05001747 atomic_inc(&ds->ds_clp->cl_count);
1748 hdr->ds_clp = ds->ds_clp;
1749 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1750 if (fh)
1751 hdr->args.fh = fh;
Tom Haynesd67ae822014-12-11 17:02:04 -05001752 /*
1753 * Note that if we ever decide to split across DSes,
1754 * then we may need to handle dense-like offsets.
1755 */
1756 hdr->args.offset = offset;
1757 hdr->mds_offset = offset;
1758
1759 /* Perform an asynchronous read to ds */
1760 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1761 vers == 3 ? &ff_layout_read_call_ops_v3 :
1762 &ff_layout_read_call_ops_v4,
1763 0, RPC_TASK_SOFTCONN);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001764 put_rpccred(ds_cred);
Tom Haynesd67ae822014-12-11 17:02:04 -05001765 return PNFS_ATTEMPTED;
1766
1767out_failed:
1768 if (ff_layout_has_available_ds(lseg))
1769 return PNFS_TRY_AGAIN;
1770 return PNFS_NOT_ATTEMPTED;
1771}
1772
1773/* Perform async writes. */
1774static enum pnfs_try_status
1775ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
1776{
1777 struct pnfs_layout_segment *lseg = hdr->lseg;
1778 struct nfs4_pnfs_ds *ds;
1779 struct rpc_clnt *ds_clnt;
1780 struct rpc_cred *ds_cred;
1781 loff_t offset = hdr->args.offset;
1782 int vers;
1783 struct nfs_fh *fh;
1784 int idx = hdr->pgio_mirror_idx;
1785
1786 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1787 if (!ds)
1788 return PNFS_NOT_ATTEMPTED;
1789
1790 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1791 hdr->inode);
1792 if (IS_ERR(ds_clnt))
1793 return PNFS_NOT_ATTEMPTED;
1794
1795 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001796 if (!ds_cred)
Tom Haynesd67ae822014-12-11 17:02:04 -05001797 return PNFS_NOT_ATTEMPTED;
1798
1799 vers = nfs4_ff_layout_ds_version(lseg, idx);
1800
1801 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n",
1802 __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
1803 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count),
1804 vers);
1805
1806 hdr->pgio_done_cb = ff_layout_write_done_cb;
1807 atomic_inc(&ds->ds_clp->cl_count);
1808 hdr->ds_clp = ds->ds_clp;
1809 hdr->ds_commit_idx = idx;
1810 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1811 if (fh)
1812 hdr->args.fh = fh;
1813
1814 /*
1815 * Note that if we ever decide to split across DSes,
1816 * then we may need to handle dense-like offsets.
1817 */
1818 hdr->args.offset = offset;
1819
1820 /* Perform an asynchronous write */
1821 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1822 vers == 3 ? &ff_layout_write_call_ops_v3 :
1823 &ff_layout_write_call_ops_v4,
1824 sync, RPC_TASK_SOFTCONN);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001825 put_rpccred(ds_cred);
Tom Haynesd67ae822014-12-11 17:02:04 -05001826 return PNFS_ATTEMPTED;
1827}
1828
Tom Haynesd67ae822014-12-11 17:02:04 -05001829static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1830{
1831 return i;
1832}
1833
1834static struct nfs_fh *
1835select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1836{
1837 struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg);
1838
1839 /* FIXME: Assume that there is only one NFS version available
1840 * for the DS.
1841 */
1842 return &flseg->mirror_array[i]->fh_versions[0];
1843}
1844
1845static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how)
1846{
1847 struct pnfs_layout_segment *lseg = data->lseg;
1848 struct nfs4_pnfs_ds *ds;
1849 struct rpc_clnt *ds_clnt;
1850 struct rpc_cred *ds_cred;
1851 u32 idx;
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001852 int vers, ret;
Tom Haynesd67ae822014-12-11 17:02:04 -05001853 struct nfs_fh *fh;
1854
1855 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1856 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1857 if (!ds)
1858 goto out_err;
1859
1860 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1861 data->inode);
1862 if (IS_ERR(ds_clnt))
1863 goto out_err;
1864
1865 ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001866 if (!ds_cred)
Tom Haynesd67ae822014-12-11 17:02:04 -05001867 goto out_err;
1868
1869 vers = nfs4_ff_layout_ds_version(lseg, idx);
1870
1871 dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__,
1872 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count),
1873 vers);
1874 data->commit_done_cb = ff_layout_commit_done_cb;
1875 data->cred = ds_cred;
1876 atomic_inc(&ds->ds_clp->cl_count);
1877 data->ds_clp = ds->ds_clp;
1878 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1879 if (fh)
1880 data->args.fh = fh;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001881
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001882 ret = nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops,
Tom Haynesd67ae822014-12-11 17:02:04 -05001883 vers == 3 ? &ff_layout_commit_call_ops_v3 :
1884 &ff_layout_commit_call_ops_v4,
1885 how, RPC_TASK_SOFTCONN);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001886 put_rpccred(ds_cred);
1887 return ret;
Tom Haynesd67ae822014-12-11 17:02:04 -05001888out_err:
1889 pnfs_generic_prepare_to_resend_writes(data);
1890 pnfs_generic_commit_release(data);
1891 return -EAGAIN;
1892}
1893
1894static int
1895ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1896 int how, struct nfs_commit_info *cinfo)
1897{
1898 return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
1899 ff_layout_initiate_commit);
1900}
1901
1902static struct pnfs_ds_commit_info *
1903ff_layout_get_ds_info(struct inode *inode)
1904{
1905 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1906
1907 if (layout == NULL)
1908 return NULL;
1909
1910 return &FF_LAYOUT_FROM_HDR(layout)->commit_info;
1911}
1912
1913static void
Trond Myklebustfc877012015-03-09 17:25:14 -04001914ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d)
Tom Haynesd67ae822014-12-11 17:02:04 -05001915{
1916 nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds,
1917 id_node));
1918}
1919
1920static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo,
1921 struct xdr_stream *xdr,
1922 const struct nfs4_layoutreturn_args *args)
1923{
1924 struct pnfs_layout_hdr *hdr = &flo->generic_hdr;
1925 __be32 *start;
1926 int count = 0, ret = 0;
1927
1928 start = xdr_reserve_space(xdr, 4);
1929 if (unlikely(!start))
1930 return -E2BIG;
1931
1932 /* This assume we always return _ALL_ layouts */
1933 spin_lock(&hdr->plh_inode->i_lock);
1934 ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range);
1935 spin_unlock(&hdr->plh_inode->i_lock);
1936
1937 *start = cpu_to_be32(count);
1938
1939 return ret;
1940}
1941
1942/* report nothing for now */
1943static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo,
1944 struct xdr_stream *xdr,
1945 const struct nfs4_layoutreturn_args *args)
1946{
1947 __be32 *p;
1948
1949 p = xdr_reserve_space(xdr, 4);
1950 if (likely(p))
1951 *p = cpu_to_be32(0);
1952}
1953
1954static struct nfs4_deviceid_node *
1955ff_layout_alloc_deviceid_node(struct nfs_server *server,
1956 struct pnfs_device *pdev, gfp_t gfp_flags)
1957{
1958 struct nfs4_ff_layout_ds *dsaddr;
1959
1960 dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags);
1961 if (!dsaddr)
1962 return NULL;
1963 return &dsaddr->id_node;
1964}
1965
1966static void
1967ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo,
1968 struct xdr_stream *xdr,
1969 const struct nfs4_layoutreturn_args *args)
1970{
1971 struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
1972 __be32 *start;
1973
1974 dprintk("%s: Begin\n", __func__);
1975 start = xdr_reserve_space(xdr, 4);
1976 BUG_ON(!start);
1977
Trond Myklebust082fa372016-01-21 15:39:40 -05001978 ff_layout_encode_ioerr(flo, xdr, args);
Tom Haynesd67ae822014-12-11 17:02:04 -05001979 ff_layout_encode_iostats(flo, xdr, args);
Trond Myklebust082fa372016-01-21 15:39:40 -05001980
Tom Haynesd67ae822014-12-11 17:02:04 -05001981 *start = cpu_to_be32((xdr->p - start - 1) * 4);
1982 dprintk("%s: Return\n", __func__);
1983}
1984
Peng Tao27c43062015-06-23 19:52:02 +08001985static int
1986ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen)
1987{
1988 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
1989
1990 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
1991}
1992
1993static size_t
1994ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf,
1995 const int buflen)
1996{
1997 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
1998 const struct in6_addr *addr = &sin6->sin6_addr;
1999
2000 /*
2001 * RFC 4291, Section 2.2.2
2002 *
2003 * Shorthanded ANY address
2004 */
2005 if (ipv6_addr_any(addr))
2006 return snprintf(buf, buflen, "::");
2007
2008 /*
2009 * RFC 4291, Section 2.2.2
2010 *
2011 * Shorthanded loopback address
2012 */
2013 if (ipv6_addr_loopback(addr))
2014 return snprintf(buf, buflen, "::1");
2015
2016 /*
2017 * RFC 4291, Section 2.2.3
2018 *
2019 * Special presentation address format for mapped v4
2020 * addresses.
2021 */
2022 if (ipv6_addr_v4mapped(addr))
2023 return snprintf(buf, buflen, "::ffff:%pI4",
2024 &addr->s6_addr32[3]);
2025
2026 /*
2027 * RFC 4291, Section 2.2.1
2028 */
2029 return snprintf(buf, buflen, "%pI6c", addr);
2030}
2031
2032/* Derived from rpc_sockaddr2uaddr */
2033static void
2034ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da)
2035{
2036 struct sockaddr *sap = (struct sockaddr *)&da->da_addr;
2037 char portbuf[RPCBIND_MAXUADDRPLEN];
2038 char addrbuf[RPCBIND_MAXUADDRLEN];
2039 char *netid;
2040 unsigned short port;
2041 int len, netid_len;
2042 __be32 *p;
2043
2044 switch (sap->sa_family) {
2045 case AF_INET:
2046 if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
2047 return;
2048 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
2049 netid = "tcp";
2050 netid_len = 3;
2051 break;
2052 case AF_INET6:
2053 if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
2054 return;
2055 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
2056 netid = "tcp6";
2057 netid_len = 4;
2058 break;
2059 default:
2060 /* we only support tcp and tcp6 */
2061 WARN_ON_ONCE(1);
2062 return;
2063 }
2064
2065 snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff);
2066 len = strlcat(addrbuf, portbuf, sizeof(addrbuf));
2067
2068 p = xdr_reserve_space(xdr, 4 + netid_len);
2069 xdr_encode_opaque(p, netid, netid_len);
2070
2071 p = xdr_reserve_space(xdr, 4 + len);
2072 xdr_encode_opaque(p, addrbuf, len);
2073}
2074
2075static void
2076ff_layout_encode_nfstime(struct xdr_stream *xdr,
2077 ktime_t t)
2078{
2079 struct timespec64 ts;
2080 __be32 *p;
2081
2082 p = xdr_reserve_space(xdr, 12);
2083 ts = ktime_to_timespec64(t);
2084 p = xdr_encode_hyper(p, ts.tv_sec);
2085 *p++ = cpu_to_be32(ts.tv_nsec);
2086}
2087
2088static void
2089ff_layout_encode_io_latency(struct xdr_stream *xdr,
2090 struct nfs4_ff_io_stat *stat)
2091{
2092 __be32 *p;
2093
2094 p = xdr_reserve_space(xdr, 5 * 8);
2095 p = xdr_encode_hyper(p, stat->ops_requested);
2096 p = xdr_encode_hyper(p, stat->bytes_requested);
2097 p = xdr_encode_hyper(p, stat->ops_completed);
2098 p = xdr_encode_hyper(p, stat->bytes_completed);
2099 p = xdr_encode_hyper(p, stat->bytes_not_delivered);
2100 ff_layout_encode_nfstime(xdr, stat->total_busy_time);
2101 ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time);
2102}
2103
2104static void
2105ff_layout_encode_layoutstats(struct xdr_stream *xdr,
2106 struct nfs42_layoutstat_args *args,
2107 struct nfs42_layoutstat_devinfo *devinfo)
2108{
2109 struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private;
2110 struct nfs4_pnfs_ds_addr *da;
2111 struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds;
2112 struct nfs_fh *fh = &mirror->fh_versions[0];
2113 __be32 *p, *start;
2114
2115 da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node);
2116 dprintk("%s: DS %s: encoding address %s\n",
2117 __func__, ds->ds_remotestr, da->da_remotestr);
2118 /* layoutupdate length */
2119 start = xdr_reserve_space(xdr, 4);
2120 /* netaddr4 */
2121 ff_layout_encode_netaddr(xdr, da);
2122 /* nfs_fh4 */
2123 p = xdr_reserve_space(xdr, 4 + fh->size);
2124 xdr_encode_opaque(p, fh->data, fh->size);
2125 /* ff_io_latency4 read */
2126 spin_lock(&mirror->lock);
2127 ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat);
2128 /* ff_io_latency4 write */
2129 ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat);
2130 spin_unlock(&mirror->lock);
2131 /* nfstime4 */
2132 ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time));
2133 /* bool */
2134 p = xdr_reserve_space(xdr, 4);
2135 *p = cpu_to_be32(false);
2136
2137 *start = cpu_to_be32((xdr->p - start - 1) * 4);
2138}
2139
Trond Myklebust266d12d2015-08-24 20:03:17 -04002140static int
Peng Taoad4dc53e2015-06-23 19:52:01 +08002141ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args,
Trond Myklebust266d12d2015-08-24 20:03:17 -04002142 struct pnfs_layout_hdr *lo,
2143 int dev_limit)
Peng Taoad4dc53e2015-06-23 19:52:01 +08002144{
Trond Myklebust266d12d2015-08-24 20:03:17 -04002145 struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002146 struct nfs4_ff_layout_mirror *mirror;
2147 struct nfs4_deviceid_node *dev;
2148 struct nfs42_layoutstat_devinfo *devinfo;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002149 int i = 0;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002150
Trond Myklebust266d12d2015-08-24 20:03:17 -04002151 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2152 if (i >= dev_limit)
Peng Taoad4dc53e2015-06-23 19:52:01 +08002153 break;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002154 if (!mirror->mirror_ds)
Peng Tao27c43062015-06-23 19:52:02 +08002155 continue;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002156 /* mirror refcount put in cleanup_layoutstats */
2157 if (!atomic_inc_not_zero(&mirror->ref))
2158 continue;
2159 dev = &mirror->mirror_ds->id_node;
2160 devinfo = &args->devinfo[i];
Peng Taoad4dc53e2015-06-23 19:52:01 +08002161 memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002162 devinfo->offset = 0;
2163 devinfo->length = NFS4_MAX_UINT64;
Peng Taod099d7b2015-08-10 16:47:32 +08002164 devinfo->read_count = mirror->read_stat.io_stat.ops_completed;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002165 devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed;
Peng Taod099d7b2015-08-10 16:47:32 +08002166 devinfo->write_count = mirror->write_stat.io_stat.ops_completed;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002167 devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed;
2168 devinfo->layout_type = LAYOUT_FLEX_FILES;
Peng Tao27c43062015-06-23 19:52:02 +08002169 devinfo->layoutstats_encode = ff_layout_encode_layoutstats;
2170 devinfo->layout_private = mirror;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002171
Trond Myklebust266d12d2015-08-24 20:03:17 -04002172 i++;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002173 }
Trond Myklebust266d12d2015-08-24 20:03:17 -04002174 return i;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002175}
2176
2177static int
2178ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args)
2179{
Trond Myklebust266d12d2015-08-24 20:03:17 -04002180 struct nfs4_flexfile_layout *ff_layout;
2181 struct nfs4_ff_layout_mirror *mirror;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002182 int dev_count = 0;
2183
2184 spin_lock(&args->inode->i_lock);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002185 ff_layout = FF_LAYOUT_FROM_HDR(NFS_I(args->inode)->layout);
2186 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2187 if (atomic_read(&mirror->ref) != 0)
2188 dev_count ++;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002189 }
2190 spin_unlock(&args->inode->i_lock);
2191 /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
2192 if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) {
2193 dprintk("%s: truncating devinfo to limit (%d:%d)\n",
2194 __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV);
2195 dev_count = PNFS_LAYOUTSTATS_MAXDEV;
2196 }
Trond Myklebust266d12d2015-08-24 20:03:17 -04002197 args->devinfo = kmalloc_array(dev_count, sizeof(*args->devinfo), GFP_NOIO);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002198 if (!args->devinfo)
2199 return -ENOMEM;
2200
Peng Taoad4dc53e2015-06-23 19:52:01 +08002201 spin_lock(&args->inode->i_lock);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002202 args->num_dev = ff_layout_mirror_prepare_stats(args,
2203 &ff_layout->generic_hdr, dev_count);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002204 spin_unlock(&args->inode->i_lock);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002205
2206 return 0;
2207}
2208
Peng Tao27c43062015-06-23 19:52:02 +08002209static void
2210ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data)
2211{
2212 struct nfs4_ff_layout_mirror *mirror;
2213 int i;
2214
2215 for (i = 0; i < data->args.num_dev; i++) {
2216 mirror = data->args.devinfo[i].layout_private;
2217 data->args.devinfo[i].layout_private = NULL;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04002218 ff_layout_put_mirror(mirror);
Peng Tao27c43062015-06-23 19:52:02 +08002219 }
2220}
2221
Tom Haynesd67ae822014-12-11 17:02:04 -05002222static struct pnfs_layoutdriver_type flexfilelayout_type = {
2223 .id = LAYOUT_FLEX_FILES,
2224 .name = "LAYOUT_FLEX_FILES",
2225 .owner = THIS_MODULE,
2226 .alloc_layout_hdr = ff_layout_alloc_layout_hdr,
2227 .free_layout_hdr = ff_layout_free_layout_hdr,
2228 .alloc_lseg = ff_layout_alloc_lseg,
2229 .free_lseg = ff_layout_free_lseg,
Trond Myklebust0762ed22015-08-25 17:38:25 -04002230 .add_lseg = ff_layout_add_lseg,
Tom Haynesd67ae822014-12-11 17:02:04 -05002231 .pg_read_ops = &ff_layout_pg_read_ops,
2232 .pg_write_ops = &ff_layout_pg_write_ops,
2233 .get_ds_info = ff_layout_get_ds_info,
Trond Myklebustfc877012015-03-09 17:25:14 -04002234 .free_deviceid_node = ff_layout_free_deviceid_node,
Tom Haynes338d00c2015-02-17 14:58:15 -08002235 .mark_request_commit = pnfs_layout_mark_request_commit,
Tom Haynesd67ae822014-12-11 17:02:04 -05002236 .clear_request_commit = pnfs_generic_clear_request_commit,
2237 .scan_commit_lists = pnfs_generic_scan_commit_lists,
2238 .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
2239 .commit_pagelist = ff_layout_commit_pagelist,
2240 .read_pagelist = ff_layout_read_pagelist,
2241 .write_pagelist = ff_layout_write_pagelist,
2242 .alloc_deviceid_node = ff_layout_alloc_deviceid_node,
2243 .encode_layoutreturn = ff_layout_encode_layoutreturn,
Trond Myklebust5bb89b42015-03-25 14:14:42 -04002244 .sync = pnfs_nfs_generic_sync,
Peng Taoad4dc53e2015-06-23 19:52:01 +08002245 .prepare_layoutstats = ff_layout_prepare_layoutstats,
Peng Tao27c43062015-06-23 19:52:02 +08002246 .cleanup_layoutstats = ff_layout_cleanup_layoutstats,
Tom Haynesd67ae822014-12-11 17:02:04 -05002247};
2248
2249static int __init nfs4flexfilelayout_init(void)
2250{
2251 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n",
2252 __func__);
Jeff Layton90a0be02016-04-21 20:51:59 -04002253 if (!ff_zero_group) {
2254 ff_zero_group = groups_alloc(0);
2255 if (!ff_zero_group)
2256 return -ENOMEM;
2257 }
Tom Haynesd67ae822014-12-11 17:02:04 -05002258 return pnfs_register_layoutdriver(&flexfilelayout_type);
2259}
2260
2261static void __exit nfs4flexfilelayout_exit(void)
2262{
2263 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
2264 __func__);
2265 pnfs_unregister_layoutdriver(&flexfilelayout_type);
Jeff Layton90a0be02016-04-21 20:51:59 -04002266 if (ff_zero_group) {
2267 put_group_info(ff_zero_group);
2268 ff_zero_group = NULL;
2269 }
Tom Haynesd67ae822014-12-11 17:02:04 -05002270}
2271
2272MODULE_ALIAS("nfs-layouttype4-4");
2273
2274MODULE_LICENSE("GPL");
2275MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
2276
2277module_init(nfs4flexfilelayout_init);
2278module_exit(nfs4flexfilelayout_exit);