blob: e6206eaf2bdf34e4aa11d23dbf68b6e7c0c4684e [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
Jeff Laytonee26bdd2016-05-17 12:28:39 -0400302 if (test_bit(NFS_LSEG_LAYOUTRETURN, &old->pls_flags))
303 return false;
Trond Myklebust0762ed22015-08-25 17:38:25 -0400304 if (new->pls_range.iomode != old->pls_range.iomode)
305 return false;
306 old_end = pnfs_calc_offset_end(old->pls_range.offset,
307 old->pls_range.length);
308 if (old_end < new->pls_range.offset)
309 return false;
310 new_end = pnfs_calc_offset_end(new->pls_range.offset,
311 new->pls_range.length);
312 if (new_end < old->pls_range.offset)
313 return false;
314
315 /* Mergeable: copy info from 'old' to 'new' */
316 if (new_end < old_end)
317 new_end = old_end;
318 if (new->pls_range.offset < old->pls_range.offset)
319 new->pls_range.offset = old->pls_range.offset;
320 new->pls_range.length = pnfs_calc_offset_length(new->pls_range.offset,
321 new_end);
322 if (test_bit(NFS_LSEG_ROC, &old->pls_flags))
323 set_bit(NFS_LSEG_ROC, &new->pls_flags);
Trond Myklebust0762ed22015-08-25 17:38:25 -0400324 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
Tom Haynesc7d73af2016-05-25 07:31:14 -0700824ff_layout_pg_get_read(struct nfs_pageio_descriptor *pgio,
825 struct nfs_page *req,
826 bool strict_iomode)
827{
828retry_strict:
829 pnfs_put_lseg(pgio->pg_lseg);
830 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
831 req->wb_context,
832 0,
833 NFS4_MAX_UINT64,
834 IOMODE_READ,
835 strict_iomode,
836 GFP_KERNEL);
837 if (IS_ERR(pgio->pg_lseg)) {
838 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
839 pgio->pg_lseg = NULL;
840 }
841
842 /* If we don't have checking, do get a IOMODE_RW
843 * segment, and the server wants to avoid READs
844 * there, then retry!
845 */
846 if (pgio->pg_lseg && !strict_iomode &&
847 ff_layout_avoid_read_on_rw(pgio->pg_lseg)) {
848 strict_iomode = true;
849 goto retry_strict;
850 }
851}
852
853static void
Tom Haynesd67ae822014-12-11 17:02:04 -0500854ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
855 struct nfs_page *req)
856{
857 struct nfs_pgio_mirror *pgm;
858 struct nfs4_ff_layout_mirror *mirror;
859 struct nfs4_pnfs_ds *ds;
860 int ds_idx;
861
862 /* Use full layout for now */
Tom Haynesc7d73af2016-05-25 07:31:14 -0700863 if (!pgio->pg_lseg)
864 ff_layout_pg_get_read(pgio, req, false);
865 else if (ff_layout_avoid_read_on_rw(pgio->pg_lseg))
866 ff_layout_pg_get_read(pgio, req, true);
Tom Haynes602c4cd2016-05-25 07:31:13 -0700867
Tom Haynesd67ae822014-12-11 17:02:04 -0500868 /* If no lseg, fall back to read through mds */
869 if (pgio->pg_lseg == NULL)
870 goto out_mds;
871
Trond Myklebust13544412015-11-02 10:11:17 -0500872 ds = ff_layout_choose_best_ds_for_read(pgio->pg_lseg, 0, &ds_idx);
Tom Haynes446ca212016-05-17 12:28:38 -0400873 if (!ds) {
874 if (ff_layout_no_fallback_to_mds(pgio->pg_lseg))
875 goto out_pnfs;
876 else
877 goto out_mds;
878 }
879
Tom Haynesd67ae822014-12-11 17:02:04 -0500880 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx);
881
882 pgio->pg_mirror_idx = ds_idx;
883
884 /* read always uses only one mirror - idx 0 for pgio layer */
885 pgm = &pgio->pg_mirrors[0];
886 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize;
887
888 return;
889out_mds:
890 pnfs_put_lseg(pgio->pg_lseg);
891 pgio->pg_lseg = NULL;
892 nfs_pageio_reset_read_mds(pgio);
Tom Haynes446ca212016-05-17 12:28:38 -0400893 return;
894
895out_pnfs:
896 pnfs_set_lo_fail(pgio->pg_lseg);
897 pnfs_put_lseg(pgio->pg_lseg);
898 pgio->pg_lseg = NULL;
Tom Haynesd67ae822014-12-11 17:02:04 -0500899}
900
901static void
902ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
903 struct nfs_page *req)
904{
905 struct nfs4_ff_layout_mirror *mirror;
906 struct nfs_pgio_mirror *pgm;
907 struct nfs_commit_info cinfo;
908 struct nfs4_pnfs_ds *ds;
909 int i;
910 int status;
911
Peng Taod600ad12015-12-04 02:57:48 +0800912 if (!pgio->pg_lseg) {
Tom Haynesd67ae822014-12-11 17:02:04 -0500913 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
914 req->wb_context,
915 0,
916 NFS4_MAX_UINT64,
917 IOMODE_RW,
Tom Haynesc7d73af2016-05-25 07:31:14 -0700918 false,
Tom Haynesd67ae822014-12-11 17:02:04 -0500919 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +0800920 if (IS_ERR(pgio->pg_lseg)) {
921 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
922 pgio->pg_lseg = NULL;
923 return;
924 }
925 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500926 /* If no lseg, fall back to write through mds */
927 if (pgio->pg_lseg == NULL)
928 goto out_mds;
929
930 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
931 status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
932 if (status < 0)
933 goto out_mds;
934
935 /* Use a direct mapping of ds_idx to pgio mirror_idx */
936 if (WARN_ON_ONCE(pgio->pg_mirror_count !=
937 FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg)))
938 goto out_mds;
939
940 for (i = 0; i < pgio->pg_mirror_count; i++) {
941 ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true);
Tom Haynes446ca212016-05-17 12:28:38 -0400942 if (!ds) {
943 if (ff_layout_no_fallback_to_mds(pgio->pg_lseg))
944 goto out_pnfs;
945 else
946 goto out_mds;
947 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500948 pgm = &pgio->pg_mirrors[i];
949 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i);
950 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize;
951 }
952
953 return;
954
955out_mds:
956 pnfs_put_lseg(pgio->pg_lseg);
957 pgio->pg_lseg = NULL;
958 nfs_pageio_reset_write_mds(pgio);
Tom Haynes446ca212016-05-17 12:28:38 -0400959 return;
960
961out_pnfs:
962 pnfs_set_lo_fail(pgio->pg_lseg);
963 pnfs_put_lseg(pgio->pg_lseg);
964 pgio->pg_lseg = NULL;
Tom Haynesd67ae822014-12-11 17:02:04 -0500965}
966
967static unsigned int
968ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
969 struct nfs_page *req)
970{
Peng Taod600ad12015-12-04 02:57:48 +0800971 if (!pgio->pg_lseg) {
Tom Haynesd67ae822014-12-11 17:02:04 -0500972 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
973 req->wb_context,
974 0,
975 NFS4_MAX_UINT64,
976 IOMODE_RW,
Tom Haynesc7d73af2016-05-25 07:31:14 -0700977 false,
Tom Haynesd67ae822014-12-11 17:02:04 -0500978 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +0800979 if (IS_ERR(pgio->pg_lseg)) {
980 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
981 pgio->pg_lseg = NULL;
982 goto out;
983 }
984 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500985 if (pgio->pg_lseg)
986 return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
987
988 /* no lseg means that pnfs is not in use, so no mirroring here */
Tom Haynesd67ae822014-12-11 17:02:04 -0500989 nfs_pageio_reset_write_mds(pgio);
Peng Taod600ad12015-12-04 02:57:48 +0800990out:
Tom Haynesd67ae822014-12-11 17:02:04 -0500991 return 1;
992}
993
994static const struct nfs_pageio_ops ff_layout_pg_read_ops = {
995 .pg_init = ff_layout_pg_init_read,
996 .pg_test = pnfs_generic_pg_test,
997 .pg_doio = pnfs_generic_pg_readpages,
998 .pg_cleanup = pnfs_generic_pg_cleanup,
999};
1000
1001static const struct nfs_pageio_ops ff_layout_pg_write_ops = {
1002 .pg_init = ff_layout_pg_init_write,
1003 .pg_test = pnfs_generic_pg_test,
1004 .pg_doio = pnfs_generic_pg_writepages,
1005 .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write,
1006 .pg_cleanup = pnfs_generic_pg_cleanup,
1007};
1008
1009static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs)
1010{
1011 struct rpc_task *task = &hdr->task;
1012
1013 pnfs_layoutcommit_inode(hdr->inode, false);
1014
1015 if (retry_pnfs) {
1016 dprintk("%s Reset task %5u for i/o through pNFS "
1017 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1018 hdr->task.tk_pid,
1019 hdr->inode->i_sb->s_id,
1020 (unsigned long long)NFS_FILEID(hdr->inode),
1021 hdr->args.count,
1022 (unsigned long long)hdr->args.offset);
1023
Trond Myklebustdc602dd2015-12-31 11:44:06 -05001024 hdr->completion_ops->reschedule_io(hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001025 return;
1026 }
1027
1028 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1029 dprintk("%s Reset task %5u for i/o through MDS "
1030 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1031 hdr->task.tk_pid,
1032 hdr->inode->i_sb->s_id,
1033 (unsigned long long)NFS_FILEID(hdr->inode),
1034 hdr->args.count,
1035 (unsigned long long)hdr->args.offset);
1036
1037 task->tk_status = pnfs_write_done_resend_to_mds(hdr);
1038 }
1039}
1040
1041static void ff_layout_reset_read(struct nfs_pgio_header *hdr)
1042{
1043 struct rpc_task *task = &hdr->task;
1044
1045 pnfs_layoutcommit_inode(hdr->inode, false);
1046
1047 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1048 dprintk("%s Reset task %5u for i/o through MDS "
1049 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1050 hdr->task.tk_pid,
1051 hdr->inode->i_sb->s_id,
1052 (unsigned long long)NFS_FILEID(hdr->inode),
1053 hdr->args.count,
1054 (unsigned long long)hdr->args.offset);
1055
1056 task->tk_status = pnfs_read_done_resend_to_mds(hdr);
1057 }
1058}
1059
1060static int ff_layout_async_handle_error_v4(struct rpc_task *task,
1061 struct nfs4_state *state,
1062 struct nfs_client *clp,
1063 struct pnfs_layout_segment *lseg,
1064 int idx)
1065{
1066 struct pnfs_layout_hdr *lo = lseg->pls_layout;
1067 struct inode *inode = lo->plh_inode;
1068 struct nfs_server *mds_server = NFS_SERVER(inode);
1069
1070 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1071 struct nfs_client *mds_client = mds_server->nfs_client;
1072 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
1073
1074 if (task->tk_status >= 0)
1075 return 0;
1076
1077 switch (task->tk_status) {
1078 /* MDS state errors */
1079 case -NFS4ERR_DELEG_REVOKED:
1080 case -NFS4ERR_ADMIN_REVOKED:
1081 case -NFS4ERR_BAD_STATEID:
1082 if (state == NULL)
1083 break;
1084 nfs_remove_bad_delegation(state->inode);
1085 case -NFS4ERR_OPENMODE:
1086 if (state == NULL)
1087 break;
1088 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
1089 goto out_bad_stateid;
1090 goto wait_on_recovery;
1091 case -NFS4ERR_EXPIRED:
1092 if (state != NULL) {
1093 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
1094 goto out_bad_stateid;
1095 }
1096 nfs4_schedule_lease_recovery(mds_client);
1097 goto wait_on_recovery;
1098 /* DS session errors */
1099 case -NFS4ERR_BADSESSION:
1100 case -NFS4ERR_BADSLOT:
1101 case -NFS4ERR_BAD_HIGH_SLOT:
1102 case -NFS4ERR_DEADSESSION:
1103 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1104 case -NFS4ERR_SEQ_FALSE_RETRY:
1105 case -NFS4ERR_SEQ_MISORDERED:
1106 dprintk("%s ERROR %d, Reset session. Exchangeid "
1107 "flags 0x%x\n", __func__, task->tk_status,
1108 clp->cl_exchange_flags);
1109 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
1110 break;
1111 case -NFS4ERR_DELAY:
1112 case -NFS4ERR_GRACE:
1113 rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX);
1114 break;
1115 case -NFS4ERR_RETRY_UNCACHED_REP:
1116 break;
1117 /* Invalidate Layout errors */
1118 case -NFS4ERR_PNFS_NO_LAYOUT:
1119 case -ESTALE: /* mapped NFS4ERR_STALE */
1120 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
1121 case -EISDIR: /* mapped NFS4ERR_ISDIR */
1122 case -NFS4ERR_FHEXPIRED:
1123 case -NFS4ERR_WRONG_TYPE:
1124 dprintk("%s Invalid layout error %d\n", __func__,
1125 task->tk_status);
1126 /*
1127 * Destroy layout so new i/o will get a new layout.
1128 * Layout will not be destroyed until all current lseg
1129 * references are put. Mark layout as invalid to resend failed
1130 * i/o and all i/o waiting on the slot table to the MDS until
1131 * layout is destroyed and a new valid layout is obtained.
1132 */
1133 pnfs_destroy_layout(NFS_I(inode));
1134 rpc_wake_up(&tbl->slot_tbl_waitq);
1135 goto reset;
1136 /* RPC connection errors */
1137 case -ECONNREFUSED:
1138 case -EHOSTDOWN:
1139 case -EHOSTUNREACH:
1140 case -ENETUNREACH:
1141 case -EIO:
1142 case -ETIMEDOUT:
1143 case -EPIPE:
1144 dprintk("%s DS connection error %d\n", __func__,
1145 task->tk_status);
1146 nfs4_mark_deviceid_unavailable(devid);
1147 rpc_wake_up(&tbl->slot_tbl_waitq);
1148 /* fall through */
1149 default:
Tom Haynes3b13b4b2016-05-17 12:28:37 -04001150 if (ff_layout_avoid_mds_available_ds(lseg))
Tom Haynesd67ae822014-12-11 17:02:04 -05001151 return -NFS4ERR_RESET_TO_PNFS;
1152reset:
1153 dprintk("%s Retry through MDS. Error %d\n", __func__,
1154 task->tk_status);
1155 return -NFS4ERR_RESET_TO_MDS;
1156 }
1157out:
1158 task->tk_status = 0;
1159 return -EAGAIN;
1160out_bad_stateid:
1161 task->tk_status = -EIO;
1162 return 0;
1163wait_on_recovery:
1164 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
1165 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
1166 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
1167 goto out;
1168}
1169
1170/* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
1171static int ff_layout_async_handle_error_v3(struct rpc_task *task,
1172 struct pnfs_layout_segment *lseg,
1173 int idx)
1174{
1175 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1176
1177 if (task->tk_status >= 0)
1178 return 0;
1179
Trond Myklebust90816d12015-08-30 09:53:06 -07001180 switch (task->tk_status) {
1181 /* File access problems. Don't mark the device as unavailable */
1182 case -EACCES:
1183 case -ESTALE:
1184 case -EISDIR:
1185 case -EBADHANDLE:
1186 case -ELOOP:
1187 case -ENOSPC:
1188 break;
1189 case -EJUKEBOX:
1190 nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY);
1191 goto out_retry;
1192 default:
Tom Haynesd67ae822014-12-11 17:02:04 -05001193 dprintk("%s DS connection error %d\n", __func__,
1194 task->tk_status);
1195 nfs4_mark_deviceid_unavailable(devid);
Tom Haynesd67ae822014-12-11 17:02:04 -05001196 }
Trond Myklebust90816d12015-08-30 09:53:06 -07001197 /* FIXME: Need to prevent infinite looping here. */
1198 return -NFS4ERR_RESET_TO_PNFS;
1199out_retry:
Tom Haynesd67ae822014-12-11 17:02:04 -05001200 task->tk_status = 0;
Trond Myklebust7eeea162015-12-17 09:22:15 -05001201 rpc_restart_call_prepare(task);
Tom Haynesd67ae822014-12-11 17:02:04 -05001202 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
1203 return -EAGAIN;
1204}
1205
1206static int ff_layout_async_handle_error(struct rpc_task *task,
1207 struct nfs4_state *state,
1208 struct nfs_client *clp,
1209 struct pnfs_layout_segment *lseg,
1210 int idx)
1211{
1212 int vers = clp->cl_nfs_mod->rpc_vers->number;
1213
1214 switch (vers) {
1215 case 3:
1216 return ff_layout_async_handle_error_v3(task, lseg, idx);
1217 case 4:
1218 return ff_layout_async_handle_error_v4(task, state, clp,
1219 lseg, idx);
1220 default:
1221 /* should never happen */
1222 WARN_ON_ONCE(1);
1223 return 0;
1224 }
1225}
1226
1227static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
1228 int idx, u64 offset, u64 length,
Trond Myklebustdd521282015-08-20 17:59:49 -05001229 u32 status, int opnum, int error)
Tom Haynesd67ae822014-12-11 17:02:04 -05001230{
1231 struct nfs4_ff_layout_mirror *mirror;
1232 int err;
1233
Trond Myklebustdd521282015-08-20 17:59:49 -05001234 if (status == 0) {
1235 switch (error) {
1236 case -ETIMEDOUT:
1237 case -EPFNOSUPPORT:
1238 case -EPROTONOSUPPORT:
1239 case -EOPNOTSUPP:
1240 case -ECONNREFUSED:
1241 case -ECONNRESET:
1242 case -EHOSTDOWN:
1243 case -EHOSTUNREACH:
1244 case -ENETUNREACH:
1245 case -EADDRINUSE:
1246 case -ENOBUFS:
1247 case -EPIPE:
1248 case -EPERM:
1249 status = NFS4ERR_NXIO;
1250 break;
1251 case -EACCES:
1252 status = NFS4ERR_ACCESS;
1253 break;
1254 default:
1255 return;
1256 }
1257 }
1258
Peng Taoc22eeb82015-12-07 17:31:38 -05001259 switch (status) {
1260 case NFS4ERR_DELAY:
1261 case NFS4ERR_GRACE:
1262 return;
1263 default:
1264 break;
1265 }
1266
Tom Haynesd67ae822014-12-11 17:02:04 -05001267 mirror = FF_LAYOUT_COMP(lseg, idx);
1268 err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
1269 mirror, offset, length, status, opnum,
1270 GFP_NOIO);
Trond Myklebustf95c03b2015-09-02 15:15:11 -07001271 pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg);
Tom Haynesd67ae822014-12-11 17:02:04 -05001272 dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status);
1273}
1274
1275/* NFS_PROTO call done callback routines */
Tom Haynesd67ae822014-12-11 17:02:04 -05001276static int ff_layout_read_done_cb(struct rpc_task *task,
1277 struct nfs_pgio_header *hdr)
1278{
Tom Haynesd67ae822014-12-11 17:02:04 -05001279 int err;
1280
1281 trace_nfs4_pnfs_read(hdr, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001282 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001283 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1284 hdr->args.offset, hdr->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001285 hdr->res.op_status, OP_READ,
1286 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001287 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1288 hdr->ds_clp, hdr->lseg,
1289 hdr->pgio_mirror_idx);
1290
1291 switch (err) {
1292 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust13544412015-11-02 10:11:17 -05001293 if (ff_layout_choose_best_ds_for_read(hdr->lseg,
1294 hdr->pgio_mirror_idx + 1,
1295 &hdr->pgio_mirror_idx))
1296 goto out_eagain;
Tom Haynesd67ae822014-12-11 17:02:04 -05001297 pnfs_read_resend_pnfs(hdr);
1298 return task->tk_status;
1299 case -NFS4ERR_RESET_TO_MDS:
Tom Haynesd67ae822014-12-11 17:02:04 -05001300 ff_layout_reset_read(hdr);
1301 return task->tk_status;
1302 case -EAGAIN:
Trond Myklebust13544412015-11-02 10:11:17 -05001303 goto out_eagain;
Tom Haynesd67ae822014-12-11 17:02:04 -05001304 }
1305
1306 return 0;
Trond Myklebust13544412015-11-02 10:11:17 -05001307out_eagain:
1308 rpc_restart_call_prepare(task);
1309 return -EAGAIN;
Tom Haynesd67ae822014-12-11 17:02:04 -05001310}
1311
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001312static bool
1313ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg)
1314{
1315 return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT);
1316}
1317
Tom Haynesd67ae822014-12-11 17:02:04 -05001318/*
1319 * We reference the rpc_cred of the first WRITE that triggers the need for
1320 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
1321 * rfc5661 is not clear about which credential should be used.
1322 *
1323 * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
1324 * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
1325 * we always send layoutcommit after DS writes.
1326 */
1327static void
Trond Myklebustbc28e1c2016-06-26 16:14:40 -04001328ff_layout_set_layoutcommit(struct inode *inode,
1329 struct pnfs_layout_segment *lseg,
1330 loff_t end_offset)
Tom Haynesd67ae822014-12-11 17:02:04 -05001331{
Trond Myklebustbc28e1c2016-06-26 16:14:40 -04001332 if (!ff_layout_need_layoutcommit(lseg))
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001333 return;
1334
Trond Myklebustbc28e1c2016-06-26 16:14:40 -04001335 pnfs_set_layoutcommit(inode, lseg, end_offset);
1336 dprintk("%s inode %lu pls_end_pos %llu\n", __func__, inode->i_ino,
1337 (unsigned long long) NFS_I(inode)->layout->plh_lwb);
Tom Haynesd67ae822014-12-11 17:02:04 -05001338}
1339
1340static bool
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001341ff_layout_device_unavailable(struct pnfs_layout_segment *lseg, int idx)
Tom Haynesd67ae822014-12-11 17:02:04 -05001342{
1343 /* No mirroring for now */
1344 struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx);
1345
1346 return ff_layout_test_devid_unavailable(node);
1347}
1348
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001349static void ff_layout_read_record_layoutstats_start(struct rpc_task *task,
1350 struct nfs_pgio_header *hdr)
Tom Haynesd67ae822014-12-11 17:02:04 -05001351{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001352 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1353 return;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04001354 nfs4_ff_layout_stat_io_start_read(hdr->inode,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001355 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
Trond Myklebuste76d28d2015-08-20 13:12:51 -05001356 hdr->args.count,
1357 task->tk_start);
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001358}
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001359
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001360static void ff_layout_read_record_layoutstats_done(struct rpc_task *task,
1361 struct nfs_pgio_header *hdr)
1362{
1363 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1364 return;
1365 nfs4_ff_layout_stat_io_end_read(task,
1366 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1367 hdr->args.count,
1368 hdr->res.count);
1369}
1370
1371static int ff_layout_read_prepare_common(struct rpc_task *task,
1372 struct nfs_pgio_header *hdr)
1373{
Tom Haynesd67ae822014-12-11 17:02:04 -05001374 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1375 rpc_exit(task, -EIO);
1376 return -EIO;
1377 }
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001378 if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) {
1379 rpc_exit(task, -EHOSTDOWN);
Tom Haynesd67ae822014-12-11 17:02:04 -05001380 return -EAGAIN;
1381 }
Tom Haynesd67ae822014-12-11 17:02:04 -05001382
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001383 ff_layout_read_record_layoutstats_start(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001384 return 0;
1385}
1386
1387/*
1388 * Call ops for the async read/write cases
1389 * In the case of dense layouts, the offset needs to be reset to its
1390 * original value.
1391 */
1392static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data)
1393{
1394 struct nfs_pgio_header *hdr = data;
1395
1396 if (ff_layout_read_prepare_common(task, hdr))
1397 return;
1398
1399 rpc_call_start(task);
1400}
1401
1402static int ff_layout_setup_sequence(struct nfs_client *ds_clp,
1403 struct nfs4_sequence_args *args,
1404 struct nfs4_sequence_res *res,
1405 struct rpc_task *task)
1406{
1407 if (ds_clp->cl_session)
1408 return nfs41_setup_sequence(ds_clp->cl_session,
1409 args,
1410 res,
1411 task);
1412 return nfs40_setup_sequence(ds_clp->cl_slot_tbl,
1413 args,
1414 res,
1415 task);
1416}
1417
1418static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data)
1419{
1420 struct nfs_pgio_header *hdr = data;
1421
Tom Haynesd67ae822014-12-11 17:02:04 -05001422 if (ff_layout_setup_sequence(hdr->ds_clp,
1423 &hdr->args.seq_args,
1424 &hdr->res.seq_res,
1425 task))
1426 return;
1427
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001428 if (ff_layout_read_prepare_common(task, hdr))
1429 return;
1430
Tom Haynesd67ae822014-12-11 17:02:04 -05001431 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1432 hdr->args.lock_context, FMODE_READ) == -EIO)
1433 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1434}
1435
1436static void ff_layout_read_call_done(struct rpc_task *task, void *data)
1437{
1438 struct nfs_pgio_header *hdr = data;
1439
1440 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
1441
1442 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1443 task->tk_status == 0) {
1444 nfs4_sequence_done(task, &hdr->res.seq_res);
1445 return;
1446 }
1447
1448 /* Note this may cause RPC to be resent */
1449 hdr->mds_ops->rpc_call_done(task, hdr);
1450}
1451
1452static void ff_layout_read_count_stats(struct rpc_task *task, void *data)
1453{
1454 struct nfs_pgio_header *hdr = data;
1455
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001456 ff_layout_read_record_layoutstats_done(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001457 rpc_count_iostats_metrics(task,
1458 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]);
1459}
1460
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001461static void ff_layout_read_release(void *data)
1462{
1463 struct nfs_pgio_header *hdr = data;
1464
1465 ff_layout_read_record_layoutstats_done(&hdr->task, hdr);
1466 pnfs_generic_rw_release(data);
1467}
1468
1469
Tom Haynesd67ae822014-12-11 17:02:04 -05001470static int ff_layout_write_done_cb(struct rpc_task *task,
1471 struct nfs_pgio_header *hdr)
1472{
Trond Myklebust2e18d4d2016-06-26 18:54:58 -04001473 loff_t end_offs = 0;
Tom Haynesd67ae822014-12-11 17:02:04 -05001474 int err;
1475
1476 trace_nfs4_pnfs_write(hdr, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001477 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001478 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1479 hdr->args.offset, hdr->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001480 hdr->res.op_status, OP_WRITE,
1481 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001482 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1483 hdr->ds_clp, hdr->lseg,
1484 hdr->pgio_mirror_idx);
1485
1486 switch (err) {
1487 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001488 ff_layout_reset_write(hdr, true);
1489 return task->tk_status;
Tom Haynesd67ae822014-12-11 17:02:04 -05001490 case -NFS4ERR_RESET_TO_MDS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001491 ff_layout_reset_write(hdr, false);
Tom Haynesd67ae822014-12-11 17:02:04 -05001492 return task->tk_status;
1493 case -EAGAIN:
Tom Haynesd67ae822014-12-11 17:02:04 -05001494 return -EAGAIN;
1495 }
1496
1497 if (hdr->res.verf->committed == NFS_FILE_SYNC ||
1498 hdr->res.verf->committed == NFS_DATA_SYNC)
Trond Myklebust2e18d4d2016-06-26 18:54:58 -04001499 end_offs = hdr->mds_offset + (loff_t)hdr->res.count;
1500
1501 /* Note: if the write is unstable, don't set end_offs until commit */
1502 ff_layout_set_layoutcommit(hdr->inode, hdr->lseg, end_offs);
Tom Haynesd67ae822014-12-11 17:02:04 -05001503
Peng Tao54204012015-08-22 06:40:00 +08001504 /* zero out fattr since we don't care DS attr at all */
1505 hdr->fattr.valid = 0;
Peng Tao69f230d2015-08-20 01:52:59 +08001506 if (task->tk_status >= 0)
1507 nfs_writeback_update_inode(hdr);
1508
Tom Haynesd67ae822014-12-11 17:02:04 -05001509 return 0;
1510}
1511
1512static int ff_layout_commit_done_cb(struct rpc_task *task,
1513 struct nfs_commit_data *data)
1514{
Tom Haynesd67ae822014-12-11 17:02:04 -05001515 int err;
1516
1517 trace_nfs4_pnfs_commit_ds(data, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001518 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001519 ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index,
1520 data->args.offset, data->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001521 data->res.op_status, OP_COMMIT,
1522 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001523 err = ff_layout_async_handle_error(task, NULL, data->ds_clp,
1524 data->lseg, data->ds_commit_index);
1525
1526 switch (err) {
1527 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001528 pnfs_generic_prepare_to_resend_writes(data);
1529 return -EAGAIN;
Tom Haynesd67ae822014-12-11 17:02:04 -05001530 case -NFS4ERR_RESET_TO_MDS:
Tom Haynesd67ae822014-12-11 17:02:04 -05001531 pnfs_generic_prepare_to_resend_writes(data);
1532 return -EAGAIN;
1533 case -EAGAIN:
1534 rpc_restart_call_prepare(task);
1535 return -EAGAIN;
1536 }
1537
Trond Myklebustbc28e1c2016-06-26 16:14:40 -04001538 ff_layout_set_layoutcommit(data->inode, data->lseg, data->lwb);
Tom Haynesd67ae822014-12-11 17:02:04 -05001539
1540 return 0;
1541}
1542
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001543static void ff_layout_write_record_layoutstats_start(struct rpc_task *task,
1544 struct nfs_pgio_header *hdr)
Tom Haynesd67ae822014-12-11 17:02:04 -05001545{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001546 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1547 return;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04001548 nfs4_ff_layout_stat_io_start_write(hdr->inode,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001549 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
Trond Myklebuste76d28d2015-08-20 13:12:51 -05001550 hdr->args.count,
1551 task->tk_start);
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001552}
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001553
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001554static void ff_layout_write_record_layoutstats_done(struct rpc_task *task,
1555 struct nfs_pgio_header *hdr)
1556{
1557 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1558 return;
1559 nfs4_ff_layout_stat_io_end_write(task,
1560 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1561 hdr->args.count, hdr->res.count,
1562 hdr->res.verf->committed);
1563}
1564
1565static int ff_layout_write_prepare_common(struct rpc_task *task,
1566 struct nfs_pgio_header *hdr)
1567{
Tom Haynesd67ae822014-12-11 17:02:04 -05001568 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1569 rpc_exit(task, -EIO);
1570 return -EIO;
1571 }
1572
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001573 if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) {
1574 rpc_exit(task, -EHOSTDOWN);
Tom Haynesd67ae822014-12-11 17:02:04 -05001575 return -EAGAIN;
1576 }
1577
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001578 ff_layout_write_record_layoutstats_start(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001579 return 0;
1580}
1581
1582static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data)
1583{
1584 struct nfs_pgio_header *hdr = data;
1585
1586 if (ff_layout_write_prepare_common(task, hdr))
1587 return;
1588
1589 rpc_call_start(task);
1590}
1591
1592static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data)
1593{
1594 struct nfs_pgio_header *hdr = data;
1595
Tom Haynesd67ae822014-12-11 17:02:04 -05001596 if (ff_layout_setup_sequence(hdr->ds_clp,
1597 &hdr->args.seq_args,
1598 &hdr->res.seq_res,
1599 task))
1600 return;
1601
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001602 if (ff_layout_write_prepare_common(task, hdr))
1603 return;
1604
Tom Haynesd67ae822014-12-11 17:02:04 -05001605 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1606 hdr->args.lock_context, FMODE_WRITE) == -EIO)
1607 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1608}
1609
1610static void ff_layout_write_call_done(struct rpc_task *task, void *data)
1611{
1612 struct nfs_pgio_header *hdr = data;
1613
1614 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1615 task->tk_status == 0) {
1616 nfs4_sequence_done(task, &hdr->res.seq_res);
1617 return;
1618 }
1619
1620 /* Note this may cause RPC to be resent */
1621 hdr->mds_ops->rpc_call_done(task, hdr);
1622}
1623
1624static void ff_layout_write_count_stats(struct rpc_task *task, void *data)
1625{
1626 struct nfs_pgio_header *hdr = data;
1627
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001628 ff_layout_write_record_layoutstats_done(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001629 rpc_count_iostats_metrics(task,
1630 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]);
1631}
1632
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001633static void ff_layout_write_release(void *data)
1634{
1635 struct nfs_pgio_header *hdr = data;
1636
1637 ff_layout_write_record_layoutstats_done(&hdr->task, hdr);
1638 pnfs_generic_rw_release(data);
1639}
1640
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001641static void ff_layout_commit_record_layoutstats_start(struct rpc_task *task,
1642 struct nfs_commit_data *cdata)
1643{
1644 if (test_and_set_bit(NFS_IOHDR_STAT, &cdata->flags))
1645 return;
1646 nfs4_ff_layout_stat_io_start_write(cdata->inode,
1647 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1648 0, task->tk_start);
1649}
1650
1651static void ff_layout_commit_record_layoutstats_done(struct rpc_task *task,
1652 struct nfs_commit_data *cdata)
1653{
1654 struct nfs_page *req;
1655 __u64 count = 0;
1656
1657 if (!test_and_clear_bit(NFS_IOHDR_STAT, &cdata->flags))
1658 return;
1659
1660 if (task->tk_status == 0) {
1661 list_for_each_entry(req, &cdata->pages, wb_list)
1662 count += req->wb_bytes;
1663 }
1664 nfs4_ff_layout_stat_io_end_write(task,
1665 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1666 count, count, NFS_FILE_SYNC);
1667}
1668
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001669static void ff_layout_commit_prepare_common(struct rpc_task *task,
1670 struct nfs_commit_data *cdata)
1671{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001672 ff_layout_commit_record_layoutstats_start(task, cdata);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001673}
1674
Tom Haynesd67ae822014-12-11 17:02:04 -05001675static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data)
1676{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001677 ff_layout_commit_prepare_common(task, data);
Tom Haynesd67ae822014-12-11 17:02:04 -05001678 rpc_call_start(task);
1679}
1680
1681static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data)
1682{
1683 struct nfs_commit_data *wdata = data;
1684
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001685 if (ff_layout_setup_sequence(wdata->ds_clp,
Tom Haynesd67ae822014-12-11 17:02:04 -05001686 &wdata->args.seq_args,
1687 &wdata->res.seq_res,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001688 task))
1689 return;
1690 ff_layout_commit_prepare_common(task, data);
1691}
1692
1693static void ff_layout_commit_done(struct rpc_task *task, void *data)
1694{
Peng Tao141b9b52015-12-07 18:33:14 -05001695 pnfs_generic_write_commit_done(task, data);
1696}
1697
1698static void ff_layout_commit_count_stats(struct rpc_task *task, void *data)
1699{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001700 struct nfs_commit_data *cdata = data;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001701
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001702 ff_layout_commit_record_layoutstats_done(task, cdata);
Tom Haynesd67ae822014-12-11 17:02:04 -05001703 rpc_count_iostats_metrics(task,
1704 &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]);
1705}
1706
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001707static void ff_layout_commit_release(void *data)
1708{
1709 struct nfs_commit_data *cdata = data;
1710
1711 ff_layout_commit_record_layoutstats_done(&cdata->task, cdata);
1712 pnfs_generic_commit_release(data);
1713}
1714
Tom Haynesd67ae822014-12-11 17:02:04 -05001715static const struct rpc_call_ops ff_layout_read_call_ops_v3 = {
1716 .rpc_call_prepare = ff_layout_read_prepare_v3,
1717 .rpc_call_done = ff_layout_read_call_done,
1718 .rpc_count_stats = ff_layout_read_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001719 .rpc_release = ff_layout_read_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001720};
1721
1722static const struct rpc_call_ops ff_layout_read_call_ops_v4 = {
1723 .rpc_call_prepare = ff_layout_read_prepare_v4,
1724 .rpc_call_done = ff_layout_read_call_done,
1725 .rpc_count_stats = ff_layout_read_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001726 .rpc_release = ff_layout_read_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001727};
1728
1729static const struct rpc_call_ops ff_layout_write_call_ops_v3 = {
1730 .rpc_call_prepare = ff_layout_write_prepare_v3,
1731 .rpc_call_done = ff_layout_write_call_done,
1732 .rpc_count_stats = ff_layout_write_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001733 .rpc_release = ff_layout_write_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001734};
1735
1736static const struct rpc_call_ops ff_layout_write_call_ops_v4 = {
1737 .rpc_call_prepare = ff_layout_write_prepare_v4,
1738 .rpc_call_done = ff_layout_write_call_done,
1739 .rpc_count_stats = ff_layout_write_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001740 .rpc_release = ff_layout_write_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001741};
1742
1743static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = {
1744 .rpc_call_prepare = ff_layout_commit_prepare_v3,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001745 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001746 .rpc_count_stats = ff_layout_commit_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001747 .rpc_release = ff_layout_commit_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001748};
1749
1750static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = {
1751 .rpc_call_prepare = ff_layout_commit_prepare_v4,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001752 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001753 .rpc_count_stats = ff_layout_commit_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001754 .rpc_release = ff_layout_commit_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001755};
1756
1757static enum pnfs_try_status
1758ff_layout_read_pagelist(struct nfs_pgio_header *hdr)
1759{
1760 struct pnfs_layout_segment *lseg = hdr->lseg;
1761 struct nfs4_pnfs_ds *ds;
1762 struct rpc_clnt *ds_clnt;
1763 struct rpc_cred *ds_cred;
1764 loff_t offset = hdr->args.offset;
1765 u32 idx = hdr->pgio_mirror_idx;
1766 int vers;
1767 struct nfs_fh *fh;
1768
1769 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
1770 __func__, hdr->inode->i_ino,
1771 hdr->args.pgbase, (size_t)hdr->args.count, offset);
1772
1773 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
1774 if (!ds)
1775 goto out_failed;
1776
1777 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1778 hdr->inode);
1779 if (IS_ERR(ds_clnt))
1780 goto out_failed;
1781
1782 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001783 if (!ds_cred)
Tom Haynesd67ae822014-12-11 17:02:04 -05001784 goto out_failed;
1785
1786 vers = nfs4_ff_layout_ds_version(lseg, idx);
1787
1788 dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__,
1789 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers);
1790
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001791 hdr->pgio_done_cb = ff_layout_read_done_cb;
Tom Haynesd67ae822014-12-11 17:02:04 -05001792 atomic_inc(&ds->ds_clp->cl_count);
1793 hdr->ds_clp = ds->ds_clp;
1794 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1795 if (fh)
1796 hdr->args.fh = fh;
Tom Haynesd67ae822014-12-11 17:02:04 -05001797 /*
1798 * Note that if we ever decide to split across DSes,
1799 * then we may need to handle dense-like offsets.
1800 */
1801 hdr->args.offset = offset;
1802 hdr->mds_offset = offset;
1803
1804 /* Perform an asynchronous read to ds */
1805 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1806 vers == 3 ? &ff_layout_read_call_ops_v3 :
1807 &ff_layout_read_call_ops_v4,
1808 0, RPC_TASK_SOFTCONN);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001809 put_rpccred(ds_cred);
Tom Haynesd67ae822014-12-11 17:02:04 -05001810 return PNFS_ATTEMPTED;
1811
1812out_failed:
Tom Haynes3b13b4b2016-05-17 12:28:37 -04001813 if (ff_layout_avoid_mds_available_ds(lseg))
Tom Haynesd67ae822014-12-11 17:02:04 -05001814 return PNFS_TRY_AGAIN;
1815 return PNFS_NOT_ATTEMPTED;
1816}
1817
1818/* Perform async writes. */
1819static enum pnfs_try_status
1820ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
1821{
1822 struct pnfs_layout_segment *lseg = hdr->lseg;
1823 struct nfs4_pnfs_ds *ds;
1824 struct rpc_clnt *ds_clnt;
1825 struct rpc_cred *ds_cred;
1826 loff_t offset = hdr->args.offset;
1827 int vers;
1828 struct nfs_fh *fh;
1829 int idx = hdr->pgio_mirror_idx;
1830
1831 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1832 if (!ds)
1833 return PNFS_NOT_ATTEMPTED;
1834
1835 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1836 hdr->inode);
1837 if (IS_ERR(ds_clnt))
1838 return PNFS_NOT_ATTEMPTED;
1839
1840 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001841 if (!ds_cred)
Tom Haynesd67ae822014-12-11 17:02:04 -05001842 return PNFS_NOT_ATTEMPTED;
1843
1844 vers = nfs4_ff_layout_ds_version(lseg, idx);
1845
1846 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n",
1847 __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
1848 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count),
1849 vers);
1850
1851 hdr->pgio_done_cb = ff_layout_write_done_cb;
1852 atomic_inc(&ds->ds_clp->cl_count);
1853 hdr->ds_clp = ds->ds_clp;
1854 hdr->ds_commit_idx = idx;
1855 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1856 if (fh)
1857 hdr->args.fh = fh;
1858
1859 /*
1860 * Note that if we ever decide to split across DSes,
1861 * then we may need to handle dense-like offsets.
1862 */
1863 hdr->args.offset = offset;
1864
1865 /* Perform an asynchronous write */
1866 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1867 vers == 3 ? &ff_layout_write_call_ops_v3 :
1868 &ff_layout_write_call_ops_v4,
1869 sync, RPC_TASK_SOFTCONN);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001870 put_rpccred(ds_cred);
Tom Haynesd67ae822014-12-11 17:02:04 -05001871 return PNFS_ATTEMPTED;
1872}
1873
Tom Haynesd67ae822014-12-11 17:02:04 -05001874static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1875{
1876 return i;
1877}
1878
1879static struct nfs_fh *
1880select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1881{
1882 struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg);
1883
1884 /* FIXME: Assume that there is only one NFS version available
1885 * for the DS.
1886 */
1887 return &flseg->mirror_array[i]->fh_versions[0];
1888}
1889
1890static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how)
1891{
1892 struct pnfs_layout_segment *lseg = data->lseg;
1893 struct nfs4_pnfs_ds *ds;
1894 struct rpc_clnt *ds_clnt;
1895 struct rpc_cred *ds_cred;
1896 u32 idx;
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001897 int vers, ret;
Tom Haynesd67ae822014-12-11 17:02:04 -05001898 struct nfs_fh *fh;
1899
1900 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1901 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1902 if (!ds)
1903 goto out_err;
1904
1905 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1906 data->inode);
1907 if (IS_ERR(ds_clnt))
1908 goto out_err;
1909
1910 ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001911 if (!ds_cred)
Tom Haynesd67ae822014-12-11 17:02:04 -05001912 goto out_err;
1913
1914 vers = nfs4_ff_layout_ds_version(lseg, idx);
1915
1916 dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__,
1917 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count),
1918 vers);
1919 data->commit_done_cb = ff_layout_commit_done_cb;
1920 data->cred = ds_cred;
1921 atomic_inc(&ds->ds_clp->cl_count);
1922 data->ds_clp = ds->ds_clp;
1923 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1924 if (fh)
1925 data->args.fh = fh;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001926
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001927 ret = nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops,
Tom Haynesd67ae822014-12-11 17:02:04 -05001928 vers == 3 ? &ff_layout_commit_call_ops_v3 :
1929 &ff_layout_commit_call_ops_v4,
1930 how, RPC_TASK_SOFTCONN);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001931 put_rpccred(ds_cred);
1932 return ret;
Tom Haynesd67ae822014-12-11 17:02:04 -05001933out_err:
1934 pnfs_generic_prepare_to_resend_writes(data);
1935 pnfs_generic_commit_release(data);
1936 return -EAGAIN;
1937}
1938
1939static int
1940ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1941 int how, struct nfs_commit_info *cinfo)
1942{
1943 return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
1944 ff_layout_initiate_commit);
1945}
1946
1947static struct pnfs_ds_commit_info *
1948ff_layout_get_ds_info(struct inode *inode)
1949{
1950 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1951
1952 if (layout == NULL)
1953 return NULL;
1954
1955 return &FF_LAYOUT_FROM_HDR(layout)->commit_info;
1956}
1957
1958static void
Trond Myklebustfc877012015-03-09 17:25:14 -04001959ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d)
Tom Haynesd67ae822014-12-11 17:02:04 -05001960{
1961 nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds,
1962 id_node));
1963}
1964
1965static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo,
1966 struct xdr_stream *xdr,
1967 const struct nfs4_layoutreturn_args *args)
1968{
1969 struct pnfs_layout_hdr *hdr = &flo->generic_hdr;
1970 __be32 *start;
1971 int count = 0, ret = 0;
1972
1973 start = xdr_reserve_space(xdr, 4);
1974 if (unlikely(!start))
1975 return -E2BIG;
1976
1977 /* This assume we always return _ALL_ layouts */
1978 spin_lock(&hdr->plh_inode->i_lock);
1979 ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range);
1980 spin_unlock(&hdr->plh_inode->i_lock);
1981
1982 *start = cpu_to_be32(count);
1983
1984 return ret;
1985}
1986
1987/* report nothing for now */
1988static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo,
1989 struct xdr_stream *xdr,
1990 const struct nfs4_layoutreturn_args *args)
1991{
1992 __be32 *p;
1993
1994 p = xdr_reserve_space(xdr, 4);
1995 if (likely(p))
1996 *p = cpu_to_be32(0);
1997}
1998
1999static struct nfs4_deviceid_node *
2000ff_layout_alloc_deviceid_node(struct nfs_server *server,
2001 struct pnfs_device *pdev, gfp_t gfp_flags)
2002{
2003 struct nfs4_ff_layout_ds *dsaddr;
2004
2005 dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags);
2006 if (!dsaddr)
2007 return NULL;
2008 return &dsaddr->id_node;
2009}
2010
2011static void
2012ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo,
2013 struct xdr_stream *xdr,
2014 const struct nfs4_layoutreturn_args *args)
2015{
2016 struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
2017 __be32 *start;
2018
2019 dprintk("%s: Begin\n", __func__);
2020 start = xdr_reserve_space(xdr, 4);
2021 BUG_ON(!start);
2022
Trond Myklebust082fa372016-01-21 15:39:40 -05002023 ff_layout_encode_ioerr(flo, xdr, args);
Tom Haynesd67ae822014-12-11 17:02:04 -05002024 ff_layout_encode_iostats(flo, xdr, args);
Trond Myklebust082fa372016-01-21 15:39:40 -05002025
Tom Haynesd67ae822014-12-11 17:02:04 -05002026 *start = cpu_to_be32((xdr->p - start - 1) * 4);
2027 dprintk("%s: Return\n", __func__);
2028}
2029
Peng Tao27c43062015-06-23 19:52:02 +08002030static int
2031ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen)
2032{
2033 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
2034
2035 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
2036}
2037
2038static size_t
2039ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf,
2040 const int buflen)
2041{
2042 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
2043 const struct in6_addr *addr = &sin6->sin6_addr;
2044
2045 /*
2046 * RFC 4291, Section 2.2.2
2047 *
2048 * Shorthanded ANY address
2049 */
2050 if (ipv6_addr_any(addr))
2051 return snprintf(buf, buflen, "::");
2052
2053 /*
2054 * RFC 4291, Section 2.2.2
2055 *
2056 * Shorthanded loopback address
2057 */
2058 if (ipv6_addr_loopback(addr))
2059 return snprintf(buf, buflen, "::1");
2060
2061 /*
2062 * RFC 4291, Section 2.2.3
2063 *
2064 * Special presentation address format for mapped v4
2065 * addresses.
2066 */
2067 if (ipv6_addr_v4mapped(addr))
2068 return snprintf(buf, buflen, "::ffff:%pI4",
2069 &addr->s6_addr32[3]);
2070
2071 /*
2072 * RFC 4291, Section 2.2.1
2073 */
2074 return snprintf(buf, buflen, "%pI6c", addr);
2075}
2076
2077/* Derived from rpc_sockaddr2uaddr */
2078static void
2079ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da)
2080{
2081 struct sockaddr *sap = (struct sockaddr *)&da->da_addr;
2082 char portbuf[RPCBIND_MAXUADDRPLEN];
2083 char addrbuf[RPCBIND_MAXUADDRLEN];
2084 char *netid;
2085 unsigned short port;
2086 int len, netid_len;
2087 __be32 *p;
2088
2089 switch (sap->sa_family) {
2090 case AF_INET:
2091 if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
2092 return;
2093 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
2094 netid = "tcp";
2095 netid_len = 3;
2096 break;
2097 case AF_INET6:
2098 if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
2099 return;
2100 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
2101 netid = "tcp6";
2102 netid_len = 4;
2103 break;
2104 default:
2105 /* we only support tcp and tcp6 */
2106 WARN_ON_ONCE(1);
2107 return;
2108 }
2109
2110 snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff);
2111 len = strlcat(addrbuf, portbuf, sizeof(addrbuf));
2112
2113 p = xdr_reserve_space(xdr, 4 + netid_len);
2114 xdr_encode_opaque(p, netid, netid_len);
2115
2116 p = xdr_reserve_space(xdr, 4 + len);
2117 xdr_encode_opaque(p, addrbuf, len);
2118}
2119
2120static void
2121ff_layout_encode_nfstime(struct xdr_stream *xdr,
2122 ktime_t t)
2123{
2124 struct timespec64 ts;
2125 __be32 *p;
2126
2127 p = xdr_reserve_space(xdr, 12);
2128 ts = ktime_to_timespec64(t);
2129 p = xdr_encode_hyper(p, ts.tv_sec);
2130 *p++ = cpu_to_be32(ts.tv_nsec);
2131}
2132
2133static void
2134ff_layout_encode_io_latency(struct xdr_stream *xdr,
2135 struct nfs4_ff_io_stat *stat)
2136{
2137 __be32 *p;
2138
2139 p = xdr_reserve_space(xdr, 5 * 8);
2140 p = xdr_encode_hyper(p, stat->ops_requested);
2141 p = xdr_encode_hyper(p, stat->bytes_requested);
2142 p = xdr_encode_hyper(p, stat->ops_completed);
2143 p = xdr_encode_hyper(p, stat->bytes_completed);
2144 p = xdr_encode_hyper(p, stat->bytes_not_delivered);
2145 ff_layout_encode_nfstime(xdr, stat->total_busy_time);
2146 ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time);
2147}
2148
2149static void
2150ff_layout_encode_layoutstats(struct xdr_stream *xdr,
2151 struct nfs42_layoutstat_args *args,
2152 struct nfs42_layoutstat_devinfo *devinfo)
2153{
2154 struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private;
2155 struct nfs4_pnfs_ds_addr *da;
2156 struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds;
2157 struct nfs_fh *fh = &mirror->fh_versions[0];
2158 __be32 *p, *start;
2159
2160 da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node);
2161 dprintk("%s: DS %s: encoding address %s\n",
2162 __func__, ds->ds_remotestr, da->da_remotestr);
2163 /* layoutupdate length */
2164 start = xdr_reserve_space(xdr, 4);
2165 /* netaddr4 */
2166 ff_layout_encode_netaddr(xdr, da);
2167 /* nfs_fh4 */
2168 p = xdr_reserve_space(xdr, 4 + fh->size);
2169 xdr_encode_opaque(p, fh->data, fh->size);
2170 /* ff_io_latency4 read */
2171 spin_lock(&mirror->lock);
2172 ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat);
2173 /* ff_io_latency4 write */
2174 ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat);
2175 spin_unlock(&mirror->lock);
2176 /* nfstime4 */
2177 ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time));
2178 /* bool */
2179 p = xdr_reserve_space(xdr, 4);
2180 *p = cpu_to_be32(false);
2181
2182 *start = cpu_to_be32((xdr->p - start - 1) * 4);
2183}
2184
Trond Myklebust266d12d2015-08-24 20:03:17 -04002185static int
Peng Taoad4dc53e2015-06-23 19:52:01 +08002186ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args,
Trond Myklebust266d12d2015-08-24 20:03:17 -04002187 struct pnfs_layout_hdr *lo,
2188 int dev_limit)
Peng Taoad4dc53e2015-06-23 19:52:01 +08002189{
Trond Myklebust266d12d2015-08-24 20:03:17 -04002190 struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002191 struct nfs4_ff_layout_mirror *mirror;
2192 struct nfs4_deviceid_node *dev;
2193 struct nfs42_layoutstat_devinfo *devinfo;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002194 int i = 0;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002195
Trond Myklebust266d12d2015-08-24 20:03:17 -04002196 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2197 if (i >= dev_limit)
Peng Taoad4dc53e2015-06-23 19:52:01 +08002198 break;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002199 if (!mirror->mirror_ds)
Peng Tao27c43062015-06-23 19:52:02 +08002200 continue;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002201 /* mirror refcount put in cleanup_layoutstats */
2202 if (!atomic_inc_not_zero(&mirror->ref))
2203 continue;
2204 dev = &mirror->mirror_ds->id_node;
2205 devinfo = &args->devinfo[i];
Peng Taoad4dc53e2015-06-23 19:52:01 +08002206 memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002207 devinfo->offset = 0;
2208 devinfo->length = NFS4_MAX_UINT64;
Peng Taod099d7b2015-08-10 16:47:32 +08002209 devinfo->read_count = mirror->read_stat.io_stat.ops_completed;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002210 devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed;
Peng Taod099d7b2015-08-10 16:47:32 +08002211 devinfo->write_count = mirror->write_stat.io_stat.ops_completed;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002212 devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed;
2213 devinfo->layout_type = LAYOUT_FLEX_FILES;
Peng Tao27c43062015-06-23 19:52:02 +08002214 devinfo->layoutstats_encode = ff_layout_encode_layoutstats;
2215 devinfo->layout_private = mirror;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002216
Trond Myklebust266d12d2015-08-24 20:03:17 -04002217 i++;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002218 }
Trond Myklebust266d12d2015-08-24 20:03:17 -04002219 return i;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002220}
2221
2222static int
2223ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args)
2224{
Trond Myklebust266d12d2015-08-24 20:03:17 -04002225 struct nfs4_flexfile_layout *ff_layout;
2226 struct nfs4_ff_layout_mirror *mirror;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002227 int dev_count = 0;
2228
2229 spin_lock(&args->inode->i_lock);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002230 ff_layout = FF_LAYOUT_FROM_HDR(NFS_I(args->inode)->layout);
2231 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2232 if (atomic_read(&mirror->ref) != 0)
2233 dev_count ++;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002234 }
2235 spin_unlock(&args->inode->i_lock);
2236 /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
2237 if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) {
2238 dprintk("%s: truncating devinfo to limit (%d:%d)\n",
2239 __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV);
2240 dev_count = PNFS_LAYOUTSTATS_MAXDEV;
2241 }
Trond Myklebust266d12d2015-08-24 20:03:17 -04002242 args->devinfo = kmalloc_array(dev_count, sizeof(*args->devinfo), GFP_NOIO);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002243 if (!args->devinfo)
2244 return -ENOMEM;
2245
Peng Taoad4dc53e2015-06-23 19:52:01 +08002246 spin_lock(&args->inode->i_lock);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002247 args->num_dev = ff_layout_mirror_prepare_stats(args,
2248 &ff_layout->generic_hdr, dev_count);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002249 spin_unlock(&args->inode->i_lock);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002250
2251 return 0;
2252}
2253
Peng Tao27c43062015-06-23 19:52:02 +08002254static void
2255ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data)
2256{
2257 struct nfs4_ff_layout_mirror *mirror;
2258 int i;
2259
2260 for (i = 0; i < data->args.num_dev; i++) {
2261 mirror = data->args.devinfo[i].layout_private;
2262 data->args.devinfo[i].layout_private = NULL;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04002263 ff_layout_put_mirror(mirror);
Peng Tao27c43062015-06-23 19:52:02 +08002264 }
2265}
2266
Tom Haynesd67ae822014-12-11 17:02:04 -05002267static struct pnfs_layoutdriver_type flexfilelayout_type = {
2268 .id = LAYOUT_FLEX_FILES,
2269 .name = "LAYOUT_FLEX_FILES",
2270 .owner = THIS_MODULE,
2271 .alloc_layout_hdr = ff_layout_alloc_layout_hdr,
2272 .free_layout_hdr = ff_layout_free_layout_hdr,
2273 .alloc_lseg = ff_layout_alloc_lseg,
2274 .free_lseg = ff_layout_free_lseg,
Trond Myklebust0762ed22015-08-25 17:38:25 -04002275 .add_lseg = ff_layout_add_lseg,
Tom Haynesd67ae822014-12-11 17:02:04 -05002276 .pg_read_ops = &ff_layout_pg_read_ops,
2277 .pg_write_ops = &ff_layout_pg_write_ops,
2278 .get_ds_info = ff_layout_get_ds_info,
Trond Myklebustfc877012015-03-09 17:25:14 -04002279 .free_deviceid_node = ff_layout_free_deviceid_node,
Tom Haynes338d00c2015-02-17 14:58:15 -08002280 .mark_request_commit = pnfs_layout_mark_request_commit,
Tom Haynesd67ae822014-12-11 17:02:04 -05002281 .clear_request_commit = pnfs_generic_clear_request_commit,
2282 .scan_commit_lists = pnfs_generic_scan_commit_lists,
2283 .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
2284 .commit_pagelist = ff_layout_commit_pagelist,
2285 .read_pagelist = ff_layout_read_pagelist,
2286 .write_pagelist = ff_layout_write_pagelist,
2287 .alloc_deviceid_node = ff_layout_alloc_deviceid_node,
2288 .encode_layoutreturn = ff_layout_encode_layoutreturn,
Trond Myklebust5bb89b42015-03-25 14:14:42 -04002289 .sync = pnfs_nfs_generic_sync,
Peng Taoad4dc53e2015-06-23 19:52:01 +08002290 .prepare_layoutstats = ff_layout_prepare_layoutstats,
Peng Tao27c43062015-06-23 19:52:02 +08002291 .cleanup_layoutstats = ff_layout_cleanup_layoutstats,
Tom Haynesd67ae822014-12-11 17:02:04 -05002292};
2293
2294static int __init nfs4flexfilelayout_init(void)
2295{
2296 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n",
2297 __func__);
Jeff Layton90a0be02016-04-21 20:51:59 -04002298 if (!ff_zero_group) {
2299 ff_zero_group = groups_alloc(0);
2300 if (!ff_zero_group)
2301 return -ENOMEM;
2302 }
Tom Haynesd67ae822014-12-11 17:02:04 -05002303 return pnfs_register_layoutdriver(&flexfilelayout_type);
2304}
2305
2306static void __exit nfs4flexfilelayout_exit(void)
2307{
2308 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
2309 __func__);
2310 pnfs_unregister_layoutdriver(&flexfilelayout_type);
Jeff Layton90a0be02016-04-21 20:51:59 -04002311 if (ff_zero_group) {
2312 put_group_info(ff_zero_group);
2313 ff_zero_group = NULL;
2314 }
Tom Haynesd67ae822014-12-11 17:02:04 -05002315}
2316
2317MODULE_ALIAS("nfs-layouttype4-4");
2318
2319MODULE_LICENSE("GPL");
2320MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
2321
2322module_init(nfs4flexfilelayout_init);
2323module_exit(nfs4flexfilelayout_exit);