blob: 9257679a15badfefa4b6161a56c1994a4f6d15db [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
29static struct pnfs_layout_hdr *
30ff_layout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
31{
32 struct nfs4_flexfile_layout *ffl;
33
34 ffl = kzalloc(sizeof(*ffl), gfp_flags);
35 if (ffl) {
36 INIT_LIST_HEAD(&ffl->error_list);
Trond Myklebust266d12d2015-08-24 20:03:17 -040037 INIT_LIST_HEAD(&ffl->mirrors);
Tom Haynesd67ae822014-12-11 17:02:04 -050038 return &ffl->generic_hdr;
39 } else
40 return NULL;
41}
42
43static void
44ff_layout_free_layout_hdr(struct pnfs_layout_hdr *lo)
45{
46 struct nfs4_ff_layout_ds_err *err, *n;
47
48 list_for_each_entry_safe(err, n, &FF_LAYOUT_FROM_HDR(lo)->error_list,
49 list) {
50 list_del(&err->list);
51 kfree(err);
52 }
53 kfree(FF_LAYOUT_FROM_HDR(lo));
54}
55
56static int decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
57{
58 __be32 *p;
59
60 p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE);
61 if (unlikely(p == NULL))
62 return -ENOBUFS;
63 memcpy(stateid, p, NFS4_STATEID_SIZE);
64 dprintk("%s: stateid id= [%x%x%x%x]\n", __func__,
65 p[0], p[1], p[2], p[3]);
66 return 0;
67}
68
69static int decode_deviceid(struct xdr_stream *xdr, struct nfs4_deviceid *devid)
70{
71 __be32 *p;
72
73 p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE);
74 if (unlikely(!p))
75 return -ENOBUFS;
76 memcpy(devid, p, NFS4_DEVICEID4_SIZE);
77 nfs4_print_deviceid(devid);
78 return 0;
79}
80
81static int decode_nfs_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
82{
83 __be32 *p;
84
85 p = xdr_inline_decode(xdr, 4);
86 if (unlikely(!p))
87 return -ENOBUFS;
88 fh->size = be32_to_cpup(p++);
89 if (fh->size > sizeof(struct nfs_fh)) {
90 printk(KERN_ERR "NFS flexfiles: Too big fh received %d\n",
91 fh->size);
92 return -EOVERFLOW;
93 }
94 /* fh.data */
95 p = xdr_inline_decode(xdr, fh->size);
96 if (unlikely(!p))
97 return -ENOBUFS;
98 memcpy(&fh->data, p, fh->size);
99 dprintk("%s: fh len %d\n", __func__, fh->size);
100
101 return 0;
102}
103
104/*
105 * Currently only stringified uids and gids are accepted.
106 * I.e., kerberos is not supported to the DSes, so no pricipals.
107 *
108 * That means that one common function will suffice, but when
109 * principals are added, this should be split to accomodate
110 * calls to both nfs_map_name_to_uid() and nfs_map_group_to_gid().
111 */
112static int
113decode_name(struct xdr_stream *xdr, u32 *id)
114{
115 __be32 *p;
116 int len;
117
118 /* opaque_length(4)*/
119 p = xdr_inline_decode(xdr, 4);
120 if (unlikely(!p))
121 return -ENOBUFS;
122 len = be32_to_cpup(p++);
123 if (len < 0)
124 return -EINVAL;
125
126 dprintk("%s: len %u\n", __func__, len);
127
128 /* opaque body */
129 p = xdr_inline_decode(xdr, len);
130 if (unlikely(!p))
131 return -ENOBUFS;
132
133 if (!nfs_map_string_to_numeric((char *)p, len, id))
134 return -EINVAL;
135
136 return 0;
137}
138
Trond Myklebust266d12d2015-08-24 20:03:17 -0400139static bool ff_mirror_match_fh(const struct nfs4_ff_layout_mirror *m1,
140 const struct nfs4_ff_layout_mirror *m2)
141{
142 int i, j;
143
144 if (m1->fh_versions_cnt != m2->fh_versions_cnt)
145 return false;
146 for (i = 0; i < m1->fh_versions_cnt; i++) {
147 bool found_fh = false;
148 for (j = 0; j < m2->fh_versions_cnt; i++) {
149 if (nfs_compare_fh(&m1->fh_versions[i],
150 &m2->fh_versions[j]) == 0) {
151 found_fh = true;
152 break;
153 }
154 }
155 if (!found_fh)
156 return false;
157 }
158 return true;
159}
160
161static struct nfs4_ff_layout_mirror *
162ff_layout_add_mirror(struct pnfs_layout_hdr *lo,
163 struct nfs4_ff_layout_mirror *mirror)
164{
165 struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
166 struct nfs4_ff_layout_mirror *pos;
167 struct inode *inode = lo->plh_inode;
168
169 spin_lock(&inode->i_lock);
170 list_for_each_entry(pos, &ff_layout->mirrors, mirrors) {
171 if (mirror->mirror_ds != pos->mirror_ds)
172 continue;
173 if (!ff_mirror_match_fh(mirror, pos))
174 continue;
175 if (atomic_inc_not_zero(&pos->ref)) {
176 spin_unlock(&inode->i_lock);
177 return pos;
178 }
179 }
180 list_add(&mirror->mirrors, &ff_layout->mirrors);
181 mirror->layout = lo;
182 spin_unlock(&inode->i_lock);
183 return mirror;
184}
185
kbuild test robote3b1df22015-08-25 11:19:25 +0800186static void
Trond Myklebust266d12d2015-08-24 20:03:17 -0400187ff_layout_remove_mirror(struct nfs4_ff_layout_mirror *mirror)
188{
189 struct inode *inode;
190 if (mirror->layout == NULL)
191 return;
192 inode = mirror->layout->plh_inode;
193 spin_lock(&inode->i_lock);
194 list_del(&mirror->mirrors);
195 spin_unlock(&inode->i_lock);
196 mirror->layout = NULL;
197}
198
Trond Myklebust28a0d722015-08-24 18:08:30 -0400199static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(gfp_t gfp_flags)
200{
201 struct nfs4_ff_layout_mirror *mirror;
202
203 mirror = kzalloc(sizeof(*mirror), gfp_flags);
204 if (mirror != NULL) {
205 spin_lock_init(&mirror->lock);
206 atomic_set(&mirror->ref, 1);
Trond Myklebust266d12d2015-08-24 20:03:17 -0400207 INIT_LIST_HEAD(&mirror->mirrors);
Trond Myklebust28a0d722015-08-24 18:08:30 -0400208 }
209 return mirror;
210}
211
212static void ff_layout_free_mirror(struct nfs4_ff_layout_mirror *mirror)
213{
Trond Myklebust266d12d2015-08-24 20:03:17 -0400214 ff_layout_remove_mirror(mirror);
Trond Myklebust28a0d722015-08-24 18:08:30 -0400215 kfree(mirror->fh_versions);
Trond Myklebust972398f2015-09-01 12:03:56 -0700216 if (mirror->cred)
217 put_rpccred(mirror->cred);
Trond Myklebust28a0d722015-08-24 18:08:30 -0400218 nfs4_ff_layout_put_deviceid(mirror->mirror_ds);
219 kfree(mirror);
220}
221
222static void ff_layout_put_mirror(struct nfs4_ff_layout_mirror *mirror)
223{
224 if (mirror != NULL && atomic_dec_and_test(&mirror->ref))
225 ff_layout_free_mirror(mirror);
226}
227
Tom Haynesd67ae822014-12-11 17:02:04 -0500228static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment *fls)
229{
230 int i;
231
232 if (fls->mirror_array) {
233 for (i = 0; i < fls->mirror_array_cnt; i++) {
234 /* normally mirror_ds is freed in
235 * .free_deviceid_node but we still do it here
236 * for .alloc_lseg error path */
Trond Myklebust28a0d722015-08-24 18:08:30 -0400237 ff_layout_put_mirror(fls->mirror_array[i]);
Tom Haynesd67ae822014-12-11 17:02:04 -0500238 }
239 kfree(fls->mirror_array);
240 fls->mirror_array = NULL;
241 }
242}
243
244static int ff_layout_check_layout(struct nfs4_layoutget_res *lgr)
245{
246 int ret = 0;
247
248 dprintk("--> %s\n", __func__);
249
250 /* FIXME: remove this check when layout segment support is added */
251 if (lgr->range.offset != 0 ||
252 lgr->range.length != NFS4_MAX_UINT64) {
253 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
254 __func__);
255 ret = -EINVAL;
256 }
257
258 dprintk("--> %s returns %d\n", __func__, ret);
259 return ret;
260}
261
262static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment *fls)
263{
264 if (fls) {
265 ff_layout_free_mirror_array(fls);
266 kfree(fls);
267 }
268}
269
Trond Myklebust0762ed22015-08-25 17:38:25 -0400270static bool
271ff_lseg_range_is_after(const struct pnfs_layout_range *l1,
272 const struct pnfs_layout_range *l2)
273{
274 u64 end1, end2;
275
276 if (l1->iomode != l2->iomode)
277 return l1->iomode != IOMODE_READ;
278 end1 = pnfs_calc_offset_end(l1->offset, l1->length);
279 end2 = pnfs_calc_offset_end(l2->offset, l2->length);
280 if (end1 < l2->offset)
281 return false;
282 if (end2 < l1->offset)
283 return true;
284 return l2->offset <= l1->offset;
285}
286
287static bool
288ff_lseg_merge(struct pnfs_layout_segment *new,
289 struct pnfs_layout_segment *old)
290{
291 u64 new_end, old_end;
292
293 if (new->pls_range.iomode != old->pls_range.iomode)
294 return false;
295 old_end = pnfs_calc_offset_end(old->pls_range.offset,
296 old->pls_range.length);
297 if (old_end < new->pls_range.offset)
298 return false;
299 new_end = pnfs_calc_offset_end(new->pls_range.offset,
300 new->pls_range.length);
301 if (new_end < old->pls_range.offset)
302 return false;
303
304 /* Mergeable: copy info from 'old' to 'new' */
305 if (new_end < old_end)
306 new_end = old_end;
307 if (new->pls_range.offset < old->pls_range.offset)
308 new->pls_range.offset = old->pls_range.offset;
309 new->pls_range.length = pnfs_calc_offset_length(new->pls_range.offset,
310 new_end);
311 if (test_bit(NFS_LSEG_ROC, &old->pls_flags))
312 set_bit(NFS_LSEG_ROC, &new->pls_flags);
313 if (test_bit(NFS_LSEG_LAYOUTRETURN, &old->pls_flags))
314 set_bit(NFS_LSEG_LAYOUTRETURN, &new->pls_flags);
315 return true;
316}
317
318static void
319ff_layout_add_lseg(struct pnfs_layout_hdr *lo,
320 struct pnfs_layout_segment *lseg,
321 struct list_head *free_me)
322{
323 pnfs_generic_layout_insert_lseg(lo, lseg,
324 ff_lseg_range_is_after,
325 ff_lseg_merge,
326 free_me);
327}
328
Tom Haynesd67ae822014-12-11 17:02:04 -0500329static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment *fls)
330{
Tom Haynesd67ae822014-12-11 17:02:04 -0500331 int i, j;
332
333 for (i = 0; i < fls->mirror_array_cnt - 1; i++) {
334 for (j = i + 1; j < fls->mirror_array_cnt; j++)
335 if (fls->mirror_array[i]->efficiency <
Fabian Frederick455b6ee2015-06-12 18:58:50 +0200336 fls->mirror_array[j]->efficiency)
337 swap(fls->mirror_array[i],
338 fls->mirror_array[j]);
Tom Haynesd67ae822014-12-11 17:02:04 -0500339 }
340}
341
Trond Myklebust260074c2015-11-02 09:59:00 -0500342static void ff_layout_mark_devices_valid(struct nfs4_ff_layout_segment *fls)
343{
344 struct nfs4_deviceid_node *node;
345 int i;
346
347 if (!(fls->flags & FF_FLAGS_NO_IO_THRU_MDS))
348 return;
349 for (i = 0; i < fls->mirror_array_cnt; i++) {
350 node = &fls->mirror_array[i]->mirror_ds->id_node;
351 clear_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags);
352 }
353}
354
Tom Haynesd67ae822014-12-11 17:02:04 -0500355static struct pnfs_layout_segment *
356ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh,
357 struct nfs4_layoutget_res *lgr,
358 gfp_t gfp_flags)
359{
360 struct pnfs_layout_segment *ret;
361 struct nfs4_ff_layout_segment *fls = NULL;
362 struct xdr_stream stream;
363 struct xdr_buf buf;
364 struct page *scratch;
365 u64 stripe_unit;
366 u32 mirror_array_cnt;
367 __be32 *p;
368 int i, rc;
369
370 dprintk("--> %s\n", __func__);
371 scratch = alloc_page(gfp_flags);
372 if (!scratch)
373 return ERR_PTR(-ENOMEM);
374
375 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages,
376 lgr->layoutp->len);
377 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
378
379 /* stripe unit and mirror_array_cnt */
380 rc = -EIO;
381 p = xdr_inline_decode(&stream, 8 + 4);
382 if (!p)
383 goto out_err_free;
384
385 p = xdr_decode_hyper(p, &stripe_unit);
386 mirror_array_cnt = be32_to_cpup(p++);
387 dprintk("%s: stripe_unit=%llu mirror_array_cnt=%u\n", __func__,
388 stripe_unit, mirror_array_cnt);
389
390 if (mirror_array_cnt > NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT ||
391 mirror_array_cnt == 0)
392 goto out_err_free;
393
394 rc = -ENOMEM;
395 fls = kzalloc(sizeof(*fls), gfp_flags);
396 if (!fls)
397 goto out_err_free;
398
399 fls->mirror_array_cnt = mirror_array_cnt;
400 fls->stripe_unit = stripe_unit;
401 fls->mirror_array = kcalloc(fls->mirror_array_cnt,
402 sizeof(fls->mirror_array[0]), gfp_flags);
403 if (fls->mirror_array == NULL)
404 goto out_err_free;
405
406 for (i = 0; i < fls->mirror_array_cnt; i++) {
Trond Myklebust266d12d2015-08-24 20:03:17 -0400407 struct nfs4_ff_layout_mirror *mirror;
Tom Haynesd67ae822014-12-11 17:02:04 -0500408 struct nfs4_deviceid devid;
409 struct nfs4_deviceid_node *idnode;
410 u32 ds_count;
411 u32 fh_count;
412 int j;
413
414 rc = -EIO;
415 p = xdr_inline_decode(&stream, 4);
416 if (!p)
417 goto out_err_free;
418 ds_count = be32_to_cpup(p);
419
420 /* FIXME: allow for striping? */
421 if (ds_count != 1)
422 goto out_err_free;
423
Trond Myklebust28a0d722015-08-24 18:08:30 -0400424 fls->mirror_array[i] = ff_layout_alloc_mirror(gfp_flags);
Tom Haynesd67ae822014-12-11 17:02:04 -0500425 if (fls->mirror_array[i] == NULL) {
426 rc = -ENOMEM;
427 goto out_err_free;
428 }
429
Tom Haynesd67ae822014-12-11 17:02:04 -0500430 fls->mirror_array[i]->ds_count = ds_count;
431
432 /* deviceid */
433 rc = decode_deviceid(&stream, &devid);
434 if (rc)
435 goto out_err_free;
436
437 idnode = nfs4_find_get_deviceid(NFS_SERVER(lh->plh_inode),
438 &devid, lh->plh_lc_cred,
439 gfp_flags);
440 /*
441 * upon success, mirror_ds is allocated by previous
442 * getdeviceinfo, or newly by .alloc_deviceid_node
443 * nfs4_find_get_deviceid failure is indeed getdeviceinfo falure
444 */
445 if (idnode)
446 fls->mirror_array[i]->mirror_ds =
447 FF_LAYOUT_MIRROR_DS(idnode);
448 else
449 goto out_err_free;
450
451 /* efficiency */
452 rc = -EIO;
453 p = xdr_inline_decode(&stream, 4);
454 if (!p)
455 goto out_err_free;
456 fls->mirror_array[i]->efficiency = be32_to_cpup(p);
457
458 /* stateid */
459 rc = decode_stateid(&stream, &fls->mirror_array[i]->stateid);
460 if (rc)
461 goto out_err_free;
462
463 /* fh */
464 p = xdr_inline_decode(&stream, 4);
465 if (!p)
466 goto out_err_free;
467 fh_count = be32_to_cpup(p);
468
469 fls->mirror_array[i]->fh_versions =
470 kzalloc(fh_count * sizeof(struct nfs_fh),
471 gfp_flags);
472 if (fls->mirror_array[i]->fh_versions == NULL) {
473 rc = -ENOMEM;
474 goto out_err_free;
475 }
476
477 for (j = 0; j < fh_count; j++) {
478 rc = decode_nfs_fh(&stream,
479 &fls->mirror_array[i]->fh_versions[j]);
480 if (rc)
481 goto out_err_free;
482 }
483
484 fls->mirror_array[i]->fh_versions_cnt = fh_count;
485
486 /* user */
487 rc = decode_name(&stream, &fls->mirror_array[i]->uid);
488 if (rc)
489 goto out_err_free;
490
491 /* group */
492 rc = decode_name(&stream, &fls->mirror_array[i]->gid);
493 if (rc)
494 goto out_err_free;
495
Trond Myklebust266d12d2015-08-24 20:03:17 -0400496 mirror = ff_layout_add_mirror(lh, fls->mirror_array[i]);
497 if (mirror != fls->mirror_array[i]) {
498 ff_layout_free_mirror(fls->mirror_array[i]);
499 fls->mirror_array[i] = mirror;
500 }
501
Tom Haynesd67ae822014-12-11 17:02:04 -0500502 dprintk("%s: uid %d gid %d\n", __func__,
503 fls->mirror_array[i]->uid,
504 fls->mirror_array[i]->gid);
505 }
506
Trond Myklebustc0f5f502015-06-26 14:51:32 -0400507 p = xdr_inline_decode(&stream, 4);
Trond Myklebustd0379a52015-11-16 11:26:07 -0500508 if (!p)
509 goto out_sort_mirrors;
510 fls->flags = be32_to_cpup(p);
Trond Myklebustc0f5f502015-06-26 14:51:32 -0400511
Trond Myklebustd0379a52015-11-16 11:26:07 -0500512 p = xdr_inline_decode(&stream, 4);
513 if (!p)
514 goto out_sort_mirrors;
515 for (i=0; i < fls->mirror_array_cnt; i++)
516 fls->mirror_array[i]->report_interval = be32_to_cpup(p);
517
518out_sort_mirrors:
Tom Haynesd67ae822014-12-11 17:02:04 -0500519 ff_layout_sort_mirrors(fls);
520 rc = ff_layout_check_layout(lgr);
521 if (rc)
522 goto out_err_free;
Trond Myklebust260074c2015-11-02 09:59:00 -0500523 ff_layout_mark_devices_valid(fls);
Tom Haynesd67ae822014-12-11 17:02:04 -0500524
525 ret = &fls->generic_hdr;
526 dprintk("<-- %s (success)\n", __func__);
527out_free_page:
528 __free_page(scratch);
529 return ret;
530out_err_free:
531 _ff_layout_free_lseg(fls);
532 ret = ERR_PTR(rc);
533 dprintk("<-- %s (%d)\n", __func__, rc);
534 goto out_free_page;
535}
536
537static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout)
538{
539 struct pnfs_layout_segment *lseg;
540
541 list_for_each_entry(lseg, &layout->plh_segs, pls_list)
542 if (lseg->pls_range.iomode == IOMODE_RW)
543 return true;
544
545 return false;
546}
547
548static void
549ff_layout_free_lseg(struct pnfs_layout_segment *lseg)
550{
551 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
Tom Haynesd67ae822014-12-11 17:02:04 -0500552
553 dprintk("--> %s\n", __func__);
554
Tom Haynesd67ae822014-12-11 17:02:04 -0500555 if (lseg->pls_range.iomode == IOMODE_RW) {
556 struct nfs4_flexfile_layout *ffl;
557 struct inode *inode;
558
559 ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout);
560 inode = ffl->generic_hdr.plh_inode;
561 spin_lock(&inode->i_lock);
562 if (!ff_layout_has_rw_segments(lseg->pls_layout)) {
563 ffl->commit_info.nbuckets = 0;
564 kfree(ffl->commit_info.buckets);
565 ffl->commit_info.buckets = NULL;
566 }
567 spin_unlock(&inode->i_lock);
568 }
569 _ff_layout_free_lseg(fls);
570}
571
572/* Return 1 until we have multiple lsegs support */
573static int
574ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls)
575{
576 return 1;
577}
578
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800579static void
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500580nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800581{
Peng Tao9bbd9bb2015-06-26 09:45:49 +0800582 /* first IO request? */
583 if (atomic_inc_return(&timer->n_ops) == 1) {
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500584 timer->start_time = now;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800585 }
586}
587
588static ktime_t
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500589nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800590{
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500591 ktime_t start;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800592
Peng Tao9bbd9bb2015-06-26 09:45:49 +0800593 if (atomic_dec_return(&timer->n_ops) < 0)
594 WARN_ON_ONCE(1);
595
Peng Tao9bbd9bb2015-06-26 09:45:49 +0800596 start = timer->start_time;
597 timer->start_time = now;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800598 return ktime_sub(now, start);
599}
600
Peng Tao97ba3752015-06-23 19:52:04 +0800601static bool
Peng Taod9838032015-06-23 19:52:00 +0800602nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500603 struct nfs4_ff_layoutstat *layoutstat,
604 ktime_t now)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800605{
Peng Taod9838032015-06-23 19:52:00 +0800606 static const ktime_t notime = {0};
Trond Myklebustbbf58bf2015-08-24 20:39:18 -0400607 s64 report_interval = FF_LAYOUTSTATS_REPORT_INTERVAL;
Peng Taod9838032015-06-23 19:52:00 +0800608
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500609 nfs4_ff_start_busy_timer(&layoutstat->busy_timer, now);
Peng Tao9bbd9bb2015-06-26 09:45:49 +0800610 if (ktime_equal(mirror->start_time, notime))
611 mirror->start_time = now;
612 if (ktime_equal(mirror->last_report_time, notime))
613 mirror->last_report_time = now;
Trond Myklebustd0379a52015-11-16 11:26:07 -0500614 if (mirror->report_interval != 0)
615 report_interval = (s64)mirror->report_interval * 1000LL;
616 else if (layoutstats_timer != 0)
Trond Myklebustbbf58bf2015-08-24 20:39:18 -0400617 report_interval = (s64)layoutstats_timer * 1000LL;
Peng Tao97ba3752015-06-23 19:52:04 +0800618 if (ktime_to_ms(ktime_sub(now, mirror->last_report_time)) >=
Trond Myklebustbbf58bf2015-08-24 20:39:18 -0400619 report_interval) {
Peng Tao97ba3752015-06-23 19:52:04 +0800620 mirror->last_report_time = now;
621 return true;
622 }
623
624 return false;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800625}
626
627static void
628nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat,
629 __u64 requested)
630{
631 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
632
633 iostat->ops_requested++;
634 iostat->bytes_requested += requested;
635}
636
637static void
638nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat,
639 __u64 requested,
640 __u64 completed,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500641 ktime_t time_completed,
642 ktime_t time_started)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800643{
644 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500645 ktime_t completion_time = ktime_sub(time_completed, time_started);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800646 ktime_t timer;
647
648 iostat->ops_completed++;
649 iostat->bytes_completed += completed;
650 iostat->bytes_not_delivered += requested - completed;
651
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500652 timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer, time_completed);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800653 iostat->total_busy_time =
654 ktime_add(iostat->total_busy_time, timer);
655 iostat->aggregate_completion_time =
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500656 ktime_add(iostat->aggregate_completion_time,
657 completion_time);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800658}
659
660static void
Trond Myklebust0b7baf92015-08-24 18:22:28 -0400661nfs4_ff_layout_stat_io_start_read(struct inode *inode,
662 struct nfs4_ff_layout_mirror *mirror,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500663 __u64 requested, ktime_t now)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800664{
Peng Tao97ba3752015-06-23 19:52:04 +0800665 bool report;
666
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800667 spin_lock(&mirror->lock);
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500668 report = nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat, now);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800669 nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested);
670 spin_unlock(&mirror->lock);
Peng Tao97ba3752015-06-23 19:52:04 +0800671
672 if (report)
Trond Myklebust0b7baf92015-08-24 18:22:28 -0400673 pnfs_report_layoutstat(inode, GFP_KERNEL);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800674}
675
676static void
677nfs4_ff_layout_stat_io_end_read(struct rpc_task *task,
678 struct nfs4_ff_layout_mirror *mirror,
679 __u64 requested,
680 __u64 completed)
681{
682 spin_lock(&mirror->lock);
683 nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat,
684 requested, completed,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500685 ktime_get(), task->tk_start);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800686 spin_unlock(&mirror->lock);
687}
688
689static void
Trond Myklebust0b7baf92015-08-24 18:22:28 -0400690nfs4_ff_layout_stat_io_start_write(struct inode *inode,
691 struct nfs4_ff_layout_mirror *mirror,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500692 __u64 requested, ktime_t now)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800693{
Peng Tao97ba3752015-06-23 19:52:04 +0800694 bool report;
695
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800696 spin_lock(&mirror->lock);
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500697 report = nfs4_ff_layoutstat_start_io(mirror , &mirror->write_stat, now);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800698 nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested);
699 spin_unlock(&mirror->lock);
Peng Tao97ba3752015-06-23 19:52:04 +0800700
701 if (report)
Trond Myklebust0b7baf92015-08-24 18:22:28 -0400702 pnfs_report_layoutstat(inode, GFP_NOIO);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800703}
704
705static void
706nfs4_ff_layout_stat_io_end_write(struct rpc_task *task,
707 struct nfs4_ff_layout_mirror *mirror,
708 __u64 requested,
709 __u64 completed,
710 enum nfs3_stable_how committed)
711{
712 if (committed == NFS_UNSTABLE)
713 requested = completed = 0;
714
715 spin_lock(&mirror->lock);
716 nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat,
Trond Myklebuste76d28d2015-08-20 13:12:51 -0500717 requested, completed, ktime_get(), task->tk_start);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800718 spin_unlock(&mirror->lock);
719}
720
Tom Haynesd67ae822014-12-11 17:02:04 -0500721static int
722ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg,
723 struct nfs_commit_info *cinfo,
724 gfp_t gfp_flags)
725{
726 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
727 struct pnfs_commit_bucket *buckets;
728 int size;
729
730 if (cinfo->ds->nbuckets != 0) {
731 /* This assumes there is only one RW lseg per file.
732 * To support multiple lseg per file, we need to
733 * change struct pnfs_commit_bucket to allow dynamic
734 * increasing nbuckets.
735 */
736 return 0;
737 }
738
739 size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg);
740
741 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
742 gfp_flags);
743 if (!buckets)
744 return -ENOMEM;
745 else {
746 int i;
747
748 spin_lock(cinfo->lock);
749 if (cinfo->ds->nbuckets != 0)
750 kfree(buckets);
751 else {
752 cinfo->ds->buckets = buckets;
753 cinfo->ds->nbuckets = size;
754 for (i = 0; i < size; i++) {
755 INIT_LIST_HEAD(&buckets[i].written);
756 INIT_LIST_HEAD(&buckets[i].committing);
757 /* mark direct verifier as unset */
758 buckets[i].direct_verf.committed =
759 NFS_INVALID_STABLE_HOW;
760 }
761 }
762 spin_unlock(cinfo->lock);
763 return 0;
764 }
765}
766
767static struct nfs4_pnfs_ds *
Trond Myklebust13544412015-11-02 10:11:17 -0500768ff_layout_choose_best_ds_for_read(struct pnfs_layout_segment *lseg,
769 int start_idx,
Tom Haynesd67ae822014-12-11 17:02:04 -0500770 int *best_idx)
771{
Trond Myklebust13544412015-11-02 10:11:17 -0500772 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
Tom Haynesd67ae822014-12-11 17:02:04 -0500773 struct nfs4_pnfs_ds *ds;
774 int idx;
775
Tom Haynesd67ae822014-12-11 17:02:04 -0500776 /* mirrors are sorted by efficiency */
Trond Myklebust13544412015-11-02 10:11:17 -0500777 for (idx = start_idx; idx < fls->mirror_array_cnt; idx++) {
778 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
Tom Haynesd67ae822014-12-11 17:02:04 -0500779 if (ds) {
780 *best_idx = idx;
781 return ds;
782 }
783 }
784
785 return NULL;
786}
787
788static void
789ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
790 struct nfs_page *req)
791{
792 struct nfs_pgio_mirror *pgm;
793 struct nfs4_ff_layout_mirror *mirror;
794 struct nfs4_pnfs_ds *ds;
795 int ds_idx;
796
797 /* Use full layout for now */
Peng Taod600ad12015-12-04 02:57:48 +0800798 if (!pgio->pg_lseg) {
Tom Haynesd67ae822014-12-11 17:02:04 -0500799 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
800 req->wb_context,
801 0,
802 NFS4_MAX_UINT64,
803 IOMODE_READ,
804 GFP_KERNEL);
Peng Taod600ad12015-12-04 02:57:48 +0800805 if (IS_ERR(pgio->pg_lseg)) {
806 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
807 pgio->pg_lseg = NULL;
808 return;
809 }
810 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500811 /* If no lseg, fall back to read through mds */
812 if (pgio->pg_lseg == NULL)
813 goto out_mds;
814
Trond Myklebust13544412015-11-02 10:11:17 -0500815 ds = ff_layout_choose_best_ds_for_read(pgio->pg_lseg, 0, &ds_idx);
Tom Haynesd67ae822014-12-11 17:02:04 -0500816 if (!ds)
817 goto out_mds;
818 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx);
819
820 pgio->pg_mirror_idx = ds_idx;
821
822 /* read always uses only one mirror - idx 0 for pgio layer */
823 pgm = &pgio->pg_mirrors[0];
824 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize;
825
826 return;
827out_mds:
828 pnfs_put_lseg(pgio->pg_lseg);
829 pgio->pg_lseg = NULL;
830 nfs_pageio_reset_read_mds(pgio);
831}
832
833static void
834ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
835 struct nfs_page *req)
836{
837 struct nfs4_ff_layout_mirror *mirror;
838 struct nfs_pgio_mirror *pgm;
839 struct nfs_commit_info cinfo;
840 struct nfs4_pnfs_ds *ds;
841 int i;
842 int status;
843
Peng Taod600ad12015-12-04 02:57:48 +0800844 if (!pgio->pg_lseg) {
Tom Haynesd67ae822014-12-11 17:02:04 -0500845 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
846 req->wb_context,
847 0,
848 NFS4_MAX_UINT64,
849 IOMODE_RW,
850 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +0800851 if (IS_ERR(pgio->pg_lseg)) {
852 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
853 pgio->pg_lseg = NULL;
854 return;
855 }
856 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500857 /* If no lseg, fall back to write through mds */
858 if (pgio->pg_lseg == NULL)
859 goto out_mds;
860
861 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
862 status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
863 if (status < 0)
864 goto out_mds;
865
866 /* Use a direct mapping of ds_idx to pgio mirror_idx */
867 if (WARN_ON_ONCE(pgio->pg_mirror_count !=
868 FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg)))
869 goto out_mds;
870
871 for (i = 0; i < pgio->pg_mirror_count; i++) {
872 ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true);
873 if (!ds)
874 goto out_mds;
875 pgm = &pgio->pg_mirrors[i];
876 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i);
877 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize;
878 }
879
880 return;
881
882out_mds:
883 pnfs_put_lseg(pgio->pg_lseg);
884 pgio->pg_lseg = NULL;
885 nfs_pageio_reset_write_mds(pgio);
886}
887
888static unsigned int
889ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
890 struct nfs_page *req)
891{
Peng Taod600ad12015-12-04 02:57:48 +0800892 if (!pgio->pg_lseg) {
Tom Haynesd67ae822014-12-11 17:02:04 -0500893 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
894 req->wb_context,
895 0,
896 NFS4_MAX_UINT64,
897 IOMODE_RW,
898 GFP_NOFS);
Peng Taod600ad12015-12-04 02:57:48 +0800899 if (IS_ERR(pgio->pg_lseg)) {
900 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
901 pgio->pg_lseg = NULL;
902 goto out;
903 }
904 }
Tom Haynesd67ae822014-12-11 17:02:04 -0500905 if (pgio->pg_lseg)
906 return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
907
908 /* no lseg means that pnfs is not in use, so no mirroring here */
Tom Haynesd67ae822014-12-11 17:02:04 -0500909 nfs_pageio_reset_write_mds(pgio);
Peng Taod600ad12015-12-04 02:57:48 +0800910out:
Tom Haynesd67ae822014-12-11 17:02:04 -0500911 return 1;
912}
913
914static const struct nfs_pageio_ops ff_layout_pg_read_ops = {
915 .pg_init = ff_layout_pg_init_read,
916 .pg_test = pnfs_generic_pg_test,
917 .pg_doio = pnfs_generic_pg_readpages,
918 .pg_cleanup = pnfs_generic_pg_cleanup,
919};
920
921static const struct nfs_pageio_ops ff_layout_pg_write_ops = {
922 .pg_init = ff_layout_pg_init_write,
923 .pg_test = pnfs_generic_pg_test,
924 .pg_doio = pnfs_generic_pg_writepages,
925 .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write,
926 .pg_cleanup = pnfs_generic_pg_cleanup,
927};
928
929static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs)
930{
931 struct rpc_task *task = &hdr->task;
932
933 pnfs_layoutcommit_inode(hdr->inode, false);
934
935 if (retry_pnfs) {
936 dprintk("%s Reset task %5u for i/o through pNFS "
937 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
938 hdr->task.tk_pid,
939 hdr->inode->i_sb->s_id,
940 (unsigned long long)NFS_FILEID(hdr->inode),
941 hdr->args.count,
942 (unsigned long long)hdr->args.offset);
943
944 if (!hdr->dreq) {
945 struct nfs_open_context *ctx;
946
947 ctx = nfs_list_entry(hdr->pages.next)->wb_context;
948 set_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
949 hdr->completion_ops->error_cleanup(&hdr->pages);
950 } else {
951 nfs_direct_set_resched_writes(hdr->dreq);
952 /* fake unstable write to let common nfs resend pages */
953 hdr->verf.committed = NFS_UNSTABLE;
Trond Myklebustd6208762015-06-26 15:37:58 -0400954 hdr->good_bytes = hdr->args.count;
Tom Haynesd67ae822014-12-11 17:02:04 -0500955 }
956 return;
957 }
958
959 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
960 dprintk("%s Reset task %5u for i/o through MDS "
961 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
962 hdr->task.tk_pid,
963 hdr->inode->i_sb->s_id,
964 (unsigned long long)NFS_FILEID(hdr->inode),
965 hdr->args.count,
966 (unsigned long long)hdr->args.offset);
967
968 task->tk_status = pnfs_write_done_resend_to_mds(hdr);
969 }
970}
971
972static void ff_layout_reset_read(struct nfs_pgio_header *hdr)
973{
974 struct rpc_task *task = &hdr->task;
975
976 pnfs_layoutcommit_inode(hdr->inode, false);
977
978 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
979 dprintk("%s Reset task %5u for i/o through MDS "
980 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
981 hdr->task.tk_pid,
982 hdr->inode->i_sb->s_id,
983 (unsigned long long)NFS_FILEID(hdr->inode),
984 hdr->args.count,
985 (unsigned long long)hdr->args.offset);
986
987 task->tk_status = pnfs_read_done_resend_to_mds(hdr);
988 }
989}
990
991static int ff_layout_async_handle_error_v4(struct rpc_task *task,
992 struct nfs4_state *state,
993 struct nfs_client *clp,
994 struct pnfs_layout_segment *lseg,
995 int idx)
996{
997 struct pnfs_layout_hdr *lo = lseg->pls_layout;
998 struct inode *inode = lo->plh_inode;
999 struct nfs_server *mds_server = NFS_SERVER(inode);
1000
1001 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1002 struct nfs_client *mds_client = mds_server->nfs_client;
1003 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
1004
1005 if (task->tk_status >= 0)
1006 return 0;
1007
1008 switch (task->tk_status) {
1009 /* MDS state errors */
1010 case -NFS4ERR_DELEG_REVOKED:
1011 case -NFS4ERR_ADMIN_REVOKED:
1012 case -NFS4ERR_BAD_STATEID:
1013 if (state == NULL)
1014 break;
1015 nfs_remove_bad_delegation(state->inode);
1016 case -NFS4ERR_OPENMODE:
1017 if (state == NULL)
1018 break;
1019 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
1020 goto out_bad_stateid;
1021 goto wait_on_recovery;
1022 case -NFS4ERR_EXPIRED:
1023 if (state != NULL) {
1024 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
1025 goto out_bad_stateid;
1026 }
1027 nfs4_schedule_lease_recovery(mds_client);
1028 goto wait_on_recovery;
1029 /* DS session errors */
1030 case -NFS4ERR_BADSESSION:
1031 case -NFS4ERR_BADSLOT:
1032 case -NFS4ERR_BAD_HIGH_SLOT:
1033 case -NFS4ERR_DEADSESSION:
1034 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1035 case -NFS4ERR_SEQ_FALSE_RETRY:
1036 case -NFS4ERR_SEQ_MISORDERED:
1037 dprintk("%s ERROR %d, Reset session. Exchangeid "
1038 "flags 0x%x\n", __func__, task->tk_status,
1039 clp->cl_exchange_flags);
1040 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
1041 break;
1042 case -NFS4ERR_DELAY:
1043 case -NFS4ERR_GRACE:
1044 rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX);
1045 break;
1046 case -NFS4ERR_RETRY_UNCACHED_REP:
1047 break;
1048 /* Invalidate Layout errors */
1049 case -NFS4ERR_PNFS_NO_LAYOUT:
1050 case -ESTALE: /* mapped NFS4ERR_STALE */
1051 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
1052 case -EISDIR: /* mapped NFS4ERR_ISDIR */
1053 case -NFS4ERR_FHEXPIRED:
1054 case -NFS4ERR_WRONG_TYPE:
1055 dprintk("%s Invalid layout error %d\n", __func__,
1056 task->tk_status);
1057 /*
1058 * Destroy layout so new i/o will get a new layout.
1059 * Layout will not be destroyed until all current lseg
1060 * references are put. Mark layout as invalid to resend failed
1061 * i/o and all i/o waiting on the slot table to the MDS until
1062 * layout is destroyed and a new valid layout is obtained.
1063 */
1064 pnfs_destroy_layout(NFS_I(inode));
1065 rpc_wake_up(&tbl->slot_tbl_waitq);
1066 goto reset;
1067 /* RPC connection errors */
1068 case -ECONNREFUSED:
1069 case -EHOSTDOWN:
1070 case -EHOSTUNREACH:
1071 case -ENETUNREACH:
1072 case -EIO:
1073 case -ETIMEDOUT:
1074 case -EPIPE:
1075 dprintk("%s DS connection error %d\n", __func__,
1076 task->tk_status);
1077 nfs4_mark_deviceid_unavailable(devid);
1078 rpc_wake_up(&tbl->slot_tbl_waitq);
1079 /* fall through */
1080 default:
Trond Myklebust260074c2015-11-02 09:59:00 -05001081 if (ff_layout_no_fallback_to_mds(lseg) ||
1082 ff_layout_has_available_ds(lseg))
Tom Haynesd67ae822014-12-11 17:02:04 -05001083 return -NFS4ERR_RESET_TO_PNFS;
1084reset:
1085 dprintk("%s Retry through MDS. Error %d\n", __func__,
1086 task->tk_status);
1087 return -NFS4ERR_RESET_TO_MDS;
1088 }
1089out:
1090 task->tk_status = 0;
1091 return -EAGAIN;
1092out_bad_stateid:
1093 task->tk_status = -EIO;
1094 return 0;
1095wait_on_recovery:
1096 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
1097 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
1098 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
1099 goto out;
1100}
1101
1102/* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
1103static int ff_layout_async_handle_error_v3(struct rpc_task *task,
1104 struct pnfs_layout_segment *lseg,
1105 int idx)
1106{
1107 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1108
1109 if (task->tk_status >= 0)
1110 return 0;
1111
Trond Myklebust90816d12015-08-30 09:53:06 -07001112 switch (task->tk_status) {
1113 /* File access problems. Don't mark the device as unavailable */
1114 case -EACCES:
1115 case -ESTALE:
1116 case -EISDIR:
1117 case -EBADHANDLE:
1118 case -ELOOP:
1119 case -ENOSPC:
1120 break;
1121 case -EJUKEBOX:
1122 nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY);
1123 goto out_retry;
1124 default:
Tom Haynesd67ae822014-12-11 17:02:04 -05001125 dprintk("%s DS connection error %d\n", __func__,
1126 task->tk_status);
1127 nfs4_mark_deviceid_unavailable(devid);
Tom Haynesd67ae822014-12-11 17:02:04 -05001128 }
Trond Myklebust90816d12015-08-30 09:53:06 -07001129 /* FIXME: Need to prevent infinite looping here. */
1130 return -NFS4ERR_RESET_TO_PNFS;
1131out_retry:
Tom Haynesd67ae822014-12-11 17:02:04 -05001132 task->tk_status = 0;
Trond Myklebust7eeea162015-12-17 09:22:15 -05001133 rpc_restart_call_prepare(task);
Tom Haynesd67ae822014-12-11 17:02:04 -05001134 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
1135 return -EAGAIN;
1136}
1137
1138static int ff_layout_async_handle_error(struct rpc_task *task,
1139 struct nfs4_state *state,
1140 struct nfs_client *clp,
1141 struct pnfs_layout_segment *lseg,
1142 int idx)
1143{
1144 int vers = clp->cl_nfs_mod->rpc_vers->number;
1145
1146 switch (vers) {
1147 case 3:
1148 return ff_layout_async_handle_error_v3(task, lseg, idx);
1149 case 4:
1150 return ff_layout_async_handle_error_v4(task, state, clp,
1151 lseg, idx);
1152 default:
1153 /* should never happen */
1154 WARN_ON_ONCE(1);
1155 return 0;
1156 }
1157}
1158
1159static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
1160 int idx, u64 offset, u64 length,
Trond Myklebustdd521282015-08-20 17:59:49 -05001161 u32 status, int opnum, int error)
Tom Haynesd67ae822014-12-11 17:02:04 -05001162{
1163 struct nfs4_ff_layout_mirror *mirror;
1164 int err;
1165
Trond Myklebustdd521282015-08-20 17:59:49 -05001166 if (status == 0) {
1167 switch (error) {
1168 case -ETIMEDOUT:
1169 case -EPFNOSUPPORT:
1170 case -EPROTONOSUPPORT:
1171 case -EOPNOTSUPP:
1172 case -ECONNREFUSED:
1173 case -ECONNRESET:
1174 case -EHOSTDOWN:
1175 case -EHOSTUNREACH:
1176 case -ENETUNREACH:
1177 case -EADDRINUSE:
1178 case -ENOBUFS:
1179 case -EPIPE:
1180 case -EPERM:
1181 status = NFS4ERR_NXIO;
1182 break;
1183 case -EACCES:
1184 status = NFS4ERR_ACCESS;
1185 break;
1186 default:
1187 return;
1188 }
1189 }
1190
Peng Taoc22eeb82015-12-07 17:31:38 -05001191 switch (status) {
1192 case NFS4ERR_DELAY:
1193 case NFS4ERR_GRACE:
1194 return;
1195 default:
1196 break;
1197 }
1198
Tom Haynesd67ae822014-12-11 17:02:04 -05001199 mirror = FF_LAYOUT_COMP(lseg, idx);
1200 err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
1201 mirror, offset, length, status, opnum,
1202 GFP_NOIO);
Trond Myklebustf95c03b2015-09-02 15:15:11 -07001203 pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg);
Tom Haynesd67ae822014-12-11 17:02:04 -05001204 dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status);
1205}
1206
1207/* NFS_PROTO call done callback routines */
Tom Haynesd67ae822014-12-11 17:02:04 -05001208static int ff_layout_read_done_cb(struct rpc_task *task,
1209 struct nfs_pgio_header *hdr)
1210{
Tom Haynesd67ae822014-12-11 17:02:04 -05001211 int err;
1212
1213 trace_nfs4_pnfs_read(hdr, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001214 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001215 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1216 hdr->args.offset, hdr->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001217 hdr->res.op_status, OP_READ,
1218 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001219 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1220 hdr->ds_clp, hdr->lseg,
1221 hdr->pgio_mirror_idx);
1222
1223 switch (err) {
1224 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust13544412015-11-02 10:11:17 -05001225 if (ff_layout_choose_best_ds_for_read(hdr->lseg,
1226 hdr->pgio_mirror_idx + 1,
1227 &hdr->pgio_mirror_idx))
1228 goto out_eagain;
Tom Haynesd67ae822014-12-11 17:02:04 -05001229 set_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
1230 &hdr->lseg->pls_layout->plh_flags);
1231 pnfs_read_resend_pnfs(hdr);
1232 return task->tk_status;
1233 case -NFS4ERR_RESET_TO_MDS:
Tom Haynesd67ae822014-12-11 17:02:04 -05001234 ff_layout_reset_read(hdr);
1235 return task->tk_status;
1236 case -EAGAIN:
Trond Myklebust13544412015-11-02 10:11:17 -05001237 goto out_eagain;
Tom Haynesd67ae822014-12-11 17:02:04 -05001238 }
1239
1240 return 0;
Trond Myklebust13544412015-11-02 10:11:17 -05001241out_eagain:
1242 rpc_restart_call_prepare(task);
1243 return -EAGAIN;
Tom Haynesd67ae822014-12-11 17:02:04 -05001244}
1245
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001246static bool
1247ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg)
1248{
1249 return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT);
1250}
1251
Tom Haynesd67ae822014-12-11 17:02:04 -05001252/*
1253 * We reference the rpc_cred of the first WRITE that triggers the need for
1254 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
1255 * rfc5661 is not clear about which credential should be used.
1256 *
1257 * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
1258 * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
1259 * we always send layoutcommit after DS writes.
1260 */
1261static void
1262ff_layout_set_layoutcommit(struct nfs_pgio_header *hdr)
1263{
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001264 if (!ff_layout_need_layoutcommit(hdr->lseg))
1265 return;
1266
Trond Myklebust67af7612015-03-25 20:40:38 -04001267 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
1268 hdr->mds_offset + hdr->res.count);
Tom Haynesd67ae822014-12-11 17:02:04 -05001269 dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
1270 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
1271}
1272
1273static bool
1274ff_layout_reset_to_mds(struct pnfs_layout_segment *lseg, int idx)
1275{
1276 /* No mirroring for now */
1277 struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx);
1278
1279 return ff_layout_test_devid_unavailable(node);
1280}
1281
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001282static void ff_layout_read_record_layoutstats_start(struct rpc_task *task,
1283 struct nfs_pgio_header *hdr)
Tom Haynesd67ae822014-12-11 17:02:04 -05001284{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001285 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1286 return;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04001287 nfs4_ff_layout_stat_io_start_read(hdr->inode,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001288 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
Trond Myklebuste76d28d2015-08-20 13:12:51 -05001289 hdr->args.count,
1290 task->tk_start);
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001291}
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001292
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001293static void ff_layout_read_record_layoutstats_done(struct rpc_task *task,
1294 struct nfs_pgio_header *hdr)
1295{
1296 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1297 return;
1298 nfs4_ff_layout_stat_io_end_read(task,
1299 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1300 hdr->args.count,
1301 hdr->res.count);
1302}
1303
1304static int ff_layout_read_prepare_common(struct rpc_task *task,
1305 struct nfs_pgio_header *hdr)
1306{
Tom Haynesd67ae822014-12-11 17:02:04 -05001307 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1308 rpc_exit(task, -EIO);
1309 return -EIO;
1310 }
1311 if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) {
1312 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
1313 if (ff_layout_has_available_ds(hdr->lseg))
1314 pnfs_read_resend_pnfs(hdr);
1315 else
1316 ff_layout_reset_read(hdr);
1317 rpc_exit(task, 0);
1318 return -EAGAIN;
1319 }
1320 hdr->pgio_done_cb = ff_layout_read_done_cb;
1321
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001322 ff_layout_read_record_layoutstats_start(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001323 return 0;
1324}
1325
1326/*
1327 * Call ops for the async read/write cases
1328 * In the case of dense layouts, the offset needs to be reset to its
1329 * original value.
1330 */
1331static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data)
1332{
1333 struct nfs_pgio_header *hdr = data;
1334
1335 if (ff_layout_read_prepare_common(task, hdr))
1336 return;
1337
1338 rpc_call_start(task);
1339}
1340
1341static int ff_layout_setup_sequence(struct nfs_client *ds_clp,
1342 struct nfs4_sequence_args *args,
1343 struct nfs4_sequence_res *res,
1344 struct rpc_task *task)
1345{
1346 if (ds_clp->cl_session)
1347 return nfs41_setup_sequence(ds_clp->cl_session,
1348 args,
1349 res,
1350 task);
1351 return nfs40_setup_sequence(ds_clp->cl_slot_tbl,
1352 args,
1353 res,
1354 task);
1355}
1356
1357static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data)
1358{
1359 struct nfs_pgio_header *hdr = data;
1360
Tom Haynesd67ae822014-12-11 17:02:04 -05001361 if (ff_layout_setup_sequence(hdr->ds_clp,
1362 &hdr->args.seq_args,
1363 &hdr->res.seq_res,
1364 task))
1365 return;
1366
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001367 if (ff_layout_read_prepare_common(task, hdr))
1368 return;
1369
Tom Haynesd67ae822014-12-11 17:02:04 -05001370 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1371 hdr->args.lock_context, FMODE_READ) == -EIO)
1372 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1373}
1374
1375static void ff_layout_read_call_done(struct rpc_task *task, void *data)
1376{
1377 struct nfs_pgio_header *hdr = data;
1378
1379 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
1380
1381 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1382 task->tk_status == 0) {
1383 nfs4_sequence_done(task, &hdr->res.seq_res);
1384 return;
1385 }
1386
1387 /* Note this may cause RPC to be resent */
1388 hdr->mds_ops->rpc_call_done(task, hdr);
1389}
1390
1391static void ff_layout_read_count_stats(struct rpc_task *task, void *data)
1392{
1393 struct nfs_pgio_header *hdr = data;
1394
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001395 ff_layout_read_record_layoutstats_done(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001396 rpc_count_iostats_metrics(task,
1397 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]);
1398}
1399
1400static int ff_layout_write_done_cb(struct rpc_task *task,
1401 struct nfs_pgio_header *hdr)
1402{
Tom Haynesd67ae822014-12-11 17:02:04 -05001403 int err;
1404
1405 trace_nfs4_pnfs_write(hdr, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001406 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001407 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1408 hdr->args.offset, hdr->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001409 hdr->res.op_status, OP_WRITE,
1410 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001411 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1412 hdr->ds_clp, hdr->lseg,
1413 hdr->pgio_mirror_idx);
1414
1415 switch (err) {
1416 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001417 ff_layout_reset_write(hdr, true);
1418 return task->tk_status;
Tom Haynesd67ae822014-12-11 17:02:04 -05001419 case -NFS4ERR_RESET_TO_MDS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001420 ff_layout_reset_write(hdr, false);
Tom Haynesd67ae822014-12-11 17:02:04 -05001421 return task->tk_status;
1422 case -EAGAIN:
Tom Haynesd67ae822014-12-11 17:02:04 -05001423 return -EAGAIN;
1424 }
1425
1426 if (hdr->res.verf->committed == NFS_FILE_SYNC ||
1427 hdr->res.verf->committed == NFS_DATA_SYNC)
1428 ff_layout_set_layoutcommit(hdr);
1429
Peng Tao54204012015-08-22 06:40:00 +08001430 /* zero out fattr since we don't care DS attr at all */
1431 hdr->fattr.valid = 0;
Peng Tao69f230d2015-08-20 01:52:59 +08001432 if (task->tk_status >= 0)
1433 nfs_writeback_update_inode(hdr);
1434
Tom Haynesd67ae822014-12-11 17:02:04 -05001435 return 0;
1436}
1437
1438static int ff_layout_commit_done_cb(struct rpc_task *task,
1439 struct nfs_commit_data *data)
1440{
Tom Haynesd67ae822014-12-11 17:02:04 -05001441 int err;
1442
1443 trace_nfs4_pnfs_commit_ds(data, task->tk_status);
Trond Myklebustdd521282015-08-20 17:59:49 -05001444 if (task->tk_status < 0)
Tom Haynesd67ae822014-12-11 17:02:04 -05001445 ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index,
1446 data->args.offset, data->args.count,
Trond Myklebustdd521282015-08-20 17:59:49 -05001447 data->res.op_status, OP_COMMIT,
1448 task->tk_status);
Tom Haynesd67ae822014-12-11 17:02:04 -05001449 err = ff_layout_async_handle_error(task, NULL, data->ds_clp,
1450 data->lseg, data->ds_commit_index);
1451
1452 switch (err) {
1453 case -NFS4ERR_RESET_TO_PNFS:
Trond Myklebust7cc8c5c2015-09-02 15:22:48 -07001454 pnfs_generic_prepare_to_resend_writes(data);
1455 return -EAGAIN;
Tom Haynesd67ae822014-12-11 17:02:04 -05001456 case -NFS4ERR_RESET_TO_MDS:
Tom Haynesd67ae822014-12-11 17:02:04 -05001457 pnfs_generic_prepare_to_resend_writes(data);
1458 return -EAGAIN;
1459 case -EAGAIN:
1460 rpc_restart_call_prepare(task);
1461 return -EAGAIN;
1462 }
1463
Trond Myklebustc0f5f502015-06-26 14:51:32 -04001464 if (data->verf.committed == NFS_UNSTABLE
1465 && ff_layout_need_layoutcommit(data->lseg))
Trond Myklebust67af7612015-03-25 20:40:38 -04001466 pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb);
Tom Haynesd67ae822014-12-11 17:02:04 -05001467
1468 return 0;
1469}
1470
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001471static void ff_layout_write_record_layoutstats_start(struct rpc_task *task,
1472 struct nfs_pgio_header *hdr)
Tom Haynesd67ae822014-12-11 17:02:04 -05001473{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001474 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1475 return;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04001476 nfs4_ff_layout_stat_io_start_write(hdr->inode,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001477 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
Trond Myklebuste76d28d2015-08-20 13:12:51 -05001478 hdr->args.count,
1479 task->tk_start);
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001480}
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001481
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001482static void ff_layout_write_record_layoutstats_done(struct rpc_task *task,
1483 struct nfs_pgio_header *hdr)
1484{
1485 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1486 return;
1487 nfs4_ff_layout_stat_io_end_write(task,
1488 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1489 hdr->args.count, hdr->res.count,
1490 hdr->res.verf->committed);
1491}
1492
1493static int ff_layout_write_prepare_common(struct rpc_task *task,
1494 struct nfs_pgio_header *hdr)
1495{
Tom Haynesd67ae822014-12-11 17:02:04 -05001496 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1497 rpc_exit(task, -EIO);
1498 return -EIO;
1499 }
1500
1501 if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) {
1502 bool retry_pnfs;
1503
1504 retry_pnfs = ff_layout_has_available_ds(hdr->lseg);
1505 dprintk("%s task %u reset io to %s\n", __func__,
1506 task->tk_pid, retry_pnfs ? "pNFS" : "MDS");
1507 ff_layout_reset_write(hdr, retry_pnfs);
1508 rpc_exit(task, 0);
1509 return -EAGAIN;
1510 }
1511
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001512 ff_layout_write_record_layoutstats_start(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001513 return 0;
1514}
1515
1516static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data)
1517{
1518 struct nfs_pgio_header *hdr = data;
1519
1520 if (ff_layout_write_prepare_common(task, hdr))
1521 return;
1522
1523 rpc_call_start(task);
1524}
1525
1526static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data)
1527{
1528 struct nfs_pgio_header *hdr = data;
1529
Tom Haynesd67ae822014-12-11 17:02:04 -05001530 if (ff_layout_setup_sequence(hdr->ds_clp,
1531 &hdr->args.seq_args,
1532 &hdr->res.seq_res,
1533 task))
1534 return;
1535
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001536 if (ff_layout_write_prepare_common(task, hdr))
1537 return;
1538
Tom Haynesd67ae822014-12-11 17:02:04 -05001539 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1540 hdr->args.lock_context, FMODE_WRITE) == -EIO)
1541 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1542}
1543
1544static void ff_layout_write_call_done(struct rpc_task *task, void *data)
1545{
1546 struct nfs_pgio_header *hdr = data;
1547
1548 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1549 task->tk_status == 0) {
1550 nfs4_sequence_done(task, &hdr->res.seq_res);
1551 return;
1552 }
1553
1554 /* Note this may cause RPC to be resent */
1555 hdr->mds_ops->rpc_call_done(task, hdr);
1556}
1557
1558static void ff_layout_write_count_stats(struct rpc_task *task, void *data)
1559{
1560 struct nfs_pgio_header *hdr = data;
1561
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001562 ff_layout_write_record_layoutstats_done(task, hdr);
Tom Haynesd67ae822014-12-11 17:02:04 -05001563 rpc_count_iostats_metrics(task,
1564 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]);
1565}
1566
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001567static void ff_layout_commit_record_layoutstats_start(struct rpc_task *task,
1568 struct nfs_commit_data *cdata)
1569{
1570 if (test_and_set_bit(NFS_IOHDR_STAT, &cdata->flags))
1571 return;
1572 nfs4_ff_layout_stat_io_start_write(cdata->inode,
1573 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1574 0, task->tk_start);
1575}
1576
1577static void ff_layout_commit_record_layoutstats_done(struct rpc_task *task,
1578 struct nfs_commit_data *cdata)
1579{
1580 struct nfs_page *req;
1581 __u64 count = 0;
1582
1583 if (!test_and_clear_bit(NFS_IOHDR_STAT, &cdata->flags))
1584 return;
1585
1586 if (task->tk_status == 0) {
1587 list_for_each_entry(req, &cdata->pages, wb_list)
1588 count += req->wb_bytes;
1589 }
1590 nfs4_ff_layout_stat_io_end_write(task,
1591 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1592 count, count, NFS_FILE_SYNC);
1593}
1594
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001595static void ff_layout_commit_prepare_common(struct rpc_task *task,
1596 struct nfs_commit_data *cdata)
1597{
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001598 ff_layout_commit_record_layoutstats_start(task, cdata);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001599}
1600
Tom Haynesd67ae822014-12-11 17:02:04 -05001601static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data)
1602{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001603 ff_layout_commit_prepare_common(task, data);
Tom Haynesd67ae822014-12-11 17:02:04 -05001604 rpc_call_start(task);
1605}
1606
1607static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data)
1608{
1609 struct nfs_commit_data *wdata = data;
1610
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001611 if (ff_layout_setup_sequence(wdata->ds_clp,
Tom Haynesd67ae822014-12-11 17:02:04 -05001612 &wdata->args.seq_args,
1613 &wdata->res.seq_res,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001614 task))
1615 return;
1616 ff_layout_commit_prepare_common(task, data);
1617}
1618
1619static void ff_layout_commit_done(struct rpc_task *task, void *data)
1620{
Peng Tao141b9b52015-12-07 18:33:14 -05001621 pnfs_generic_write_commit_done(task, data);
1622}
1623
1624static void ff_layout_commit_count_stats(struct rpc_task *task, void *data)
1625{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001626 struct nfs_commit_data *cdata = data;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001627
Trond Myklebust37e9ed22015-12-22 12:30:24 -05001628 ff_layout_commit_record_layoutstats_done(task, cdata);
Tom Haynesd67ae822014-12-11 17:02:04 -05001629 rpc_count_iostats_metrics(task,
1630 &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]);
1631}
1632
1633static const struct rpc_call_ops ff_layout_read_call_ops_v3 = {
1634 .rpc_call_prepare = ff_layout_read_prepare_v3,
1635 .rpc_call_done = ff_layout_read_call_done,
1636 .rpc_count_stats = ff_layout_read_count_stats,
1637 .rpc_release = pnfs_generic_rw_release,
1638};
1639
1640static const struct rpc_call_ops ff_layout_read_call_ops_v4 = {
1641 .rpc_call_prepare = ff_layout_read_prepare_v4,
1642 .rpc_call_done = ff_layout_read_call_done,
1643 .rpc_count_stats = ff_layout_read_count_stats,
1644 .rpc_release = pnfs_generic_rw_release,
1645};
1646
1647static const struct rpc_call_ops ff_layout_write_call_ops_v3 = {
1648 .rpc_call_prepare = ff_layout_write_prepare_v3,
1649 .rpc_call_done = ff_layout_write_call_done,
1650 .rpc_count_stats = ff_layout_write_count_stats,
1651 .rpc_release = pnfs_generic_rw_release,
1652};
1653
1654static const struct rpc_call_ops ff_layout_write_call_ops_v4 = {
1655 .rpc_call_prepare = ff_layout_write_prepare_v4,
1656 .rpc_call_done = ff_layout_write_call_done,
1657 .rpc_count_stats = ff_layout_write_count_stats,
1658 .rpc_release = pnfs_generic_rw_release,
1659};
1660
1661static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = {
1662 .rpc_call_prepare = ff_layout_commit_prepare_v3,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001663 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001664 .rpc_count_stats = ff_layout_commit_count_stats,
1665 .rpc_release = pnfs_generic_commit_release,
1666};
1667
1668static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = {
1669 .rpc_call_prepare = ff_layout_commit_prepare_v4,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001670 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001671 .rpc_count_stats = ff_layout_commit_count_stats,
1672 .rpc_release = pnfs_generic_commit_release,
1673};
1674
1675static enum pnfs_try_status
1676ff_layout_read_pagelist(struct nfs_pgio_header *hdr)
1677{
1678 struct pnfs_layout_segment *lseg = hdr->lseg;
1679 struct nfs4_pnfs_ds *ds;
1680 struct rpc_clnt *ds_clnt;
1681 struct rpc_cred *ds_cred;
1682 loff_t offset = hdr->args.offset;
1683 u32 idx = hdr->pgio_mirror_idx;
1684 int vers;
1685 struct nfs_fh *fh;
1686
1687 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
1688 __func__, hdr->inode->i_ino,
1689 hdr->args.pgbase, (size_t)hdr->args.count, offset);
1690
1691 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
1692 if (!ds)
1693 goto out_failed;
1694
1695 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1696 hdr->inode);
1697 if (IS_ERR(ds_clnt))
1698 goto out_failed;
1699
1700 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
1701 if (IS_ERR(ds_cred))
1702 goto out_failed;
1703
1704 vers = nfs4_ff_layout_ds_version(lseg, idx);
1705
1706 dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__,
1707 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers);
1708
1709 atomic_inc(&ds->ds_clp->cl_count);
1710 hdr->ds_clp = ds->ds_clp;
1711 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1712 if (fh)
1713 hdr->args.fh = fh;
Tom Haynesd67ae822014-12-11 17:02:04 -05001714 /*
1715 * Note that if we ever decide to split across DSes,
1716 * then we may need to handle dense-like offsets.
1717 */
1718 hdr->args.offset = offset;
1719 hdr->mds_offset = offset;
1720
1721 /* Perform an asynchronous read to ds */
1722 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1723 vers == 3 ? &ff_layout_read_call_ops_v3 :
1724 &ff_layout_read_call_ops_v4,
1725 0, RPC_TASK_SOFTCONN);
1726
1727 return PNFS_ATTEMPTED;
1728
1729out_failed:
1730 if (ff_layout_has_available_ds(lseg))
1731 return PNFS_TRY_AGAIN;
1732 return PNFS_NOT_ATTEMPTED;
1733}
1734
1735/* Perform async writes. */
1736static enum pnfs_try_status
1737ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
1738{
1739 struct pnfs_layout_segment *lseg = hdr->lseg;
1740 struct nfs4_pnfs_ds *ds;
1741 struct rpc_clnt *ds_clnt;
1742 struct rpc_cred *ds_cred;
1743 loff_t offset = hdr->args.offset;
1744 int vers;
1745 struct nfs_fh *fh;
1746 int idx = hdr->pgio_mirror_idx;
1747
1748 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1749 if (!ds)
1750 return PNFS_NOT_ATTEMPTED;
1751
1752 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1753 hdr->inode);
1754 if (IS_ERR(ds_clnt))
1755 return PNFS_NOT_ATTEMPTED;
1756
1757 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
1758 if (IS_ERR(ds_cred))
1759 return PNFS_NOT_ATTEMPTED;
1760
1761 vers = nfs4_ff_layout_ds_version(lseg, idx);
1762
1763 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n",
1764 __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
1765 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count),
1766 vers);
1767
1768 hdr->pgio_done_cb = ff_layout_write_done_cb;
1769 atomic_inc(&ds->ds_clp->cl_count);
1770 hdr->ds_clp = ds->ds_clp;
1771 hdr->ds_commit_idx = idx;
1772 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1773 if (fh)
1774 hdr->args.fh = fh;
1775
1776 /*
1777 * Note that if we ever decide to split across DSes,
1778 * then we may need to handle dense-like offsets.
1779 */
1780 hdr->args.offset = offset;
1781
1782 /* Perform an asynchronous write */
1783 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1784 vers == 3 ? &ff_layout_write_call_ops_v3 :
1785 &ff_layout_write_call_ops_v4,
1786 sync, RPC_TASK_SOFTCONN);
1787 return PNFS_ATTEMPTED;
1788}
1789
Tom Haynesd67ae822014-12-11 17:02:04 -05001790static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1791{
1792 return i;
1793}
1794
1795static struct nfs_fh *
1796select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1797{
1798 struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg);
1799
1800 /* FIXME: Assume that there is only one NFS version available
1801 * for the DS.
1802 */
1803 return &flseg->mirror_array[i]->fh_versions[0];
1804}
1805
1806static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how)
1807{
1808 struct pnfs_layout_segment *lseg = data->lseg;
1809 struct nfs4_pnfs_ds *ds;
1810 struct rpc_clnt *ds_clnt;
1811 struct rpc_cred *ds_cred;
1812 u32 idx;
1813 int vers;
1814 struct nfs_fh *fh;
1815
1816 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1817 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1818 if (!ds)
1819 goto out_err;
1820
1821 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1822 data->inode);
1823 if (IS_ERR(ds_clnt))
1824 goto out_err;
1825
1826 ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred);
1827 if (IS_ERR(ds_cred))
1828 goto out_err;
1829
1830 vers = nfs4_ff_layout_ds_version(lseg, idx);
1831
1832 dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__,
1833 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count),
1834 vers);
1835 data->commit_done_cb = ff_layout_commit_done_cb;
1836 data->cred = ds_cred;
1837 atomic_inc(&ds->ds_clp->cl_count);
1838 data->ds_clp = ds->ds_clp;
1839 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1840 if (fh)
1841 data->args.fh = fh;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001842
Tom Haynesd67ae822014-12-11 17:02:04 -05001843 return nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops,
1844 vers == 3 ? &ff_layout_commit_call_ops_v3 :
1845 &ff_layout_commit_call_ops_v4,
1846 how, RPC_TASK_SOFTCONN);
1847out_err:
1848 pnfs_generic_prepare_to_resend_writes(data);
1849 pnfs_generic_commit_release(data);
1850 return -EAGAIN;
1851}
1852
1853static int
1854ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1855 int how, struct nfs_commit_info *cinfo)
1856{
1857 return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
1858 ff_layout_initiate_commit);
1859}
1860
1861static struct pnfs_ds_commit_info *
1862ff_layout_get_ds_info(struct inode *inode)
1863{
1864 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1865
1866 if (layout == NULL)
1867 return NULL;
1868
1869 return &FF_LAYOUT_FROM_HDR(layout)->commit_info;
1870}
1871
1872static void
Trond Myklebustfc877012015-03-09 17:25:14 -04001873ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d)
Tom Haynesd67ae822014-12-11 17:02:04 -05001874{
1875 nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds,
1876 id_node));
1877}
1878
1879static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo,
1880 struct xdr_stream *xdr,
1881 const struct nfs4_layoutreturn_args *args)
1882{
1883 struct pnfs_layout_hdr *hdr = &flo->generic_hdr;
1884 __be32 *start;
1885 int count = 0, ret = 0;
1886
1887 start = xdr_reserve_space(xdr, 4);
1888 if (unlikely(!start))
1889 return -E2BIG;
1890
1891 /* This assume we always return _ALL_ layouts */
1892 spin_lock(&hdr->plh_inode->i_lock);
1893 ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range);
1894 spin_unlock(&hdr->plh_inode->i_lock);
1895
1896 *start = cpu_to_be32(count);
1897
1898 return ret;
1899}
1900
1901/* report nothing for now */
1902static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo,
1903 struct xdr_stream *xdr,
1904 const struct nfs4_layoutreturn_args *args)
1905{
1906 __be32 *p;
1907
1908 p = xdr_reserve_space(xdr, 4);
1909 if (likely(p))
1910 *p = cpu_to_be32(0);
1911}
1912
1913static struct nfs4_deviceid_node *
1914ff_layout_alloc_deviceid_node(struct nfs_server *server,
1915 struct pnfs_device *pdev, gfp_t gfp_flags)
1916{
1917 struct nfs4_ff_layout_ds *dsaddr;
1918
1919 dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags);
1920 if (!dsaddr)
1921 return NULL;
1922 return &dsaddr->id_node;
1923}
1924
1925static void
1926ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo,
1927 struct xdr_stream *xdr,
1928 const struct nfs4_layoutreturn_args *args)
1929{
1930 struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
1931 __be32 *start;
1932
1933 dprintk("%s: Begin\n", __func__);
1934 start = xdr_reserve_space(xdr, 4);
1935 BUG_ON(!start);
1936
1937 if (ff_layout_encode_ioerr(flo, xdr, args))
1938 goto out;
1939
1940 ff_layout_encode_iostats(flo, xdr, args);
1941out:
1942 *start = cpu_to_be32((xdr->p - start - 1) * 4);
1943 dprintk("%s: Return\n", __func__);
1944}
1945
Peng Tao27c43062015-06-23 19:52:02 +08001946static int
1947ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen)
1948{
1949 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
1950
1951 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
1952}
1953
1954static size_t
1955ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf,
1956 const int buflen)
1957{
1958 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
1959 const struct in6_addr *addr = &sin6->sin6_addr;
1960
1961 /*
1962 * RFC 4291, Section 2.2.2
1963 *
1964 * Shorthanded ANY address
1965 */
1966 if (ipv6_addr_any(addr))
1967 return snprintf(buf, buflen, "::");
1968
1969 /*
1970 * RFC 4291, Section 2.2.2
1971 *
1972 * Shorthanded loopback address
1973 */
1974 if (ipv6_addr_loopback(addr))
1975 return snprintf(buf, buflen, "::1");
1976
1977 /*
1978 * RFC 4291, Section 2.2.3
1979 *
1980 * Special presentation address format for mapped v4
1981 * addresses.
1982 */
1983 if (ipv6_addr_v4mapped(addr))
1984 return snprintf(buf, buflen, "::ffff:%pI4",
1985 &addr->s6_addr32[3]);
1986
1987 /*
1988 * RFC 4291, Section 2.2.1
1989 */
1990 return snprintf(buf, buflen, "%pI6c", addr);
1991}
1992
1993/* Derived from rpc_sockaddr2uaddr */
1994static void
1995ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da)
1996{
1997 struct sockaddr *sap = (struct sockaddr *)&da->da_addr;
1998 char portbuf[RPCBIND_MAXUADDRPLEN];
1999 char addrbuf[RPCBIND_MAXUADDRLEN];
2000 char *netid;
2001 unsigned short port;
2002 int len, netid_len;
2003 __be32 *p;
2004
2005 switch (sap->sa_family) {
2006 case AF_INET:
2007 if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
2008 return;
2009 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
2010 netid = "tcp";
2011 netid_len = 3;
2012 break;
2013 case AF_INET6:
2014 if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
2015 return;
2016 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
2017 netid = "tcp6";
2018 netid_len = 4;
2019 break;
2020 default:
2021 /* we only support tcp and tcp6 */
2022 WARN_ON_ONCE(1);
2023 return;
2024 }
2025
2026 snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff);
2027 len = strlcat(addrbuf, portbuf, sizeof(addrbuf));
2028
2029 p = xdr_reserve_space(xdr, 4 + netid_len);
2030 xdr_encode_opaque(p, netid, netid_len);
2031
2032 p = xdr_reserve_space(xdr, 4 + len);
2033 xdr_encode_opaque(p, addrbuf, len);
2034}
2035
2036static void
2037ff_layout_encode_nfstime(struct xdr_stream *xdr,
2038 ktime_t t)
2039{
2040 struct timespec64 ts;
2041 __be32 *p;
2042
2043 p = xdr_reserve_space(xdr, 12);
2044 ts = ktime_to_timespec64(t);
2045 p = xdr_encode_hyper(p, ts.tv_sec);
2046 *p++ = cpu_to_be32(ts.tv_nsec);
2047}
2048
2049static void
2050ff_layout_encode_io_latency(struct xdr_stream *xdr,
2051 struct nfs4_ff_io_stat *stat)
2052{
2053 __be32 *p;
2054
2055 p = xdr_reserve_space(xdr, 5 * 8);
2056 p = xdr_encode_hyper(p, stat->ops_requested);
2057 p = xdr_encode_hyper(p, stat->bytes_requested);
2058 p = xdr_encode_hyper(p, stat->ops_completed);
2059 p = xdr_encode_hyper(p, stat->bytes_completed);
2060 p = xdr_encode_hyper(p, stat->bytes_not_delivered);
2061 ff_layout_encode_nfstime(xdr, stat->total_busy_time);
2062 ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time);
2063}
2064
2065static void
2066ff_layout_encode_layoutstats(struct xdr_stream *xdr,
2067 struct nfs42_layoutstat_args *args,
2068 struct nfs42_layoutstat_devinfo *devinfo)
2069{
2070 struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private;
2071 struct nfs4_pnfs_ds_addr *da;
2072 struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds;
2073 struct nfs_fh *fh = &mirror->fh_versions[0];
2074 __be32 *p, *start;
2075
2076 da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node);
2077 dprintk("%s: DS %s: encoding address %s\n",
2078 __func__, ds->ds_remotestr, da->da_remotestr);
2079 /* layoutupdate length */
2080 start = xdr_reserve_space(xdr, 4);
2081 /* netaddr4 */
2082 ff_layout_encode_netaddr(xdr, da);
2083 /* nfs_fh4 */
2084 p = xdr_reserve_space(xdr, 4 + fh->size);
2085 xdr_encode_opaque(p, fh->data, fh->size);
2086 /* ff_io_latency4 read */
2087 spin_lock(&mirror->lock);
2088 ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat);
2089 /* ff_io_latency4 write */
2090 ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat);
2091 spin_unlock(&mirror->lock);
2092 /* nfstime4 */
2093 ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time));
2094 /* bool */
2095 p = xdr_reserve_space(xdr, 4);
2096 *p = cpu_to_be32(false);
2097
2098 *start = cpu_to_be32((xdr->p - start - 1) * 4);
2099}
2100
Trond Myklebust266d12d2015-08-24 20:03:17 -04002101static int
Peng Taoad4dc53e2015-06-23 19:52:01 +08002102ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args,
Trond Myklebust266d12d2015-08-24 20:03:17 -04002103 struct pnfs_layout_hdr *lo,
2104 int dev_limit)
Peng Taoad4dc53e2015-06-23 19:52:01 +08002105{
Trond Myklebust266d12d2015-08-24 20:03:17 -04002106 struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002107 struct nfs4_ff_layout_mirror *mirror;
2108 struct nfs4_deviceid_node *dev;
2109 struct nfs42_layoutstat_devinfo *devinfo;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002110 int i = 0;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002111
Trond Myklebust266d12d2015-08-24 20:03:17 -04002112 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2113 if (i >= dev_limit)
Peng Taoad4dc53e2015-06-23 19:52:01 +08002114 break;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002115 if (!mirror->mirror_ds)
Peng Tao27c43062015-06-23 19:52:02 +08002116 continue;
Trond Myklebust266d12d2015-08-24 20:03:17 -04002117 /* mirror refcount put in cleanup_layoutstats */
2118 if (!atomic_inc_not_zero(&mirror->ref))
2119 continue;
2120 dev = &mirror->mirror_ds->id_node;
2121 devinfo = &args->devinfo[i];
Peng Taoad4dc53e2015-06-23 19:52:01 +08002122 memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002123 devinfo->offset = 0;
2124 devinfo->length = NFS4_MAX_UINT64;
Peng Taod099d7b2015-08-10 16:47:32 +08002125 devinfo->read_count = mirror->read_stat.io_stat.ops_completed;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002126 devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed;
Peng Taod099d7b2015-08-10 16:47:32 +08002127 devinfo->write_count = mirror->write_stat.io_stat.ops_completed;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002128 devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed;
2129 devinfo->layout_type = LAYOUT_FLEX_FILES;
Peng Tao27c43062015-06-23 19:52:02 +08002130 devinfo->layoutstats_encode = ff_layout_encode_layoutstats;
2131 devinfo->layout_private = mirror;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002132
Trond Myklebust266d12d2015-08-24 20:03:17 -04002133 i++;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002134 }
Trond Myklebust266d12d2015-08-24 20:03:17 -04002135 return i;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002136}
2137
2138static int
2139ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args)
2140{
Trond Myklebust266d12d2015-08-24 20:03:17 -04002141 struct nfs4_flexfile_layout *ff_layout;
2142 struct nfs4_ff_layout_mirror *mirror;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002143 int dev_count = 0;
2144
2145 spin_lock(&args->inode->i_lock);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002146 ff_layout = FF_LAYOUT_FROM_HDR(NFS_I(args->inode)->layout);
2147 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2148 if (atomic_read(&mirror->ref) != 0)
2149 dev_count ++;
Peng Taoad4dc53e2015-06-23 19:52:01 +08002150 }
2151 spin_unlock(&args->inode->i_lock);
2152 /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
2153 if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) {
2154 dprintk("%s: truncating devinfo to limit (%d:%d)\n",
2155 __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV);
2156 dev_count = PNFS_LAYOUTSTATS_MAXDEV;
2157 }
Trond Myklebust266d12d2015-08-24 20:03:17 -04002158 args->devinfo = kmalloc_array(dev_count, sizeof(*args->devinfo), GFP_NOIO);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002159 if (!args->devinfo)
2160 return -ENOMEM;
2161
Peng Taoad4dc53e2015-06-23 19:52:01 +08002162 spin_lock(&args->inode->i_lock);
Trond Myklebust266d12d2015-08-24 20:03:17 -04002163 args->num_dev = ff_layout_mirror_prepare_stats(args,
2164 &ff_layout->generic_hdr, dev_count);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002165 spin_unlock(&args->inode->i_lock);
Peng Taoad4dc53e2015-06-23 19:52:01 +08002166
2167 return 0;
2168}
2169
Peng Tao27c43062015-06-23 19:52:02 +08002170static void
2171ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data)
2172{
2173 struct nfs4_ff_layout_mirror *mirror;
2174 int i;
2175
2176 for (i = 0; i < data->args.num_dev; i++) {
2177 mirror = data->args.devinfo[i].layout_private;
2178 data->args.devinfo[i].layout_private = NULL;
Trond Myklebust0b7baf92015-08-24 18:22:28 -04002179 ff_layout_put_mirror(mirror);
Peng Tao27c43062015-06-23 19:52:02 +08002180 }
2181}
2182
Tom Haynesd67ae822014-12-11 17:02:04 -05002183static struct pnfs_layoutdriver_type flexfilelayout_type = {
2184 .id = LAYOUT_FLEX_FILES,
2185 .name = "LAYOUT_FLEX_FILES",
2186 .owner = THIS_MODULE,
2187 .alloc_layout_hdr = ff_layout_alloc_layout_hdr,
2188 .free_layout_hdr = ff_layout_free_layout_hdr,
2189 .alloc_lseg = ff_layout_alloc_lseg,
2190 .free_lseg = ff_layout_free_lseg,
Trond Myklebust0762ed22015-08-25 17:38:25 -04002191 .add_lseg = ff_layout_add_lseg,
Tom Haynesd67ae822014-12-11 17:02:04 -05002192 .pg_read_ops = &ff_layout_pg_read_ops,
2193 .pg_write_ops = &ff_layout_pg_write_ops,
2194 .get_ds_info = ff_layout_get_ds_info,
Trond Myklebustfc877012015-03-09 17:25:14 -04002195 .free_deviceid_node = ff_layout_free_deviceid_node,
Tom Haynes338d00c2015-02-17 14:58:15 -08002196 .mark_request_commit = pnfs_layout_mark_request_commit,
Tom Haynesd67ae822014-12-11 17:02:04 -05002197 .clear_request_commit = pnfs_generic_clear_request_commit,
2198 .scan_commit_lists = pnfs_generic_scan_commit_lists,
2199 .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
2200 .commit_pagelist = ff_layout_commit_pagelist,
2201 .read_pagelist = ff_layout_read_pagelist,
2202 .write_pagelist = ff_layout_write_pagelist,
2203 .alloc_deviceid_node = ff_layout_alloc_deviceid_node,
2204 .encode_layoutreturn = ff_layout_encode_layoutreturn,
Trond Myklebust5bb89b42015-03-25 14:14:42 -04002205 .sync = pnfs_nfs_generic_sync,
Peng Taoad4dc53e2015-06-23 19:52:01 +08002206 .prepare_layoutstats = ff_layout_prepare_layoutstats,
Peng Tao27c43062015-06-23 19:52:02 +08002207 .cleanup_layoutstats = ff_layout_cleanup_layoutstats,
Tom Haynesd67ae822014-12-11 17:02:04 -05002208};
2209
2210static int __init nfs4flexfilelayout_init(void)
2211{
2212 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n",
2213 __func__);
2214 return pnfs_register_layoutdriver(&flexfilelayout_type);
2215}
2216
2217static void __exit nfs4flexfilelayout_exit(void)
2218{
2219 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
2220 __func__);
2221 pnfs_unregister_layoutdriver(&flexfilelayout_type);
2222}
2223
2224MODULE_ALIAS("nfs-layouttype4-4");
2225
2226MODULE_LICENSE("GPL");
2227MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
2228
2229module_init(nfs4flexfilelayout_init);
2230module_exit(nfs4flexfilelayout_exit);