blob: a7aeb7407252ee126525b296783ff81da29e8e42 [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
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 */
Tom Haynes602c4cd2016-05-25 07:31:13 -0700833 if (!pgio->pg_lseg || ff_layout_avoid_read_on_rw(pgio->pg_lseg)) {
834 pnfs_put_lseg(pgio->pg_lseg);
Tom Haynesd67ae822014-12-11 17:02:04 -0500835 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
836 req->wb_context,
837 0,
838 NFS4_MAX_UINT64,
839 IOMODE_READ,
840 GFP_KERNEL);
Peng Taod600ad12015-12-04 02:57:48 +0800841 if (IS_ERR(pgio->pg_lseg)) {
842 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
843 pgio->pg_lseg = NULL;
Peng Taod600ad12015-12-04 02:57:48 +0800844 }
845 }
Tom Haynes602c4cd2016-05-25 07:31:13 -0700846
Tom Haynesd67ae822014-12-11 17:02:04 -0500847 /* If no lseg, fall back to read through mds */
848 if (pgio->pg_lseg == NULL)
849 goto out_mds;
850
Trond Myklebust13544412015-11-02 10:11:17 -0500851 ds = ff_layout_choose_best_ds_for_read(pgio->pg_lseg, 0, &ds_idx);
Tom Haynes446ca212016-05-17 12:28:38 -0400852 if (!ds) {
853 if (ff_layout_no_fallback_to_mds(pgio->pg_lseg))
854 goto out_pnfs;
855 else
856 goto out_mds;
857 }
858
Tom Haynesd67ae822014-12-11 17:02:04 -0500859 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx);
860
861 pgio->pg_mirror_idx = ds_idx;
862
863 /* read always uses only one mirror - idx 0 for pgio layer */
864 pgm = &pgio->pg_mirrors[0];
865 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize;
866
867 return;
868out_mds:
869 pnfs_put_lseg(pgio->pg_lseg);
870 pgio->pg_lseg = NULL;
871 nfs_pageio_reset_read_mds(pgio);
Tom Haynes446ca212016-05-17 12:28:38 -0400872 return;
873
874out_pnfs:
875 pnfs_set_lo_fail(pgio->pg_lseg);
876 pnfs_put_lseg(pgio->pg_lseg);
877 pgio->pg_lseg = NULL;
Tom Haynesd67ae822014-12-11 17:02:04 -0500878}
879
880static void
881ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
882 struct nfs_page *req)
883{
884 struct nfs4_ff_layout_mirror *mirror;
885 struct nfs_pgio_mirror *pgm;
886 struct nfs_commit_info cinfo;
887 struct nfs4_pnfs_ds *ds;
888 int i;
889 int status;
890
Peng Taod600ad12015-12-04 02:57:48 +0800891 if (!pgio->pg_lseg) {
Tom Haynesd67ae822014-12-11 17:02:04 -0500892 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
893 req->wb_context,
894 0,
895 NFS4_MAX_UINT64,
896 IOMODE_RW,
897 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +0800898 if (IS_ERR(pgio->pg_lseg)) {
899 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
900 pgio->pg_lseg = NULL;
901 return;
902 }
903 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500904 /* If no lseg, fall back to write through mds */
905 if (pgio->pg_lseg == NULL)
906 goto out_mds;
907
908 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
909 status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
910 if (status < 0)
911 goto out_mds;
912
913 /* Use a direct mapping of ds_idx to pgio mirror_idx */
914 if (WARN_ON_ONCE(pgio->pg_mirror_count !=
915 FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg)))
916 goto out_mds;
917
918 for (i = 0; i < pgio->pg_mirror_count; i++) {
919 ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true);
Tom Haynes446ca212016-05-17 12:28:38 -0400920 if (!ds) {
921 if (ff_layout_no_fallback_to_mds(pgio->pg_lseg))
922 goto out_pnfs;
923 else
924 goto out_mds;
925 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500926 pgm = &pgio->pg_mirrors[i];
927 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i);
928 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize;
929 }
930
931 return;
932
933out_mds:
934 pnfs_put_lseg(pgio->pg_lseg);
935 pgio->pg_lseg = NULL;
936 nfs_pageio_reset_write_mds(pgio);
Tom Haynes446ca212016-05-17 12:28:38 -0400937 return;
938
939out_pnfs:
940 pnfs_set_lo_fail(pgio->pg_lseg);
941 pnfs_put_lseg(pgio->pg_lseg);
942 pgio->pg_lseg = NULL;
Tom Haynesd67ae822014-12-11 17:02:04 -0500943}
944
945static unsigned int
946ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
947 struct nfs_page *req)
948{
Peng Taod600ad12015-12-04 02:57:48 +0800949 if (!pgio->pg_lseg) {
Tom Haynesd67ae822014-12-11 17:02:04 -0500950 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
951 req->wb_context,
952 0,
953 NFS4_MAX_UINT64,
954 IOMODE_RW,
955 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +0800956 if (IS_ERR(pgio->pg_lseg)) {
957 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
958 pgio->pg_lseg = NULL;
959 goto out;
960 }
961 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500962 if (pgio->pg_lseg)
963 return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
964
965 /* no lseg means that pnfs is not in use, so no mirroring here */
Tom Haynesd67ae822014-12-11 17:02:04 -0500966 nfs_pageio_reset_write_mds(pgio);
Peng Taod600ad12015-12-04 02:57:48 +0800967out:
Tom Haynesd67ae822014-12-11 17:02:04 -0500968 return 1;
969}
970
971static const struct nfs_pageio_ops ff_layout_pg_read_ops = {
972 .pg_init = ff_layout_pg_init_read,
973 .pg_test = pnfs_generic_pg_test,
974 .pg_doio = pnfs_generic_pg_readpages,
975 .pg_cleanup = pnfs_generic_pg_cleanup,
976};
977
978static const struct nfs_pageio_ops ff_layout_pg_write_ops = {
979 .pg_init = ff_layout_pg_init_write,
980 .pg_test = pnfs_generic_pg_test,
981 .pg_doio = pnfs_generic_pg_writepages,
982 .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write,
983 .pg_cleanup = pnfs_generic_pg_cleanup,
984};
985
986static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs)
987{
988 struct rpc_task *task = &hdr->task;
989
990 pnfs_layoutcommit_inode(hdr->inode, false);
991
992 if (retry_pnfs) {
993 dprintk("%s Reset task %5u for i/o through pNFS "
994 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
995 hdr->task.tk_pid,
996 hdr->inode->i_sb->s_id,
997 (unsigned long long)NFS_FILEID(hdr->inode),
998 hdr->args.count,
999 (unsigned long long)hdr->args.offset);
1000
Trond Myklebustdc602dd2015-12-31 11:44:06 -05001001 hdr->completion_ops->reschedule_io(hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001002 return;
1003 }
1004
1005 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1006 dprintk("%s Reset task %5u for i/o through MDS "
1007 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1008 hdr->task.tk_pid,
1009 hdr->inode->i_sb->s_id,
1010 (unsigned long long)NFS_FILEID(hdr->inode),
1011 hdr->args.count,
1012 (unsigned long long)hdr->args.offset);
1013
1014 task->tk_status = pnfs_write_done_resend_to_mds(hdr);
1015 }
1016}
1017
1018static void ff_layout_reset_read(struct nfs_pgio_header *hdr)
1019{
1020 struct rpc_task *task = &hdr->task;
1021
1022 pnfs_layoutcommit_inode(hdr->inode, false);
1023
1024 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1025 dprintk("%s Reset task %5u for i/o through MDS "
1026 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1027 hdr->task.tk_pid,
1028 hdr->inode->i_sb->s_id,
1029 (unsigned long long)NFS_FILEID(hdr->inode),
1030 hdr->args.count,
1031 (unsigned long long)hdr->args.offset);
1032
1033 task->tk_status = pnfs_read_done_resend_to_mds(hdr);
1034 }
1035}
1036
1037static int ff_layout_async_handle_error_v4(struct rpc_task *task,
1038 struct nfs4_state *state,
1039 struct nfs_client *clp,
1040 struct pnfs_layout_segment *lseg,
1041 int idx)
1042{
1043 struct pnfs_layout_hdr *lo = lseg->pls_layout;
1044 struct inode *inode = lo->plh_inode;
1045 struct nfs_server *mds_server = NFS_SERVER(inode);
1046
1047 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1048 struct nfs_client *mds_client = mds_server->nfs_client;
1049 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
1050
1051 if (task->tk_status >= 0)
1052 return 0;
1053
1054 switch (task->tk_status) {
1055 /* MDS state errors */
1056 case -NFS4ERR_DELEG_REVOKED:
1057 case -NFS4ERR_ADMIN_REVOKED:
1058 case -NFS4ERR_BAD_STATEID:
1059 if (state == NULL)
1060 break;
1061 nfs_remove_bad_delegation(state->inode);
1062 case -NFS4ERR_OPENMODE:
1063 if (state == NULL)
1064 break;
1065 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
1066 goto out_bad_stateid;
1067 goto wait_on_recovery;
1068 case -NFS4ERR_EXPIRED:
1069 if (state != NULL) {
1070 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
1071 goto out_bad_stateid;
1072 }
1073 nfs4_schedule_lease_recovery(mds_client);
1074 goto wait_on_recovery;
1075 /* DS session errors */
1076 case -NFS4ERR_BADSESSION:
1077 case -NFS4ERR_BADSLOT:
1078 case -NFS4ERR_BAD_HIGH_SLOT:
1079 case -NFS4ERR_DEADSESSION:
1080 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1081 case -NFS4ERR_SEQ_FALSE_RETRY:
1082 case -NFS4ERR_SEQ_MISORDERED:
1083 dprintk("%s ERROR %d, Reset session. Exchangeid "
1084 "flags 0x%x\n", __func__, task->tk_status,
1085 clp->cl_exchange_flags);
1086 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
1087 break;
1088 case -NFS4ERR_DELAY:
1089 case -NFS4ERR_GRACE:
1090 rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX);
1091 break;
1092 case -NFS4ERR_RETRY_UNCACHED_REP:
1093 break;
1094 /* Invalidate Layout errors */
1095 case -NFS4ERR_PNFS_NO_LAYOUT:
1096 case -ESTALE: /* mapped NFS4ERR_STALE */
1097 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
1098 case -EISDIR: /* mapped NFS4ERR_ISDIR */
1099 case -NFS4ERR_FHEXPIRED:
1100 case -NFS4ERR_WRONG_TYPE:
1101 dprintk("%s Invalid layout error %d\n", __func__,
1102 task->tk_status);
1103 /*
1104 * Destroy layout so new i/o will get a new layout.
1105 * Layout will not be destroyed until all current lseg
1106 * references are put. Mark layout as invalid to resend failed
1107 * i/o and all i/o waiting on the slot table to the MDS until
1108 * layout is destroyed and a new valid layout is obtained.
1109 */
1110 pnfs_destroy_layout(NFS_I(inode));
1111 rpc_wake_up(&tbl->slot_tbl_waitq);
1112 goto reset;
1113 /* RPC connection errors */
1114 case -ECONNREFUSED:
1115 case -EHOSTDOWN:
1116 case -EHOSTUNREACH:
1117 case -ENETUNREACH:
1118 case -EIO:
1119 case -ETIMEDOUT:
1120 case -EPIPE:
1121 dprintk("%s DS connection error %d\n", __func__,
1122 task->tk_status);
1123 nfs4_mark_deviceid_unavailable(devid);
1124 rpc_wake_up(&tbl->slot_tbl_waitq);
1125 /* fall through */
1126 default:
Tom Haynes3b13b4b2016-05-17 12:28:37 -04001127 if (ff_layout_avoid_mds_available_ds(lseg))
Tom Haynesd67ae822014-12-11 17:02:04 -05001128 return -NFS4ERR_RESET_TO_PNFS;
1129reset:
1130 dprintk("%s Retry through MDS. Error %d\n", __func__,
1131 task->tk_status);
1132 return -NFS4ERR_RESET_TO_MDS;
1133 }
1134out:
1135 task->tk_status = 0;
1136 return -EAGAIN;
1137out_bad_stateid:
1138 task->tk_status = -EIO;
1139 return 0;
1140wait_on_recovery:
1141 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
1142 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
1143 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
1144 goto out;
1145}
1146
1147/* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
1148static int ff_layout_async_handle_error_v3(struct rpc_task *task,
1149 struct pnfs_layout_segment *lseg,
1150 int idx)
1151{
1152 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1153
1154 if (task->tk_status >= 0)
1155 return 0;
1156
Trond Myklebust90816d12015-08-30 09:53:06 -07001157 switch (task->tk_status) {
1158 /* File access problems. Don't mark the device as unavailable */
1159 case -EACCES:
1160 case -ESTALE:
1161 case -EISDIR:
1162 case -EBADHANDLE:
1163 case -ELOOP:
1164 case -ENOSPC:
1165 break;
1166 case -EJUKEBOX:
1167 nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY);
1168 goto out_retry;
1169 default:
Tom Haynesd67ae822014-12-11 17:02:04 -05001170 dprintk("%s DS connection error %d\n", __func__,
1171 task->tk_status);
1172 nfs4_mark_deviceid_unavailable(devid);
Tom Haynesd67ae822014-12-11 17:02:04 -05001173 }
Trond Myklebust90816d12015-08-30 09:53:06 -07001174 /* FIXME: Need to prevent infinite looping here. */
1175 return -NFS4ERR_RESET_TO_PNFS;
1176out_retry:
Tom Haynesd67ae822014-12-11 17:02:04 -05001177 task->tk_status = 0;
Trond Myklebust7eeea162015-12-17 09:22:15 -05001178 rpc_restart_call_prepare(task);
Tom Haynesd67ae822014-12-11 17:02:04 -05001179 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
1180 return -EAGAIN;
1181}
1182
1183static int ff_layout_async_handle_error(struct rpc_task *task,
1184 struct nfs4_state *state,
1185 struct nfs_client *clp,
1186 struct pnfs_layout_segment *lseg,
1187 int idx)
1188{
1189 int vers = clp->cl_nfs_mod->rpc_vers->number;
1190
1191 switch (vers) {
1192 case 3:
1193 return ff_layout_async_handle_error_v3(task, lseg, idx);
1194 case 4:
1195 return ff_layout_async_handle_error_v4(task, state, clp,
1196 lseg, idx);
1197 default:
1198 /* should never happen */
1199 WARN_ON_ONCE(1);
1200 return 0;
1201 }
1202}
1203
1204static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
1205 int idx, u64 offset, u64 length,
Trond Myklebustdd521282015-08-20 17:59:49 -05001206 u32 status, int opnum, int error)
Tom Haynesd67ae822014-12-11 17:02:04 -05001207{
1208 struct nfs4_ff_layout_mirror *mirror;
1209 int err;
1210
Trond Myklebustdd521282015-08-20 17:59:49 -05001211 if (status == 0) {
1212 switch (error) {
1213 case -ETIMEDOUT:
1214 case -EPFNOSUPPORT:
1215 case -EPROTONOSUPPORT:
1216 case -EOPNOTSUPP:
1217 case -ECONNREFUSED:
1218 case -ECONNRESET:
1219 case -EHOSTDOWN:
1220 case -EHOSTUNREACH:
1221 case -ENETUNREACH:
1222 case -EADDRINUSE:
1223 case -ENOBUFS:
1224 case -EPIPE:
1225 case -EPERM:
1226 status = NFS4ERR_NXIO;
1227 break;
1228 case -EACCES:
1229 status = NFS4ERR_ACCESS;
1230 break;
1231 default:
1232 return;
1233 }
1234 }
1235
Peng Taoc22eeb82015-12-07 17:31:38 -05001236 switch (status) {
1237 case NFS4ERR_DELAY:
1238 case NFS4ERR_GRACE:
1239 return;
1240 default:
1241 break;
1242 }
1243
Tom Haynesd67ae822014-12-11 17:02:04 -05001244 mirror = FF_LAYOUT_COMP(lseg, idx);
1245 err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
1246 mirror, offset, length, status, opnum,
1247 GFP_NOIO);
Trond Myklebustf95c03b2015-09-02 15:15:11 -07001248 pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg);
Tom Haynesd67ae822014-12-11 17:02:04 -05001249 dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status);
1250}
1251
1252/* NFS_PROTO call done callback routines */
Tom Haynesd67ae822014-12-11 17:02:04 -05001253static int ff_layout_read_done_cb(struct rpc_task *task,
1254 struct nfs_pgio_header *hdr)
1255{
Tom Haynesd67ae822014-12-11 17:02:04 -05001256 int err;
1257
1258 trace_nfs4_pnfs_read(hdr, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001259 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001260 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1261 hdr->args.offset, hdr->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001262 hdr->res.op_status, OP_READ,
1263 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001264 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1265 hdr->ds_clp, hdr->lseg,
1266 hdr->pgio_mirror_idx);
1267
1268 switch (err) {
1269 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust13544412015-11-02 10:11:17 -05001270 if (ff_layout_choose_best_ds_for_read(hdr->lseg,
1271 hdr->pgio_mirror_idx + 1,
1272 &hdr->pgio_mirror_idx))
1273 goto out_eagain;
Tom Haynesd67ae822014-12-11 17:02:04 -05001274 pnfs_read_resend_pnfs(hdr);
1275 return task->tk_status;
1276 case -NFS4ERR_RESET_TO_MDS:
Tom Haynesd67ae822014-12-11 17:02:04 -05001277 ff_layout_reset_read(hdr);
1278 return task->tk_status;
1279 case -EAGAIN:
Trond Myklebust13544412015-11-02 10:11:17 -05001280 goto out_eagain;
Tom Haynesd67ae822014-12-11 17:02:04 -05001281 }
1282
1283 return 0;
Trond Myklebust13544412015-11-02 10:11:17 -05001284out_eagain:
1285 rpc_restart_call_prepare(task);
1286 return -EAGAIN;
Tom Haynesd67ae822014-12-11 17:02:04 -05001287}
1288
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001289static bool
1290ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg)
1291{
1292 return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT);
1293}
1294
Tom Haynesd67ae822014-12-11 17:02:04 -05001295/*
1296 * We reference the rpc_cred of the first WRITE that triggers the need for
1297 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
1298 * rfc5661 is not clear about which credential should be used.
1299 *
1300 * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
1301 * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
1302 * we always send layoutcommit after DS writes.
1303 */
1304static void
1305ff_layout_set_layoutcommit(struct nfs_pgio_header *hdr)
1306{
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001307 if (!ff_layout_need_layoutcommit(hdr->lseg))
1308 return;
1309
Trond Myklebust67af7612015-03-25 20:40:38 -04001310 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
1311 hdr->mds_offset + hdr->res.count);
Tom Haynesd67ae822014-12-11 17:02:04 -05001312 dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
1313 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
1314}
1315
1316static bool
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001317ff_layout_device_unavailable(struct pnfs_layout_segment *lseg, int idx)
Tom Haynesd67ae822014-12-11 17:02:04 -05001318{
1319 /* No mirroring for now */
1320 struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx);
1321
1322 return ff_layout_test_devid_unavailable(node);
1323}
1324
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001325static void ff_layout_read_record_layoutstats_start(struct rpc_task *task,
1326 struct nfs_pgio_header *hdr)
Tom Haynesd67ae822014-12-11 17:02:04 -05001327{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001328 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1329 return;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04001330 nfs4_ff_layout_stat_io_start_read(hdr->inode,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001331 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
Trond Myklebuste76d28d2015-08-20 13:12:51 -05001332 hdr->args.count,
1333 task->tk_start);
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001334}
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001335
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001336static void ff_layout_read_record_layoutstats_done(struct rpc_task *task,
1337 struct nfs_pgio_header *hdr)
1338{
1339 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1340 return;
1341 nfs4_ff_layout_stat_io_end_read(task,
1342 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1343 hdr->args.count,
1344 hdr->res.count);
1345}
1346
1347static int ff_layout_read_prepare_common(struct rpc_task *task,
1348 struct nfs_pgio_header *hdr)
1349{
Tom Haynesd67ae822014-12-11 17:02:04 -05001350 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1351 rpc_exit(task, -EIO);
1352 return -EIO;
1353 }
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001354 if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) {
1355 rpc_exit(task, -EHOSTDOWN);
Tom Haynesd67ae822014-12-11 17:02:04 -05001356 return -EAGAIN;
1357 }
Tom Haynesd67ae822014-12-11 17:02:04 -05001358
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001359 ff_layout_read_record_layoutstats_start(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001360 return 0;
1361}
1362
1363/*
1364 * Call ops for the async read/write cases
1365 * In the case of dense layouts, the offset needs to be reset to its
1366 * original value.
1367 */
1368static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data)
1369{
1370 struct nfs_pgio_header *hdr = data;
1371
1372 if (ff_layout_read_prepare_common(task, hdr))
1373 return;
1374
1375 rpc_call_start(task);
1376}
1377
1378static int ff_layout_setup_sequence(struct nfs_client *ds_clp,
1379 struct nfs4_sequence_args *args,
1380 struct nfs4_sequence_res *res,
1381 struct rpc_task *task)
1382{
1383 if (ds_clp->cl_session)
1384 return nfs41_setup_sequence(ds_clp->cl_session,
1385 args,
1386 res,
1387 task);
1388 return nfs40_setup_sequence(ds_clp->cl_slot_tbl,
1389 args,
1390 res,
1391 task);
1392}
1393
1394static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data)
1395{
1396 struct nfs_pgio_header *hdr = data;
1397
Tom Haynesd67ae822014-12-11 17:02:04 -05001398 if (ff_layout_setup_sequence(hdr->ds_clp,
1399 &hdr->args.seq_args,
1400 &hdr->res.seq_res,
1401 task))
1402 return;
1403
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001404 if (ff_layout_read_prepare_common(task, hdr))
1405 return;
1406
Tom Haynesd67ae822014-12-11 17:02:04 -05001407 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1408 hdr->args.lock_context, FMODE_READ) == -EIO)
1409 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1410}
1411
1412static void ff_layout_read_call_done(struct rpc_task *task, void *data)
1413{
1414 struct nfs_pgio_header *hdr = data;
1415
1416 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
1417
1418 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1419 task->tk_status == 0) {
1420 nfs4_sequence_done(task, &hdr->res.seq_res);
1421 return;
1422 }
1423
1424 /* Note this may cause RPC to be resent */
1425 hdr->mds_ops->rpc_call_done(task, hdr);
1426}
1427
1428static void ff_layout_read_count_stats(struct rpc_task *task, void *data)
1429{
1430 struct nfs_pgio_header *hdr = data;
1431
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001432 ff_layout_read_record_layoutstats_done(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001433 rpc_count_iostats_metrics(task,
1434 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]);
1435}
1436
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001437static void ff_layout_read_release(void *data)
1438{
1439 struct nfs_pgio_header *hdr = data;
1440
1441 ff_layout_read_record_layoutstats_done(&hdr->task, hdr);
1442 pnfs_generic_rw_release(data);
1443}
1444
1445
Tom Haynesd67ae822014-12-11 17:02:04 -05001446static int ff_layout_write_done_cb(struct rpc_task *task,
1447 struct nfs_pgio_header *hdr)
1448{
Tom Haynesd67ae822014-12-11 17:02:04 -05001449 int err;
1450
1451 trace_nfs4_pnfs_write(hdr, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001452 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001453 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1454 hdr->args.offset, hdr->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001455 hdr->res.op_status, OP_WRITE,
1456 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001457 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1458 hdr->ds_clp, hdr->lseg,
1459 hdr->pgio_mirror_idx);
1460
1461 switch (err) {
1462 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001463 ff_layout_reset_write(hdr, true);
1464 return task->tk_status;
Tom Haynesd67ae822014-12-11 17:02:04 -05001465 case -NFS4ERR_RESET_TO_MDS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001466 ff_layout_reset_write(hdr, false);
Tom Haynesd67ae822014-12-11 17:02:04 -05001467 return task->tk_status;
1468 case -EAGAIN:
Tom Haynesd67ae822014-12-11 17:02:04 -05001469 return -EAGAIN;
1470 }
1471
1472 if (hdr->res.verf->committed == NFS_FILE_SYNC ||
1473 hdr->res.verf->committed == NFS_DATA_SYNC)
1474 ff_layout_set_layoutcommit(hdr);
1475
Peng Tao54204012015-08-22 06:40:00 +08001476 /* zero out fattr since we don't care DS attr at all */
1477 hdr->fattr.valid = 0;
Peng Tao69f230d2015-08-20 01:52:59 +08001478 if (task->tk_status >= 0)
1479 nfs_writeback_update_inode(hdr);
1480
Tom Haynesd67ae822014-12-11 17:02:04 -05001481 return 0;
1482}
1483
1484static int ff_layout_commit_done_cb(struct rpc_task *task,
1485 struct nfs_commit_data *data)
1486{
Tom Haynesd67ae822014-12-11 17:02:04 -05001487 int err;
1488
1489 trace_nfs4_pnfs_commit_ds(data, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001490 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001491 ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index,
1492 data->args.offset, data->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001493 data->res.op_status, OP_COMMIT,
1494 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001495 err = ff_layout_async_handle_error(task, NULL, data->ds_clp,
1496 data->lseg, data->ds_commit_index);
1497
1498 switch (err) {
1499 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001500 pnfs_generic_prepare_to_resend_writes(data);
1501 return -EAGAIN;
Tom Haynesd67ae822014-12-11 17:02:04 -05001502 case -NFS4ERR_RESET_TO_MDS:
Tom Haynesd67ae822014-12-11 17:02:04 -05001503 pnfs_generic_prepare_to_resend_writes(data);
1504 return -EAGAIN;
1505 case -EAGAIN:
1506 rpc_restart_call_prepare(task);
1507 return -EAGAIN;
1508 }
1509
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001510 if (data->verf.committed == NFS_UNSTABLE
1511 && ff_layout_need_layoutcommit(data->lseg))
Trond Myklebust67af7612015-03-25 20:40:38 -04001512 pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb);
Tom Haynesd67ae822014-12-11 17:02:04 -05001513
1514 return 0;
1515}
1516
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001517static void ff_layout_write_record_layoutstats_start(struct rpc_task *task,
1518 struct nfs_pgio_header *hdr)
Tom Haynesd67ae822014-12-11 17:02:04 -05001519{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001520 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1521 return;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04001522 nfs4_ff_layout_stat_io_start_write(hdr->inode,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001523 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
Trond Myklebuste76d28d2015-08-20 13:12:51 -05001524 hdr->args.count,
1525 task->tk_start);
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001526}
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001527
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001528static void ff_layout_write_record_layoutstats_done(struct rpc_task *task,
1529 struct nfs_pgio_header *hdr)
1530{
1531 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1532 return;
1533 nfs4_ff_layout_stat_io_end_write(task,
1534 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1535 hdr->args.count, hdr->res.count,
1536 hdr->res.verf->committed);
1537}
1538
1539static int ff_layout_write_prepare_common(struct rpc_task *task,
1540 struct nfs_pgio_header *hdr)
1541{
Tom Haynesd67ae822014-12-11 17:02:04 -05001542 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1543 rpc_exit(task, -EIO);
1544 return -EIO;
1545 }
1546
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001547 if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) {
1548 rpc_exit(task, -EHOSTDOWN);
Tom Haynesd67ae822014-12-11 17:02:04 -05001549 return -EAGAIN;
1550 }
1551
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001552 ff_layout_write_record_layoutstats_start(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001553 return 0;
1554}
1555
1556static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data)
1557{
1558 struct nfs_pgio_header *hdr = data;
1559
1560 if (ff_layout_write_prepare_common(task, hdr))
1561 return;
1562
1563 rpc_call_start(task);
1564}
1565
1566static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data)
1567{
1568 struct nfs_pgio_header *hdr = data;
1569
Tom Haynesd67ae822014-12-11 17:02:04 -05001570 if (ff_layout_setup_sequence(hdr->ds_clp,
1571 &hdr->args.seq_args,
1572 &hdr->res.seq_res,
1573 task))
1574 return;
1575
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001576 if (ff_layout_write_prepare_common(task, hdr))
1577 return;
1578
Tom Haynesd67ae822014-12-11 17:02:04 -05001579 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1580 hdr->args.lock_context, FMODE_WRITE) == -EIO)
1581 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1582}
1583
1584static void ff_layout_write_call_done(struct rpc_task *task, void *data)
1585{
1586 struct nfs_pgio_header *hdr = data;
1587
1588 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1589 task->tk_status == 0) {
1590 nfs4_sequence_done(task, &hdr->res.seq_res);
1591 return;
1592 }
1593
1594 /* Note this may cause RPC to be resent */
1595 hdr->mds_ops->rpc_call_done(task, hdr);
1596}
1597
1598static void ff_layout_write_count_stats(struct rpc_task *task, void *data)
1599{
1600 struct nfs_pgio_header *hdr = data;
1601
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001602 ff_layout_write_record_layoutstats_done(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001603 rpc_count_iostats_metrics(task,
1604 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]);
1605}
1606
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001607static void ff_layout_write_release(void *data)
1608{
1609 struct nfs_pgio_header *hdr = data;
1610
1611 ff_layout_write_record_layoutstats_done(&hdr->task, hdr);
1612 pnfs_generic_rw_release(data);
1613}
1614
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001615static void ff_layout_commit_record_layoutstats_start(struct rpc_task *task,
1616 struct nfs_commit_data *cdata)
1617{
1618 if (test_and_set_bit(NFS_IOHDR_STAT, &cdata->flags))
1619 return;
1620 nfs4_ff_layout_stat_io_start_write(cdata->inode,
1621 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1622 0, task->tk_start);
1623}
1624
1625static void ff_layout_commit_record_layoutstats_done(struct rpc_task *task,
1626 struct nfs_commit_data *cdata)
1627{
1628 struct nfs_page *req;
1629 __u64 count = 0;
1630
1631 if (!test_and_clear_bit(NFS_IOHDR_STAT, &cdata->flags))
1632 return;
1633
1634 if (task->tk_status == 0) {
1635 list_for_each_entry(req, &cdata->pages, wb_list)
1636 count += req->wb_bytes;
1637 }
1638 nfs4_ff_layout_stat_io_end_write(task,
1639 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1640 count, count, NFS_FILE_SYNC);
1641}
1642
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001643static void ff_layout_commit_prepare_common(struct rpc_task *task,
1644 struct nfs_commit_data *cdata)
1645{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001646 ff_layout_commit_record_layoutstats_start(task, cdata);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001647}
1648
Tom Haynesd67ae822014-12-11 17:02:04 -05001649static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data)
1650{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001651 ff_layout_commit_prepare_common(task, data);
Tom Haynesd67ae822014-12-11 17:02:04 -05001652 rpc_call_start(task);
1653}
1654
1655static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data)
1656{
1657 struct nfs_commit_data *wdata = data;
1658
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001659 if (ff_layout_setup_sequence(wdata->ds_clp,
Tom Haynesd67ae822014-12-11 17:02:04 -05001660 &wdata->args.seq_args,
1661 &wdata->res.seq_res,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001662 task))
1663 return;
1664 ff_layout_commit_prepare_common(task, data);
1665}
1666
1667static void ff_layout_commit_done(struct rpc_task *task, void *data)
1668{
Peng Tao141b9b52015-12-07 18:33:14 -05001669 pnfs_generic_write_commit_done(task, data);
1670}
1671
1672static void ff_layout_commit_count_stats(struct rpc_task *task, void *data)
1673{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001674 struct nfs_commit_data *cdata = data;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001675
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001676 ff_layout_commit_record_layoutstats_done(task, cdata);
Tom Haynesd67ae822014-12-11 17:02:04 -05001677 rpc_count_iostats_metrics(task,
1678 &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]);
1679}
1680
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001681static void ff_layout_commit_release(void *data)
1682{
1683 struct nfs_commit_data *cdata = data;
1684
1685 ff_layout_commit_record_layoutstats_done(&cdata->task, cdata);
1686 pnfs_generic_commit_release(data);
1687}
1688
Tom Haynesd67ae822014-12-11 17:02:04 -05001689static const struct rpc_call_ops ff_layout_read_call_ops_v3 = {
1690 .rpc_call_prepare = ff_layout_read_prepare_v3,
1691 .rpc_call_done = ff_layout_read_call_done,
1692 .rpc_count_stats = ff_layout_read_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001693 .rpc_release = ff_layout_read_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001694};
1695
1696static const struct rpc_call_ops ff_layout_read_call_ops_v4 = {
1697 .rpc_call_prepare = ff_layout_read_prepare_v4,
1698 .rpc_call_done = ff_layout_read_call_done,
1699 .rpc_count_stats = ff_layout_read_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001700 .rpc_release = ff_layout_read_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001701};
1702
1703static const struct rpc_call_ops ff_layout_write_call_ops_v3 = {
1704 .rpc_call_prepare = ff_layout_write_prepare_v3,
1705 .rpc_call_done = ff_layout_write_call_done,
1706 .rpc_count_stats = ff_layout_write_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001707 .rpc_release = ff_layout_write_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001708};
1709
1710static const struct rpc_call_ops ff_layout_write_call_ops_v4 = {
1711 .rpc_call_prepare = ff_layout_write_prepare_v4,
1712 .rpc_call_done = ff_layout_write_call_done,
1713 .rpc_count_stats = ff_layout_write_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001714 .rpc_release = ff_layout_write_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001715};
1716
1717static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = {
1718 .rpc_call_prepare = ff_layout_commit_prepare_v3,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001719 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001720 .rpc_count_stats = ff_layout_commit_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001721 .rpc_release = ff_layout_commit_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001722};
1723
1724static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = {
1725 .rpc_call_prepare = ff_layout_commit_prepare_v4,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001726 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001727 .rpc_count_stats = ff_layout_commit_count_stats,
Trond Myklebust4d0ac222015-12-22 15:32:00 -05001728 .rpc_release = ff_layout_commit_release,
Tom Haynesd67ae822014-12-11 17:02:04 -05001729};
1730
1731static enum pnfs_try_status
1732ff_layout_read_pagelist(struct nfs_pgio_header *hdr)
1733{
1734 struct pnfs_layout_segment *lseg = hdr->lseg;
1735 struct nfs4_pnfs_ds *ds;
1736 struct rpc_clnt *ds_clnt;
1737 struct rpc_cred *ds_cred;
1738 loff_t offset = hdr->args.offset;
1739 u32 idx = hdr->pgio_mirror_idx;
1740 int vers;
1741 struct nfs_fh *fh;
1742
1743 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
1744 __func__, hdr->inode->i_ino,
1745 hdr->args.pgbase, (size_t)hdr->args.count, offset);
1746
1747 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
1748 if (!ds)
1749 goto out_failed;
1750
1751 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1752 hdr->inode);
1753 if (IS_ERR(ds_clnt))
1754 goto out_failed;
1755
1756 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001757 if (!ds_cred)
Tom Haynesd67ae822014-12-11 17:02:04 -05001758 goto out_failed;
1759
1760 vers = nfs4_ff_layout_ds_version(lseg, idx);
1761
1762 dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__,
1763 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers);
1764
Trond Myklebust75bf47e2016-05-17 12:28:36 -04001765 hdr->pgio_done_cb = ff_layout_read_done_cb;
Tom Haynesd67ae822014-12-11 17:02:04 -05001766 atomic_inc(&ds->ds_clp->cl_count);
1767 hdr->ds_clp = ds->ds_clp;
1768 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1769 if (fh)
1770 hdr->args.fh = fh;
Tom Haynesd67ae822014-12-11 17:02:04 -05001771 /*
1772 * Note that if we ever decide to split across DSes,
1773 * then we may need to handle dense-like offsets.
1774 */
1775 hdr->args.offset = offset;
1776 hdr->mds_offset = offset;
1777
1778 /* Perform an asynchronous read to ds */
1779 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1780 vers == 3 ? &ff_layout_read_call_ops_v3 :
1781 &ff_layout_read_call_ops_v4,
1782 0, RPC_TASK_SOFTCONN);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001783 put_rpccred(ds_cred);
Tom Haynesd67ae822014-12-11 17:02:04 -05001784 return PNFS_ATTEMPTED;
1785
1786out_failed:
Tom Haynes3b13b4b2016-05-17 12:28:37 -04001787 if (ff_layout_avoid_mds_available_ds(lseg))
Tom Haynesd67ae822014-12-11 17:02:04 -05001788 return PNFS_TRY_AGAIN;
1789 return PNFS_NOT_ATTEMPTED;
1790}
1791
1792/* Perform async writes. */
1793static enum pnfs_try_status
1794ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
1795{
1796 struct pnfs_layout_segment *lseg = hdr->lseg;
1797 struct nfs4_pnfs_ds *ds;
1798 struct rpc_clnt *ds_clnt;
1799 struct rpc_cred *ds_cred;
1800 loff_t offset = hdr->args.offset;
1801 int vers;
1802 struct nfs_fh *fh;
1803 int idx = hdr->pgio_mirror_idx;
1804
1805 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1806 if (!ds)
1807 return PNFS_NOT_ATTEMPTED;
1808
1809 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1810 hdr->inode);
1811 if (IS_ERR(ds_clnt))
1812 return PNFS_NOT_ATTEMPTED;
1813
1814 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001815 if (!ds_cred)
Tom Haynesd67ae822014-12-11 17:02:04 -05001816 return PNFS_NOT_ATTEMPTED;
1817
1818 vers = nfs4_ff_layout_ds_version(lseg, idx);
1819
1820 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n",
1821 __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
1822 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count),
1823 vers);
1824
1825 hdr->pgio_done_cb = ff_layout_write_done_cb;
1826 atomic_inc(&ds->ds_clp->cl_count);
1827 hdr->ds_clp = ds->ds_clp;
1828 hdr->ds_commit_idx = idx;
1829 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1830 if (fh)
1831 hdr->args.fh = fh;
1832
1833 /*
1834 * Note that if we ever decide to split across DSes,
1835 * then we may need to handle dense-like offsets.
1836 */
1837 hdr->args.offset = offset;
1838
1839 /* Perform an asynchronous write */
1840 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1841 vers == 3 ? &ff_layout_write_call_ops_v3 :
1842 &ff_layout_write_call_ops_v4,
1843 sync, RPC_TASK_SOFTCONN);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001844 put_rpccred(ds_cred);
Tom Haynesd67ae822014-12-11 17:02:04 -05001845 return PNFS_ATTEMPTED;
1846}
1847
Tom Haynesd67ae822014-12-11 17:02:04 -05001848static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1849{
1850 return i;
1851}
1852
1853static struct nfs_fh *
1854select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1855{
1856 struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg);
1857
1858 /* FIXME: Assume that there is only one NFS version available
1859 * for the DS.
1860 */
1861 return &flseg->mirror_array[i]->fh_versions[0];
1862}
1863
1864static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how)
1865{
1866 struct pnfs_layout_segment *lseg = data->lseg;
1867 struct nfs4_pnfs_ds *ds;
1868 struct rpc_clnt *ds_clnt;
1869 struct rpc_cred *ds_cred;
1870 u32 idx;
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001871 int vers, ret;
Tom Haynesd67ae822014-12-11 17:02:04 -05001872 struct nfs_fh *fh;
1873
1874 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1875 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1876 if (!ds)
1877 goto out_err;
1878
1879 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1880 data->inode);
1881 if (IS_ERR(ds_clnt))
1882 goto out_err;
1883
1884 ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001885 if (!ds_cred)
Tom Haynesd67ae822014-12-11 17:02:04 -05001886 goto out_err;
1887
1888 vers = nfs4_ff_layout_ds_version(lseg, idx);
1889
1890 dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__,
1891 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count),
1892 vers);
1893 data->commit_done_cb = ff_layout_commit_done_cb;
1894 data->cred = ds_cred;
1895 atomic_inc(&ds->ds_clp->cl_count);
1896 data->ds_clp = ds->ds_clp;
1897 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1898 if (fh)
1899 data->args.fh = fh;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001900
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001901 ret = nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops,
Tom Haynesd67ae822014-12-11 17:02:04 -05001902 vers == 3 ? &ff_layout_commit_call_ops_v3 :
1903 &ff_layout_commit_call_ops_v4,
1904 how, RPC_TASK_SOFTCONN);
Jeff Layton57f3f4c2016-04-21 20:51:58 -04001905 put_rpccred(ds_cred);
1906 return ret;
Tom Haynesd67ae822014-12-11 17:02:04 -05001907out_err:
1908 pnfs_generic_prepare_to_resend_writes(data);
1909 pnfs_generic_commit_release(data);
1910 return -EAGAIN;
1911}
1912
1913static int
1914ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1915 int how, struct nfs_commit_info *cinfo)
1916{
1917 return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
1918 ff_layout_initiate_commit);
1919}
1920
1921static struct pnfs_ds_commit_info *
1922ff_layout_get_ds_info(struct inode *inode)
1923{
1924 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1925
1926 if (layout == NULL)
1927 return NULL;
1928
1929 return &FF_LAYOUT_FROM_HDR(layout)->commit_info;
1930}
1931
1932static void
Trond Myklebustfc877012015-03-09 17:25:14 -04001933ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d)
Tom Haynesd67ae822014-12-11 17:02:04 -05001934{
1935 nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds,
1936 id_node));
1937}
1938
1939static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo,
1940 struct xdr_stream *xdr,
1941 const struct nfs4_layoutreturn_args *args)
1942{
1943 struct pnfs_layout_hdr *hdr = &flo->generic_hdr;
1944 __be32 *start;
1945 int count = 0, ret = 0;
1946
1947 start = xdr_reserve_space(xdr, 4);
1948 if (unlikely(!start))
1949 return -E2BIG;
1950
1951 /* This assume we always return _ALL_ layouts */
1952 spin_lock(&hdr->plh_inode->i_lock);
1953 ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range);
1954 spin_unlock(&hdr->plh_inode->i_lock);
1955
1956 *start = cpu_to_be32(count);
1957
1958 return ret;
1959}
1960
1961/* report nothing for now */
1962static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo,
1963 struct xdr_stream *xdr,
1964 const struct nfs4_layoutreturn_args *args)
1965{
1966 __be32 *p;
1967
1968 p = xdr_reserve_space(xdr, 4);
1969 if (likely(p))
1970 *p = cpu_to_be32(0);
1971}
1972
1973static struct nfs4_deviceid_node *
1974ff_layout_alloc_deviceid_node(struct nfs_server *server,
1975 struct pnfs_device *pdev, gfp_t gfp_flags)
1976{
1977 struct nfs4_ff_layout_ds *dsaddr;
1978
1979 dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags);
1980 if (!dsaddr)
1981 return NULL;
1982 return &dsaddr->id_node;
1983}
1984
1985static void
1986ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo,
1987 struct xdr_stream *xdr,
1988 const struct nfs4_layoutreturn_args *args)
1989{
1990 struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
1991 __be32 *start;
1992
1993 dprintk("%s: Begin\n", __func__);
1994 start = xdr_reserve_space(xdr, 4);
1995 BUG_ON(!start);
1996
Trond Myklebust082fa372016-01-21 15:39:40 -05001997 ff_layout_encode_ioerr(flo, xdr, args);
Tom Haynesd67ae822014-12-11 17:02:04 -05001998 ff_layout_encode_iostats(flo, xdr, args);
Trond Myklebust082fa372016-01-21 15:39:40 -05001999
Tom Haynesd67ae822014-12-11 17:02:04 -05002000 *start = cpu_to_be32((xdr->p - start - 1) * 4);
2001 dprintk("%s: Return\n", __func__);
2002}
2003
Peng Tao27c43062015-06-23 19:52:02 +08002004static int
2005ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen)
2006{
2007 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
2008
2009 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
2010}
2011
2012static size_t
2013ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf,
2014 const int buflen)
2015{
2016 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
2017 const struct in6_addr *addr = &sin6->sin6_addr;
2018
2019 /*
2020 * RFC 4291, Section 2.2.2
2021 *
2022 * Shorthanded ANY address
2023 */
2024 if (ipv6_addr_any(addr))
2025 return snprintf(buf, buflen, "::");
2026
2027 /*
2028 * RFC 4291, Section 2.2.2
2029 *
2030 * Shorthanded loopback address
2031 */
2032 if (ipv6_addr_loopback(addr))
2033 return snprintf(buf, buflen, "::1");
2034
2035 /*
2036 * RFC 4291, Section 2.2.3
2037 *
2038 * Special presentation address format for mapped v4
2039 * addresses.
2040 */
2041 if (ipv6_addr_v4mapped(addr))
2042 return snprintf(buf, buflen, "::ffff:%pI4",
2043 &addr->s6_addr32[3]);
2044
2045 /*
2046 * RFC 4291, Section 2.2.1
2047 */
2048 return snprintf(buf, buflen, "%pI6c", addr);
2049}
2050
2051/* Derived from rpc_sockaddr2uaddr */
2052static void
2053ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da)
2054{
2055 struct sockaddr *sap = (struct sockaddr *)&da->da_addr;
2056 char portbuf[RPCBIND_MAXUADDRPLEN];
2057 char addrbuf[RPCBIND_MAXUADDRLEN];
2058 char *netid;
2059 unsigned short port;
2060 int len, netid_len;
2061 __be32 *p;
2062
2063 switch (sap->sa_family) {
2064 case AF_INET:
2065 if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
2066 return;
2067 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
2068 netid = "tcp";
2069 netid_len = 3;
2070 break;
2071 case AF_INET6:
2072 if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
2073 return;
2074 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
2075 netid = "tcp6";
2076 netid_len = 4;
2077 break;
2078 default:
2079 /* we only support tcp and tcp6 */
2080 WARN_ON_ONCE(1);
2081 return;
2082 }
2083
2084 snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff);
2085 len = strlcat(addrbuf, portbuf, sizeof(addrbuf));
2086
2087 p = xdr_reserve_space(xdr, 4 + netid_len);
2088 xdr_encode_opaque(p, netid, netid_len);
2089
2090 p = xdr_reserve_space(xdr, 4 + len);
2091 xdr_encode_opaque(p, addrbuf, len);
2092}
2093
2094static void
2095ff_layout_encode_nfstime(struct xdr_stream *xdr,
2096 ktime_t t)
2097{
2098 struct timespec64 ts;
2099 __be32 *p;
2100
2101 p = xdr_reserve_space(xdr, 12);
2102 ts = ktime_to_timespec64(t);
2103 p = xdr_encode_hyper(p, ts.tv_sec);
2104 *p++ = cpu_to_be32(ts.tv_nsec);
2105}
2106
2107static void
2108ff_layout_encode_io_latency(struct xdr_stream *xdr,
2109 struct nfs4_ff_io_stat *stat)
2110{
2111 __be32 *p;
2112
2113 p = xdr_reserve_space(xdr, 5 * 8);
2114 p = xdr_encode_hyper(p, stat->ops_requested);
2115 p = xdr_encode_hyper(p, stat->bytes_requested);
2116 p = xdr_encode_hyper(p, stat->ops_completed);
2117 p = xdr_encode_hyper(p, stat->bytes_completed);
2118 p = xdr_encode_hyper(p, stat->bytes_not_delivered);
2119 ff_layout_encode_nfstime(xdr, stat->total_busy_time);
2120 ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time);
2121}
2122
2123static void
2124ff_layout_encode_layoutstats(struct xdr_stream *xdr,
2125 struct nfs42_layoutstat_args *args,
2126 struct nfs42_layoutstat_devinfo *devinfo)
2127{
2128 struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private;
2129 struct nfs4_pnfs_ds_addr *da;
2130 struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds;
2131 struct nfs_fh *fh = &mirror->fh_versions[0];
2132 __be32 *p, *start;
2133
2134 da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node);
2135 dprintk("%s: DS %s: encoding address %s\n",
2136 __func__, ds->ds_remotestr, da->da_remotestr);
2137 /* layoutupdate length */
2138 start = xdr_reserve_space(xdr, 4);
2139 /* netaddr4 */
2140 ff_layout_encode_netaddr(xdr, da);
2141 /* nfs_fh4 */
2142 p = xdr_reserve_space(xdr, 4 + fh->size);
2143 xdr_encode_opaque(p, fh->data, fh->size);
2144 /* ff_io_latency4 read */
2145 spin_lock(&mirror->lock);
2146 ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat);
2147 /* ff_io_latency4 write */
2148 ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat);
2149 spin_unlock(&mirror->lock);
2150 /* nfstime4 */
2151 ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time));
2152 /* bool */
2153 p = xdr_reserve_space(xdr, 4);
2154 *p = cpu_to_be32(false);
2155
2156 *start = cpu_to_be32((xdr->p - start - 1) * 4);
2157}
2158
Trond Myklebust266d12d2015-08-24 20:03:17 -04002159static int
Peng Taoad4dc53e2015-06-23 19:52:01 +08002160ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args,
Trond Myklebust266d12d2015-08-24 20:03:17 -04002161 struct pnfs_layout_hdr *lo,
2162 int dev_limit)
Peng Taoad4dc53e2015-06-23 19:52:01 +08002163{
Trond Myklebust266d12d2015-08-24 20:03:17 -04002164 struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002165 struct nfs4_ff_layout_mirror *mirror;
2166 struct nfs4_deviceid_node *dev;
2167 struct nfs42_layoutstat_devinfo *devinfo;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002168 int i = 0;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002169
Trond Myklebust266d12d2015-08-24 20:03:17 -04002170 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2171 if (i >= dev_limit)
Peng Taoad4dc53e2015-06-23 19:52:01 +08002172 break;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002173 if (!mirror->mirror_ds)
Peng Tao27c43062015-06-23 19:52:02 +08002174 continue;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002175 /* mirror refcount put in cleanup_layoutstats */
2176 if (!atomic_inc_not_zero(&mirror->ref))
2177 continue;
2178 dev = &mirror->mirror_ds->id_node;
2179 devinfo = &args->devinfo[i];
Peng Taoad4dc53e2015-06-23 19:52:01 +08002180 memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002181 devinfo->offset = 0;
2182 devinfo->length = NFS4_MAX_UINT64;
Peng Taod099d7b2015-08-10 16:47:32 +08002183 devinfo->read_count = mirror->read_stat.io_stat.ops_completed;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002184 devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed;
Peng Taod099d7b2015-08-10 16:47:32 +08002185 devinfo->write_count = mirror->write_stat.io_stat.ops_completed;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002186 devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed;
2187 devinfo->layout_type = LAYOUT_FLEX_FILES;
Peng Tao27c43062015-06-23 19:52:02 +08002188 devinfo->layoutstats_encode = ff_layout_encode_layoutstats;
2189 devinfo->layout_private = mirror;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002190
Trond Myklebust266d12d2015-08-24 20:03:17 -04002191 i++;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002192 }
Trond Myklebust266d12d2015-08-24 20:03:17 -04002193 return i;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002194}
2195
2196static int
2197ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args)
2198{
Trond Myklebust266d12d2015-08-24 20:03:17 -04002199 struct nfs4_flexfile_layout *ff_layout;
2200 struct nfs4_ff_layout_mirror *mirror;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002201 int dev_count = 0;
2202
2203 spin_lock(&args->inode->i_lock);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002204 ff_layout = FF_LAYOUT_FROM_HDR(NFS_I(args->inode)->layout);
2205 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2206 if (atomic_read(&mirror->ref) != 0)
2207 dev_count ++;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002208 }
2209 spin_unlock(&args->inode->i_lock);
2210 /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
2211 if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) {
2212 dprintk("%s: truncating devinfo to limit (%d:%d)\n",
2213 __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV);
2214 dev_count = PNFS_LAYOUTSTATS_MAXDEV;
2215 }
Trond Myklebust266d12d2015-08-24 20:03:17 -04002216 args->devinfo = kmalloc_array(dev_count, sizeof(*args->devinfo), GFP_NOIO);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002217 if (!args->devinfo)
2218 return -ENOMEM;
2219
Peng Taoad4dc53e2015-06-23 19:52:01 +08002220 spin_lock(&args->inode->i_lock);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002221 args->num_dev = ff_layout_mirror_prepare_stats(args,
2222 &ff_layout->generic_hdr, dev_count);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002223 spin_unlock(&args->inode->i_lock);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002224
2225 return 0;
2226}
2227
Peng Tao27c43062015-06-23 19:52:02 +08002228static void
2229ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data)
2230{
2231 struct nfs4_ff_layout_mirror *mirror;
2232 int i;
2233
2234 for (i = 0; i < data->args.num_dev; i++) {
2235 mirror = data->args.devinfo[i].layout_private;
2236 data->args.devinfo[i].layout_private = NULL;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04002237 ff_layout_put_mirror(mirror);
Peng Tao27c43062015-06-23 19:52:02 +08002238 }
2239}
2240
Tom Haynesd67ae822014-12-11 17:02:04 -05002241static struct pnfs_layoutdriver_type flexfilelayout_type = {
2242 .id = LAYOUT_FLEX_FILES,
2243 .name = "LAYOUT_FLEX_FILES",
2244 .owner = THIS_MODULE,
2245 .alloc_layout_hdr = ff_layout_alloc_layout_hdr,
2246 .free_layout_hdr = ff_layout_free_layout_hdr,
2247 .alloc_lseg = ff_layout_alloc_lseg,
2248 .free_lseg = ff_layout_free_lseg,
Trond Myklebust0762ed22015-08-25 17:38:25 -04002249 .add_lseg = ff_layout_add_lseg,
Tom Haynesd67ae822014-12-11 17:02:04 -05002250 .pg_read_ops = &ff_layout_pg_read_ops,
2251 .pg_write_ops = &ff_layout_pg_write_ops,
2252 .get_ds_info = ff_layout_get_ds_info,
Trond Myklebustfc877012015-03-09 17:25:14 -04002253 .free_deviceid_node = ff_layout_free_deviceid_node,
Tom Haynes338d00c2015-02-17 14:58:15 -08002254 .mark_request_commit = pnfs_layout_mark_request_commit,
Tom Haynesd67ae822014-12-11 17:02:04 -05002255 .clear_request_commit = pnfs_generic_clear_request_commit,
2256 .scan_commit_lists = pnfs_generic_scan_commit_lists,
2257 .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
2258 .commit_pagelist = ff_layout_commit_pagelist,
2259 .read_pagelist = ff_layout_read_pagelist,
2260 .write_pagelist = ff_layout_write_pagelist,
2261 .alloc_deviceid_node = ff_layout_alloc_deviceid_node,
2262 .encode_layoutreturn = ff_layout_encode_layoutreturn,
Trond Myklebust5bb89b42015-03-25 14:14:42 -04002263 .sync = pnfs_nfs_generic_sync,
Peng Taoad4dc53e2015-06-23 19:52:01 +08002264 .prepare_layoutstats = ff_layout_prepare_layoutstats,
Peng Tao27c43062015-06-23 19:52:02 +08002265 .cleanup_layoutstats = ff_layout_cleanup_layoutstats,
Tom Haynesd67ae822014-12-11 17:02:04 -05002266};
2267
2268static int __init nfs4flexfilelayout_init(void)
2269{
2270 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n",
2271 __func__);
Jeff Layton90a0be02016-04-21 20:51:59 -04002272 if (!ff_zero_group) {
2273 ff_zero_group = groups_alloc(0);
2274 if (!ff_zero_group)
2275 return -ENOMEM;
2276 }
Tom Haynesd67ae822014-12-11 17:02:04 -05002277 return pnfs_register_layoutdriver(&flexfilelayout_type);
2278}
2279
2280static void __exit nfs4flexfilelayout_exit(void)
2281{
2282 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
2283 __func__);
2284 pnfs_unregister_layoutdriver(&flexfilelayout_type);
Jeff Layton90a0be02016-04-21 20:51:59 -04002285 if (ff_zero_group) {
2286 put_group_info(ff_zero_group);
2287 ff_zero_group = NULL;
2288 }
Tom Haynesd67ae822014-12-11 17:02:04 -05002289}
2290
2291MODULE_ALIAS("nfs-layouttype4-4");
2292
2293MODULE_LICENSE("GPL");
2294MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
2295
2296module_init(nfs4flexfilelayout_init);
2297module_exit(nfs4flexfilelayout_exit);