blob: 6bcb6d6c9dc314c41592fdf285d423329fbe964e [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);
37 return &ffl->generic_hdr;
38 } else
39 return NULL;
40}
41
42static void
43ff_layout_free_layout_hdr(struct pnfs_layout_hdr *lo)
44{
45 struct nfs4_ff_layout_ds_err *err, *n;
46
47 list_for_each_entry_safe(err, n, &FF_LAYOUT_FROM_HDR(lo)->error_list,
48 list) {
49 list_del(&err->list);
50 kfree(err);
51 }
52 kfree(FF_LAYOUT_FROM_HDR(lo));
53}
54
55static int decode_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
56{
57 __be32 *p;
58
59 p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE);
60 if (unlikely(p == NULL))
61 return -ENOBUFS;
62 memcpy(stateid, p, NFS4_STATEID_SIZE);
63 dprintk("%s: stateid id= [%x%x%x%x]\n", __func__,
64 p[0], p[1], p[2], p[3]);
65 return 0;
66}
67
68static int decode_deviceid(struct xdr_stream *xdr, struct nfs4_deviceid *devid)
69{
70 __be32 *p;
71
72 p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE);
73 if (unlikely(!p))
74 return -ENOBUFS;
75 memcpy(devid, p, NFS4_DEVICEID4_SIZE);
76 nfs4_print_deviceid(devid);
77 return 0;
78}
79
80static int decode_nfs_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
81{
82 __be32 *p;
83
84 p = xdr_inline_decode(xdr, 4);
85 if (unlikely(!p))
86 return -ENOBUFS;
87 fh->size = be32_to_cpup(p++);
88 if (fh->size > sizeof(struct nfs_fh)) {
89 printk(KERN_ERR "NFS flexfiles: Too big fh received %d\n",
90 fh->size);
91 return -EOVERFLOW;
92 }
93 /* fh.data */
94 p = xdr_inline_decode(xdr, fh->size);
95 if (unlikely(!p))
96 return -ENOBUFS;
97 memcpy(&fh->data, p, fh->size);
98 dprintk("%s: fh len %d\n", __func__, fh->size);
99
100 return 0;
101}
102
103/*
104 * Currently only stringified uids and gids are accepted.
105 * I.e., kerberos is not supported to the DSes, so no pricipals.
106 *
107 * That means that one common function will suffice, but when
108 * principals are added, this should be split to accomodate
109 * calls to both nfs_map_name_to_uid() and nfs_map_group_to_gid().
110 */
111static int
112decode_name(struct xdr_stream *xdr, u32 *id)
113{
114 __be32 *p;
115 int len;
116
117 /* opaque_length(4)*/
118 p = xdr_inline_decode(xdr, 4);
119 if (unlikely(!p))
120 return -ENOBUFS;
121 len = be32_to_cpup(p++);
122 if (len < 0)
123 return -EINVAL;
124
125 dprintk("%s: len %u\n", __func__, len);
126
127 /* opaque body */
128 p = xdr_inline_decode(xdr, len);
129 if (unlikely(!p))
130 return -ENOBUFS;
131
132 if (!nfs_map_string_to_numeric((char *)p, len, id))
133 return -EINVAL;
134
135 return 0;
136}
137
138static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment *fls)
139{
140 int i;
141
142 if (fls->mirror_array) {
143 for (i = 0; i < fls->mirror_array_cnt; i++) {
144 /* normally mirror_ds is freed in
145 * .free_deviceid_node but we still do it here
146 * for .alloc_lseg error path */
147 if (fls->mirror_array[i]) {
148 kfree(fls->mirror_array[i]->fh_versions);
149 nfs4_ff_layout_put_deviceid(fls->mirror_array[i]->mirror_ds);
150 kfree(fls->mirror_array[i]);
151 }
152 }
153 kfree(fls->mirror_array);
154 fls->mirror_array = NULL;
155 }
156}
157
158static int ff_layout_check_layout(struct nfs4_layoutget_res *lgr)
159{
160 int ret = 0;
161
162 dprintk("--> %s\n", __func__);
163
164 /* FIXME: remove this check when layout segment support is added */
165 if (lgr->range.offset != 0 ||
166 lgr->range.length != NFS4_MAX_UINT64) {
167 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
168 __func__);
169 ret = -EINVAL;
170 }
171
172 dprintk("--> %s returns %d\n", __func__, ret);
173 return ret;
174}
175
176static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment *fls)
177{
178 if (fls) {
179 ff_layout_free_mirror_array(fls);
180 kfree(fls);
181 }
182}
183
184static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment *fls)
185{
Tom Haynesd67ae822014-12-11 17:02:04 -0500186 int i, j;
187
188 for (i = 0; i < fls->mirror_array_cnt - 1; i++) {
189 for (j = i + 1; j < fls->mirror_array_cnt; j++)
190 if (fls->mirror_array[i]->efficiency <
Fabian Frederick455b6ee2015-06-12 18:58:50 +0200191 fls->mirror_array[j]->efficiency)
192 swap(fls->mirror_array[i],
193 fls->mirror_array[j]);
Tom Haynesd67ae822014-12-11 17:02:04 -0500194 }
195}
196
197static struct pnfs_layout_segment *
198ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh,
199 struct nfs4_layoutget_res *lgr,
200 gfp_t gfp_flags)
201{
202 struct pnfs_layout_segment *ret;
203 struct nfs4_ff_layout_segment *fls = NULL;
204 struct xdr_stream stream;
205 struct xdr_buf buf;
206 struct page *scratch;
207 u64 stripe_unit;
208 u32 mirror_array_cnt;
209 __be32 *p;
210 int i, rc;
211
212 dprintk("--> %s\n", __func__);
213 scratch = alloc_page(gfp_flags);
214 if (!scratch)
215 return ERR_PTR(-ENOMEM);
216
217 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages,
218 lgr->layoutp->len);
219 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
220
221 /* stripe unit and mirror_array_cnt */
222 rc = -EIO;
223 p = xdr_inline_decode(&stream, 8 + 4);
224 if (!p)
225 goto out_err_free;
226
227 p = xdr_decode_hyper(p, &stripe_unit);
228 mirror_array_cnt = be32_to_cpup(p++);
229 dprintk("%s: stripe_unit=%llu mirror_array_cnt=%u\n", __func__,
230 stripe_unit, mirror_array_cnt);
231
232 if (mirror_array_cnt > NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT ||
233 mirror_array_cnt == 0)
234 goto out_err_free;
235
236 rc = -ENOMEM;
237 fls = kzalloc(sizeof(*fls), gfp_flags);
238 if (!fls)
239 goto out_err_free;
240
241 fls->mirror_array_cnt = mirror_array_cnt;
242 fls->stripe_unit = stripe_unit;
243 fls->mirror_array = kcalloc(fls->mirror_array_cnt,
244 sizeof(fls->mirror_array[0]), gfp_flags);
245 if (fls->mirror_array == NULL)
246 goto out_err_free;
247
248 for (i = 0; i < fls->mirror_array_cnt; i++) {
249 struct nfs4_deviceid devid;
250 struct nfs4_deviceid_node *idnode;
251 u32 ds_count;
252 u32 fh_count;
253 int j;
254
255 rc = -EIO;
256 p = xdr_inline_decode(&stream, 4);
257 if (!p)
258 goto out_err_free;
259 ds_count = be32_to_cpup(p);
260
261 /* FIXME: allow for striping? */
262 if (ds_count != 1)
263 goto out_err_free;
264
265 fls->mirror_array[i] =
266 kzalloc(sizeof(struct nfs4_ff_layout_mirror),
267 gfp_flags);
268 if (fls->mirror_array[i] == NULL) {
269 rc = -ENOMEM;
270 goto out_err_free;
271 }
272
273 spin_lock_init(&fls->mirror_array[i]->lock);
274 fls->mirror_array[i]->ds_count = ds_count;
Peng Tao27c43062015-06-23 19:52:02 +0800275 fls->mirror_array[i]->lseg = &fls->generic_hdr;
Tom Haynesd67ae822014-12-11 17:02:04 -0500276
277 /* deviceid */
278 rc = decode_deviceid(&stream, &devid);
279 if (rc)
280 goto out_err_free;
281
282 idnode = nfs4_find_get_deviceid(NFS_SERVER(lh->plh_inode),
283 &devid, lh->plh_lc_cred,
284 gfp_flags);
285 /*
286 * upon success, mirror_ds is allocated by previous
287 * getdeviceinfo, or newly by .alloc_deviceid_node
288 * nfs4_find_get_deviceid failure is indeed getdeviceinfo falure
289 */
290 if (idnode)
291 fls->mirror_array[i]->mirror_ds =
292 FF_LAYOUT_MIRROR_DS(idnode);
293 else
294 goto out_err_free;
295
296 /* efficiency */
297 rc = -EIO;
298 p = xdr_inline_decode(&stream, 4);
299 if (!p)
300 goto out_err_free;
301 fls->mirror_array[i]->efficiency = be32_to_cpup(p);
302
303 /* stateid */
304 rc = decode_stateid(&stream, &fls->mirror_array[i]->stateid);
305 if (rc)
306 goto out_err_free;
307
308 /* fh */
309 p = xdr_inline_decode(&stream, 4);
310 if (!p)
311 goto out_err_free;
312 fh_count = be32_to_cpup(p);
313
314 fls->mirror_array[i]->fh_versions =
315 kzalloc(fh_count * sizeof(struct nfs_fh),
316 gfp_flags);
317 if (fls->mirror_array[i]->fh_versions == NULL) {
318 rc = -ENOMEM;
319 goto out_err_free;
320 }
321
322 for (j = 0; j < fh_count; j++) {
323 rc = decode_nfs_fh(&stream,
324 &fls->mirror_array[i]->fh_versions[j]);
325 if (rc)
326 goto out_err_free;
327 }
328
329 fls->mirror_array[i]->fh_versions_cnt = fh_count;
330
331 /* user */
332 rc = decode_name(&stream, &fls->mirror_array[i]->uid);
333 if (rc)
334 goto out_err_free;
335
336 /* group */
337 rc = decode_name(&stream, &fls->mirror_array[i]->gid);
338 if (rc)
339 goto out_err_free;
340
341 dprintk("%s: uid %d gid %d\n", __func__,
342 fls->mirror_array[i]->uid,
343 fls->mirror_array[i]->gid);
344 }
345
346 ff_layout_sort_mirrors(fls);
347 rc = ff_layout_check_layout(lgr);
348 if (rc)
349 goto out_err_free;
350
351 ret = &fls->generic_hdr;
352 dprintk("<-- %s (success)\n", __func__);
353out_free_page:
354 __free_page(scratch);
355 return ret;
356out_err_free:
357 _ff_layout_free_lseg(fls);
358 ret = ERR_PTR(rc);
359 dprintk("<-- %s (%d)\n", __func__, rc);
360 goto out_free_page;
361}
362
363static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout)
364{
365 struct pnfs_layout_segment *lseg;
366
367 list_for_each_entry(lseg, &layout->plh_segs, pls_list)
368 if (lseg->pls_range.iomode == IOMODE_RW)
369 return true;
370
371 return false;
372}
373
374static void
375ff_layout_free_lseg(struct pnfs_layout_segment *lseg)
376{
377 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
378 int i;
379
380 dprintk("--> %s\n", __func__);
381
382 for (i = 0; i < fls->mirror_array_cnt; i++) {
383 if (fls->mirror_array[i]) {
384 nfs4_ff_layout_put_deviceid(fls->mirror_array[i]->mirror_ds);
385 fls->mirror_array[i]->mirror_ds = NULL;
386 if (fls->mirror_array[i]->cred) {
387 put_rpccred(fls->mirror_array[i]->cred);
388 fls->mirror_array[i]->cred = NULL;
389 }
390 }
391 }
392
393 if (lseg->pls_range.iomode == IOMODE_RW) {
394 struct nfs4_flexfile_layout *ffl;
395 struct inode *inode;
396
397 ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout);
398 inode = ffl->generic_hdr.plh_inode;
399 spin_lock(&inode->i_lock);
400 if (!ff_layout_has_rw_segments(lseg->pls_layout)) {
401 ffl->commit_info.nbuckets = 0;
402 kfree(ffl->commit_info.buckets);
403 ffl->commit_info.buckets = NULL;
404 }
405 spin_unlock(&inode->i_lock);
406 }
407 _ff_layout_free_lseg(fls);
408}
409
410/* Return 1 until we have multiple lsegs support */
411static int
412ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls)
413{
414 return 1;
415}
416
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800417static void
418nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer *timer)
419{
420 ktime_t old, new;
421
422 /*
423 * Note: careful here!
424 * If the counter is zero, then we must not increment it until after
425 * we've set the start_time.
426 * If we were instead to use atomic_inc_return(), then another
427 * request might come in, bump, and then call end_busy_timer()
428 * before we've set the timer->start_time.
429 */
430 old = timer->start_time;
431 if (atomic_inc_not_zero(&timer->n_ops) == 0) {
432 new = ktime_get();
433 cmpxchg(&timer->start_time.tv64, old.tv64, new.tv64);
434 atomic_inc(&timer->n_ops);
435 }
436}
437
438static ktime_t
439nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer *timer)
440{
441 ktime_t start, now;
442
443 now = ktime_get();
444 start.tv64 = xchg(&timer->start_time.tv64, now.tv64);
445 atomic_dec(&timer->n_ops);
446 return ktime_sub(now, start);
447}
448
449static ktime_t
450nfs4_ff_layout_calc_completion_time(struct rpc_task *task)
451{
452 return ktime_sub(ktime_get(), task->tk_start);
453}
454
455static void
Peng Taod9838032015-06-23 19:52:00 +0800456nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror,
457 struct nfs4_ff_layoutstat *layoutstat)
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800458{
Peng Taod9838032015-06-23 19:52:00 +0800459 static const ktime_t notime = {0};
460
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800461 nfs4_ff_start_busy_timer(&layoutstat->busy_timer);
Peng Taod9838032015-06-23 19:52:00 +0800462 cmpxchg(&mirror->start_time, notime, ktime_get());
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800463}
464
465static void
466nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat,
467 __u64 requested)
468{
469 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
470
471 iostat->ops_requested++;
472 iostat->bytes_requested += requested;
473}
474
475static void
476nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat,
477 __u64 requested,
478 __u64 completed,
479 ktime_t time_completed)
480{
481 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
482 ktime_t timer;
483
484 iostat->ops_completed++;
485 iostat->bytes_completed += completed;
486 iostat->bytes_not_delivered += requested - completed;
487
488 timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer);
489 iostat->total_busy_time =
490 ktime_add(iostat->total_busy_time, timer);
491 iostat->aggregate_completion_time =
492 ktime_add(iostat->aggregate_completion_time, time_completed);
493}
494
495static void
496nfs4_ff_layout_stat_io_start_read(struct nfs4_ff_layout_mirror *mirror,
497 __u64 requested)
498{
499 spin_lock(&mirror->lock);
Peng Taod9838032015-06-23 19:52:00 +0800500 nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800501 nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested);
502 spin_unlock(&mirror->lock);
503}
504
505static void
506nfs4_ff_layout_stat_io_end_read(struct rpc_task *task,
507 struct nfs4_ff_layout_mirror *mirror,
508 __u64 requested,
509 __u64 completed)
510{
511 spin_lock(&mirror->lock);
512 nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat,
513 requested, completed,
514 nfs4_ff_layout_calc_completion_time(task));
515 spin_unlock(&mirror->lock);
516}
517
518static void
519nfs4_ff_layout_stat_io_start_write(struct nfs4_ff_layout_mirror *mirror,
520 __u64 requested)
521{
522 spin_lock(&mirror->lock);
Peng Taod9838032015-06-23 19:52:00 +0800523 nfs4_ff_layoutstat_start_io(mirror, &mirror->write_stat);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800524 nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested);
525 spin_unlock(&mirror->lock);
526}
527
528static void
529nfs4_ff_layout_stat_io_end_write(struct rpc_task *task,
530 struct nfs4_ff_layout_mirror *mirror,
531 __u64 requested,
532 __u64 completed,
533 enum nfs3_stable_how committed)
534{
535 if (committed == NFS_UNSTABLE)
536 requested = completed = 0;
537
538 spin_lock(&mirror->lock);
539 nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat,
540 requested, completed,
541 nfs4_ff_layout_calc_completion_time(task));
542 spin_unlock(&mirror->lock);
543}
544
Tom Haynesd67ae822014-12-11 17:02:04 -0500545static int
546ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg,
547 struct nfs_commit_info *cinfo,
548 gfp_t gfp_flags)
549{
550 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
551 struct pnfs_commit_bucket *buckets;
552 int size;
553
554 if (cinfo->ds->nbuckets != 0) {
555 /* This assumes there is only one RW lseg per file.
556 * To support multiple lseg per file, we need to
557 * change struct pnfs_commit_bucket to allow dynamic
558 * increasing nbuckets.
559 */
560 return 0;
561 }
562
563 size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg);
564
565 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
566 gfp_flags);
567 if (!buckets)
568 return -ENOMEM;
569 else {
570 int i;
571
572 spin_lock(cinfo->lock);
573 if (cinfo->ds->nbuckets != 0)
574 kfree(buckets);
575 else {
576 cinfo->ds->buckets = buckets;
577 cinfo->ds->nbuckets = size;
578 for (i = 0; i < size; i++) {
579 INIT_LIST_HEAD(&buckets[i].written);
580 INIT_LIST_HEAD(&buckets[i].committing);
581 /* mark direct verifier as unset */
582 buckets[i].direct_verf.committed =
583 NFS_INVALID_STABLE_HOW;
584 }
585 }
586 spin_unlock(cinfo->lock);
587 return 0;
588 }
589}
590
591static struct nfs4_pnfs_ds *
592ff_layout_choose_best_ds_for_read(struct nfs_pageio_descriptor *pgio,
593 int *best_idx)
594{
595 struct nfs4_ff_layout_segment *fls;
596 struct nfs4_pnfs_ds *ds;
597 int idx;
598
599 fls = FF_LAYOUT_LSEG(pgio->pg_lseg);
600 /* mirrors are sorted by efficiency */
601 for (idx = 0; idx < fls->mirror_array_cnt; idx++) {
602 ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, idx, false);
603 if (ds) {
604 *best_idx = idx;
605 return ds;
606 }
607 }
608
609 return NULL;
610}
611
612static void
613ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
614 struct nfs_page *req)
615{
616 struct nfs_pgio_mirror *pgm;
617 struct nfs4_ff_layout_mirror *mirror;
618 struct nfs4_pnfs_ds *ds;
619 int ds_idx;
620
621 /* Use full layout for now */
622 if (!pgio->pg_lseg)
623 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
624 req->wb_context,
625 0,
626 NFS4_MAX_UINT64,
627 IOMODE_READ,
628 GFP_KERNEL);
629 /* If no lseg, fall back to read through mds */
630 if (pgio->pg_lseg == NULL)
631 goto out_mds;
632
633 ds = ff_layout_choose_best_ds_for_read(pgio, &ds_idx);
634 if (!ds)
635 goto out_mds;
636 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx);
637
638 pgio->pg_mirror_idx = ds_idx;
639
640 /* read always uses only one mirror - idx 0 for pgio layer */
641 pgm = &pgio->pg_mirrors[0];
642 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize;
643
644 return;
645out_mds:
646 pnfs_put_lseg(pgio->pg_lseg);
647 pgio->pg_lseg = NULL;
648 nfs_pageio_reset_read_mds(pgio);
649}
650
651static void
652ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
653 struct nfs_page *req)
654{
655 struct nfs4_ff_layout_mirror *mirror;
656 struct nfs_pgio_mirror *pgm;
657 struct nfs_commit_info cinfo;
658 struct nfs4_pnfs_ds *ds;
659 int i;
660 int status;
661
662 if (!pgio->pg_lseg)
663 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
664 req->wb_context,
665 0,
666 NFS4_MAX_UINT64,
667 IOMODE_RW,
668 GFP_NOFS);
669 /* If no lseg, fall back to write through mds */
670 if (pgio->pg_lseg == NULL)
671 goto out_mds;
672
673 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
674 status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
675 if (status < 0)
676 goto out_mds;
677
678 /* Use a direct mapping of ds_idx to pgio mirror_idx */
679 if (WARN_ON_ONCE(pgio->pg_mirror_count !=
680 FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg)))
681 goto out_mds;
682
683 for (i = 0; i < pgio->pg_mirror_count; i++) {
684 ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true);
685 if (!ds)
686 goto out_mds;
687 pgm = &pgio->pg_mirrors[i];
688 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i);
689 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize;
690 }
691
692 return;
693
694out_mds:
695 pnfs_put_lseg(pgio->pg_lseg);
696 pgio->pg_lseg = NULL;
697 nfs_pageio_reset_write_mds(pgio);
698}
699
700static unsigned int
701ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
702 struct nfs_page *req)
703{
704 if (!pgio->pg_lseg)
705 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
706 req->wb_context,
707 0,
708 NFS4_MAX_UINT64,
709 IOMODE_RW,
710 GFP_NOFS);
711 if (pgio->pg_lseg)
712 return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
713
714 /* no lseg means that pnfs is not in use, so no mirroring here */
715 pnfs_put_lseg(pgio->pg_lseg);
716 pgio->pg_lseg = NULL;
717 nfs_pageio_reset_write_mds(pgio);
718 return 1;
719}
720
721static const struct nfs_pageio_ops ff_layout_pg_read_ops = {
722 .pg_init = ff_layout_pg_init_read,
723 .pg_test = pnfs_generic_pg_test,
724 .pg_doio = pnfs_generic_pg_readpages,
725 .pg_cleanup = pnfs_generic_pg_cleanup,
726};
727
728static const struct nfs_pageio_ops ff_layout_pg_write_ops = {
729 .pg_init = ff_layout_pg_init_write,
730 .pg_test = pnfs_generic_pg_test,
731 .pg_doio = pnfs_generic_pg_writepages,
732 .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write,
733 .pg_cleanup = pnfs_generic_pg_cleanup,
734};
735
736static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs)
737{
738 struct rpc_task *task = &hdr->task;
739
740 pnfs_layoutcommit_inode(hdr->inode, false);
741
742 if (retry_pnfs) {
743 dprintk("%s Reset task %5u for i/o through pNFS "
744 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
745 hdr->task.tk_pid,
746 hdr->inode->i_sb->s_id,
747 (unsigned long long)NFS_FILEID(hdr->inode),
748 hdr->args.count,
749 (unsigned long long)hdr->args.offset);
750
751 if (!hdr->dreq) {
752 struct nfs_open_context *ctx;
753
754 ctx = nfs_list_entry(hdr->pages.next)->wb_context;
755 set_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
756 hdr->completion_ops->error_cleanup(&hdr->pages);
757 } else {
758 nfs_direct_set_resched_writes(hdr->dreq);
759 /* fake unstable write to let common nfs resend pages */
760 hdr->verf.committed = NFS_UNSTABLE;
761 hdr->good_bytes = 0;
762 }
763 return;
764 }
765
766 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
767 dprintk("%s Reset task %5u for i/o through MDS "
768 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
769 hdr->task.tk_pid,
770 hdr->inode->i_sb->s_id,
771 (unsigned long long)NFS_FILEID(hdr->inode),
772 hdr->args.count,
773 (unsigned long long)hdr->args.offset);
774
775 task->tk_status = pnfs_write_done_resend_to_mds(hdr);
776 }
777}
778
779static void ff_layout_reset_read(struct nfs_pgio_header *hdr)
780{
781 struct rpc_task *task = &hdr->task;
782
783 pnfs_layoutcommit_inode(hdr->inode, false);
784
785 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
786 dprintk("%s Reset task %5u for i/o through MDS "
787 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
788 hdr->task.tk_pid,
789 hdr->inode->i_sb->s_id,
790 (unsigned long long)NFS_FILEID(hdr->inode),
791 hdr->args.count,
792 (unsigned long long)hdr->args.offset);
793
794 task->tk_status = pnfs_read_done_resend_to_mds(hdr);
795 }
796}
797
798static int ff_layout_async_handle_error_v4(struct rpc_task *task,
799 struct nfs4_state *state,
800 struct nfs_client *clp,
801 struct pnfs_layout_segment *lseg,
802 int idx)
803{
804 struct pnfs_layout_hdr *lo = lseg->pls_layout;
805 struct inode *inode = lo->plh_inode;
806 struct nfs_server *mds_server = NFS_SERVER(inode);
807
808 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
809 struct nfs_client *mds_client = mds_server->nfs_client;
810 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
811
812 if (task->tk_status >= 0)
813 return 0;
814
815 switch (task->tk_status) {
816 /* MDS state errors */
817 case -NFS4ERR_DELEG_REVOKED:
818 case -NFS4ERR_ADMIN_REVOKED:
819 case -NFS4ERR_BAD_STATEID:
820 if (state == NULL)
821 break;
822 nfs_remove_bad_delegation(state->inode);
823 case -NFS4ERR_OPENMODE:
824 if (state == NULL)
825 break;
826 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
827 goto out_bad_stateid;
828 goto wait_on_recovery;
829 case -NFS4ERR_EXPIRED:
830 if (state != NULL) {
831 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
832 goto out_bad_stateid;
833 }
834 nfs4_schedule_lease_recovery(mds_client);
835 goto wait_on_recovery;
836 /* DS session errors */
837 case -NFS4ERR_BADSESSION:
838 case -NFS4ERR_BADSLOT:
839 case -NFS4ERR_BAD_HIGH_SLOT:
840 case -NFS4ERR_DEADSESSION:
841 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
842 case -NFS4ERR_SEQ_FALSE_RETRY:
843 case -NFS4ERR_SEQ_MISORDERED:
844 dprintk("%s ERROR %d, Reset session. Exchangeid "
845 "flags 0x%x\n", __func__, task->tk_status,
846 clp->cl_exchange_flags);
847 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
848 break;
849 case -NFS4ERR_DELAY:
850 case -NFS4ERR_GRACE:
851 rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX);
852 break;
853 case -NFS4ERR_RETRY_UNCACHED_REP:
854 break;
855 /* Invalidate Layout errors */
856 case -NFS4ERR_PNFS_NO_LAYOUT:
857 case -ESTALE: /* mapped NFS4ERR_STALE */
858 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
859 case -EISDIR: /* mapped NFS4ERR_ISDIR */
860 case -NFS4ERR_FHEXPIRED:
861 case -NFS4ERR_WRONG_TYPE:
862 dprintk("%s Invalid layout error %d\n", __func__,
863 task->tk_status);
864 /*
865 * Destroy layout so new i/o will get a new layout.
866 * Layout will not be destroyed until all current lseg
867 * references are put. Mark layout as invalid to resend failed
868 * i/o and all i/o waiting on the slot table to the MDS until
869 * layout is destroyed and a new valid layout is obtained.
870 */
871 pnfs_destroy_layout(NFS_I(inode));
872 rpc_wake_up(&tbl->slot_tbl_waitq);
873 goto reset;
874 /* RPC connection errors */
875 case -ECONNREFUSED:
876 case -EHOSTDOWN:
877 case -EHOSTUNREACH:
878 case -ENETUNREACH:
879 case -EIO:
880 case -ETIMEDOUT:
881 case -EPIPE:
882 dprintk("%s DS connection error %d\n", __func__,
883 task->tk_status);
884 nfs4_mark_deviceid_unavailable(devid);
885 rpc_wake_up(&tbl->slot_tbl_waitq);
886 /* fall through */
887 default:
888 if (ff_layout_has_available_ds(lseg))
889 return -NFS4ERR_RESET_TO_PNFS;
890reset:
891 dprintk("%s Retry through MDS. Error %d\n", __func__,
892 task->tk_status);
893 return -NFS4ERR_RESET_TO_MDS;
894 }
895out:
896 task->tk_status = 0;
897 return -EAGAIN;
898out_bad_stateid:
899 task->tk_status = -EIO;
900 return 0;
901wait_on_recovery:
902 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
903 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
904 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
905 goto out;
906}
907
908/* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
909static int ff_layout_async_handle_error_v3(struct rpc_task *task,
910 struct pnfs_layout_segment *lseg,
911 int idx)
912{
913 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
914
915 if (task->tk_status >= 0)
916 return 0;
917
918 if (task->tk_status != -EJUKEBOX) {
919 dprintk("%s DS connection error %d\n", __func__,
920 task->tk_status);
921 nfs4_mark_deviceid_unavailable(devid);
922 if (ff_layout_has_available_ds(lseg))
923 return -NFS4ERR_RESET_TO_PNFS;
924 else
925 return -NFS4ERR_RESET_TO_MDS;
926 }
927
928 if (task->tk_status == -EJUKEBOX)
929 nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY);
930 task->tk_status = 0;
931 rpc_restart_call(task);
932 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
933 return -EAGAIN;
934}
935
936static int ff_layout_async_handle_error(struct rpc_task *task,
937 struct nfs4_state *state,
938 struct nfs_client *clp,
939 struct pnfs_layout_segment *lseg,
940 int idx)
941{
942 int vers = clp->cl_nfs_mod->rpc_vers->number;
943
944 switch (vers) {
945 case 3:
946 return ff_layout_async_handle_error_v3(task, lseg, idx);
947 case 4:
948 return ff_layout_async_handle_error_v4(task, state, clp,
949 lseg, idx);
950 default:
951 /* should never happen */
952 WARN_ON_ONCE(1);
953 return 0;
954 }
955}
956
957static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
958 int idx, u64 offset, u64 length,
959 u32 status, int opnum)
960{
961 struct nfs4_ff_layout_mirror *mirror;
962 int err;
963
964 mirror = FF_LAYOUT_COMP(lseg, idx);
965 err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
966 mirror, offset, length, status, opnum,
967 GFP_NOIO);
968 dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status);
969}
970
971/* NFS_PROTO call done callback routines */
972
973static int ff_layout_read_done_cb(struct rpc_task *task,
974 struct nfs_pgio_header *hdr)
975{
976 struct inode *inode;
977 int err;
978
979 trace_nfs4_pnfs_read(hdr, task->tk_status);
980 if (task->tk_status == -ETIMEDOUT && !hdr->res.op_status)
981 hdr->res.op_status = NFS4ERR_NXIO;
982 if (task->tk_status < 0 && hdr->res.op_status)
983 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
984 hdr->args.offset, hdr->args.count,
985 hdr->res.op_status, OP_READ);
986 err = ff_layout_async_handle_error(task, hdr->args.context->state,
987 hdr->ds_clp, hdr->lseg,
988 hdr->pgio_mirror_idx);
989
990 switch (err) {
991 case -NFS4ERR_RESET_TO_PNFS:
992 set_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
993 &hdr->lseg->pls_layout->plh_flags);
994 pnfs_read_resend_pnfs(hdr);
995 return task->tk_status;
996 case -NFS4ERR_RESET_TO_MDS:
997 inode = hdr->lseg->pls_layout->plh_inode;
998 pnfs_error_mark_layout_for_return(inode, hdr->lseg);
999 ff_layout_reset_read(hdr);
1000 return task->tk_status;
1001 case -EAGAIN:
1002 rpc_restart_call_prepare(task);
1003 return -EAGAIN;
1004 }
1005
1006 return 0;
1007}
1008
1009/*
1010 * We reference the rpc_cred of the first WRITE that triggers the need for
1011 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
1012 * rfc5661 is not clear about which credential should be used.
1013 *
1014 * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
1015 * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
1016 * we always send layoutcommit after DS writes.
1017 */
1018static void
1019ff_layout_set_layoutcommit(struct nfs_pgio_header *hdr)
1020{
Trond Myklebust67af7612015-03-25 20:40:38 -04001021 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
1022 hdr->mds_offset + hdr->res.count);
Tom Haynesd67ae822014-12-11 17:02:04 -05001023 dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
1024 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
1025}
1026
1027static bool
1028ff_layout_reset_to_mds(struct pnfs_layout_segment *lseg, int idx)
1029{
1030 /* No mirroring for now */
1031 struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx);
1032
1033 return ff_layout_test_devid_unavailable(node);
1034}
1035
1036static int ff_layout_read_prepare_common(struct rpc_task *task,
1037 struct nfs_pgio_header *hdr)
1038{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001039 nfs4_ff_layout_stat_io_start_read(
1040 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1041 hdr->args.count);
1042
Tom Haynesd67ae822014-12-11 17:02:04 -05001043 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1044 rpc_exit(task, -EIO);
1045 return -EIO;
1046 }
1047 if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) {
1048 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
1049 if (ff_layout_has_available_ds(hdr->lseg))
1050 pnfs_read_resend_pnfs(hdr);
1051 else
1052 ff_layout_reset_read(hdr);
1053 rpc_exit(task, 0);
1054 return -EAGAIN;
1055 }
1056 hdr->pgio_done_cb = ff_layout_read_done_cb;
1057
1058 return 0;
1059}
1060
1061/*
1062 * Call ops for the async read/write cases
1063 * In the case of dense layouts, the offset needs to be reset to its
1064 * original value.
1065 */
1066static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data)
1067{
1068 struct nfs_pgio_header *hdr = data;
1069
1070 if (ff_layout_read_prepare_common(task, hdr))
1071 return;
1072
1073 rpc_call_start(task);
1074}
1075
1076static int ff_layout_setup_sequence(struct nfs_client *ds_clp,
1077 struct nfs4_sequence_args *args,
1078 struct nfs4_sequence_res *res,
1079 struct rpc_task *task)
1080{
1081 if (ds_clp->cl_session)
1082 return nfs41_setup_sequence(ds_clp->cl_session,
1083 args,
1084 res,
1085 task);
1086 return nfs40_setup_sequence(ds_clp->cl_slot_tbl,
1087 args,
1088 res,
1089 task);
1090}
1091
1092static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data)
1093{
1094 struct nfs_pgio_header *hdr = data;
1095
Tom Haynesd67ae822014-12-11 17:02:04 -05001096 if (ff_layout_setup_sequence(hdr->ds_clp,
1097 &hdr->args.seq_args,
1098 &hdr->res.seq_res,
1099 task))
1100 return;
1101
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001102 if (ff_layout_read_prepare_common(task, hdr))
1103 return;
1104
Tom Haynesd67ae822014-12-11 17:02:04 -05001105 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1106 hdr->args.lock_context, FMODE_READ) == -EIO)
1107 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1108}
1109
1110static void ff_layout_read_call_done(struct rpc_task *task, void *data)
1111{
1112 struct nfs_pgio_header *hdr = data;
1113
1114 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
1115
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001116 nfs4_ff_layout_stat_io_end_read(task,
1117 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1118 hdr->args.count, hdr->res.count);
1119
Tom Haynesd67ae822014-12-11 17:02:04 -05001120 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1121 task->tk_status == 0) {
1122 nfs4_sequence_done(task, &hdr->res.seq_res);
1123 return;
1124 }
1125
1126 /* Note this may cause RPC to be resent */
1127 hdr->mds_ops->rpc_call_done(task, hdr);
1128}
1129
1130static void ff_layout_read_count_stats(struct rpc_task *task, void *data)
1131{
1132 struct nfs_pgio_header *hdr = data;
1133
1134 rpc_count_iostats_metrics(task,
1135 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]);
1136}
1137
1138static int ff_layout_write_done_cb(struct rpc_task *task,
1139 struct nfs_pgio_header *hdr)
1140{
1141 struct inode *inode;
1142 int err;
1143
1144 trace_nfs4_pnfs_write(hdr, task->tk_status);
1145 if (task->tk_status == -ETIMEDOUT && !hdr->res.op_status)
1146 hdr->res.op_status = NFS4ERR_NXIO;
1147 if (task->tk_status < 0 && hdr->res.op_status)
1148 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1149 hdr->args.offset, hdr->args.count,
1150 hdr->res.op_status, OP_WRITE);
1151 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1152 hdr->ds_clp, hdr->lseg,
1153 hdr->pgio_mirror_idx);
1154
1155 switch (err) {
1156 case -NFS4ERR_RESET_TO_PNFS:
1157 case -NFS4ERR_RESET_TO_MDS:
1158 inode = hdr->lseg->pls_layout->plh_inode;
1159 pnfs_error_mark_layout_for_return(inode, hdr->lseg);
1160 if (err == -NFS4ERR_RESET_TO_PNFS) {
1161 pnfs_set_retry_layoutget(hdr->lseg->pls_layout);
1162 ff_layout_reset_write(hdr, true);
1163 } else {
1164 pnfs_clear_retry_layoutget(hdr->lseg->pls_layout);
1165 ff_layout_reset_write(hdr, false);
1166 }
1167 return task->tk_status;
1168 case -EAGAIN:
1169 rpc_restart_call_prepare(task);
1170 return -EAGAIN;
1171 }
1172
1173 if (hdr->res.verf->committed == NFS_FILE_SYNC ||
1174 hdr->res.verf->committed == NFS_DATA_SYNC)
1175 ff_layout_set_layoutcommit(hdr);
1176
1177 return 0;
1178}
1179
1180static int ff_layout_commit_done_cb(struct rpc_task *task,
1181 struct nfs_commit_data *data)
1182{
1183 struct inode *inode;
1184 int err;
1185
1186 trace_nfs4_pnfs_commit_ds(data, task->tk_status);
1187 if (task->tk_status == -ETIMEDOUT && !data->res.op_status)
1188 data->res.op_status = NFS4ERR_NXIO;
1189 if (task->tk_status < 0 && data->res.op_status)
1190 ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index,
1191 data->args.offset, data->args.count,
1192 data->res.op_status, OP_COMMIT);
1193 err = ff_layout_async_handle_error(task, NULL, data->ds_clp,
1194 data->lseg, data->ds_commit_index);
1195
1196 switch (err) {
1197 case -NFS4ERR_RESET_TO_PNFS:
1198 case -NFS4ERR_RESET_TO_MDS:
1199 inode = data->lseg->pls_layout->plh_inode;
1200 pnfs_error_mark_layout_for_return(inode, data->lseg);
1201 if (err == -NFS4ERR_RESET_TO_PNFS)
1202 pnfs_set_retry_layoutget(data->lseg->pls_layout);
1203 else
1204 pnfs_clear_retry_layoutget(data->lseg->pls_layout);
1205 pnfs_generic_prepare_to_resend_writes(data);
1206 return -EAGAIN;
1207 case -EAGAIN:
1208 rpc_restart_call_prepare(task);
1209 return -EAGAIN;
1210 }
1211
1212 if (data->verf.committed == NFS_UNSTABLE)
Trond Myklebust67af7612015-03-25 20:40:38 -04001213 pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb);
Tom Haynesd67ae822014-12-11 17:02:04 -05001214
1215 return 0;
1216}
1217
1218static int ff_layout_write_prepare_common(struct rpc_task *task,
1219 struct nfs_pgio_header *hdr)
1220{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001221 nfs4_ff_layout_stat_io_start_write(
1222 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1223 hdr->args.count);
1224
Tom Haynesd67ae822014-12-11 17:02:04 -05001225 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1226 rpc_exit(task, -EIO);
1227 return -EIO;
1228 }
1229
1230 if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) {
1231 bool retry_pnfs;
1232
1233 retry_pnfs = ff_layout_has_available_ds(hdr->lseg);
1234 dprintk("%s task %u reset io to %s\n", __func__,
1235 task->tk_pid, retry_pnfs ? "pNFS" : "MDS");
1236 ff_layout_reset_write(hdr, retry_pnfs);
1237 rpc_exit(task, 0);
1238 return -EAGAIN;
1239 }
1240
1241 return 0;
1242}
1243
1244static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data)
1245{
1246 struct nfs_pgio_header *hdr = data;
1247
1248 if (ff_layout_write_prepare_common(task, hdr))
1249 return;
1250
1251 rpc_call_start(task);
1252}
1253
1254static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data)
1255{
1256 struct nfs_pgio_header *hdr = data;
1257
Tom Haynesd67ae822014-12-11 17:02:04 -05001258 if (ff_layout_setup_sequence(hdr->ds_clp,
1259 &hdr->args.seq_args,
1260 &hdr->res.seq_res,
1261 task))
1262 return;
1263
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001264 if (ff_layout_write_prepare_common(task, hdr))
1265 return;
1266
Tom Haynesd67ae822014-12-11 17:02:04 -05001267 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1268 hdr->args.lock_context, FMODE_WRITE) == -EIO)
1269 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1270}
1271
1272static void ff_layout_write_call_done(struct rpc_task *task, void *data)
1273{
1274 struct nfs_pgio_header *hdr = data;
1275
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001276 nfs4_ff_layout_stat_io_end_write(task,
1277 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1278 hdr->args.count, hdr->res.count,
1279 hdr->res.verf->committed);
1280
Tom Haynesd67ae822014-12-11 17:02:04 -05001281 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1282 task->tk_status == 0) {
1283 nfs4_sequence_done(task, &hdr->res.seq_res);
1284 return;
1285 }
1286
1287 /* Note this may cause RPC to be resent */
1288 hdr->mds_ops->rpc_call_done(task, hdr);
1289}
1290
1291static void ff_layout_write_count_stats(struct rpc_task *task, void *data)
1292{
1293 struct nfs_pgio_header *hdr = data;
1294
1295 rpc_count_iostats_metrics(task,
1296 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]);
1297}
1298
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001299static void ff_layout_commit_prepare_common(struct rpc_task *task,
1300 struct nfs_commit_data *cdata)
1301{
1302 nfs4_ff_layout_stat_io_start_write(
1303 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1304 0);
1305}
1306
Tom Haynesd67ae822014-12-11 17:02:04 -05001307static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data)
1308{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001309 ff_layout_commit_prepare_common(task, data);
Tom Haynesd67ae822014-12-11 17:02:04 -05001310 rpc_call_start(task);
1311}
1312
1313static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data)
1314{
1315 struct nfs_commit_data *wdata = data;
1316
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001317 if (ff_layout_setup_sequence(wdata->ds_clp,
Tom Haynesd67ae822014-12-11 17:02:04 -05001318 &wdata->args.seq_args,
1319 &wdata->res.seq_res,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001320 task))
1321 return;
1322 ff_layout_commit_prepare_common(task, data);
1323}
1324
1325static void ff_layout_commit_done(struct rpc_task *task, void *data)
1326{
1327 struct nfs_commit_data *cdata = data;
1328 struct nfs_page *req;
1329 __u64 count = 0;
1330
1331 if (task->tk_status == 0) {
1332 list_for_each_entry(req, &cdata->pages, wb_list)
1333 count += req->wb_bytes;
1334 }
1335
1336 nfs4_ff_layout_stat_io_end_write(task,
1337 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1338 count, count, NFS_FILE_SYNC);
1339
1340 pnfs_generic_write_commit_done(task, data);
Tom Haynesd67ae822014-12-11 17:02:04 -05001341}
1342
1343static void ff_layout_commit_count_stats(struct rpc_task *task, void *data)
1344{
1345 struct nfs_commit_data *cdata = data;
1346
1347 rpc_count_iostats_metrics(task,
1348 &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]);
1349}
1350
1351static const struct rpc_call_ops ff_layout_read_call_ops_v3 = {
1352 .rpc_call_prepare = ff_layout_read_prepare_v3,
1353 .rpc_call_done = ff_layout_read_call_done,
1354 .rpc_count_stats = ff_layout_read_count_stats,
1355 .rpc_release = pnfs_generic_rw_release,
1356};
1357
1358static const struct rpc_call_ops ff_layout_read_call_ops_v4 = {
1359 .rpc_call_prepare = ff_layout_read_prepare_v4,
1360 .rpc_call_done = ff_layout_read_call_done,
1361 .rpc_count_stats = ff_layout_read_count_stats,
1362 .rpc_release = pnfs_generic_rw_release,
1363};
1364
1365static const struct rpc_call_ops ff_layout_write_call_ops_v3 = {
1366 .rpc_call_prepare = ff_layout_write_prepare_v3,
1367 .rpc_call_done = ff_layout_write_call_done,
1368 .rpc_count_stats = ff_layout_write_count_stats,
1369 .rpc_release = pnfs_generic_rw_release,
1370};
1371
1372static const struct rpc_call_ops ff_layout_write_call_ops_v4 = {
1373 .rpc_call_prepare = ff_layout_write_prepare_v4,
1374 .rpc_call_done = ff_layout_write_call_done,
1375 .rpc_count_stats = ff_layout_write_count_stats,
1376 .rpc_release = pnfs_generic_rw_release,
1377};
1378
1379static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = {
1380 .rpc_call_prepare = ff_layout_commit_prepare_v3,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001381 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001382 .rpc_count_stats = ff_layout_commit_count_stats,
1383 .rpc_release = pnfs_generic_commit_release,
1384};
1385
1386static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = {
1387 .rpc_call_prepare = ff_layout_commit_prepare_v4,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001388 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001389 .rpc_count_stats = ff_layout_commit_count_stats,
1390 .rpc_release = pnfs_generic_commit_release,
1391};
1392
1393static enum pnfs_try_status
1394ff_layout_read_pagelist(struct nfs_pgio_header *hdr)
1395{
1396 struct pnfs_layout_segment *lseg = hdr->lseg;
1397 struct nfs4_pnfs_ds *ds;
1398 struct rpc_clnt *ds_clnt;
1399 struct rpc_cred *ds_cred;
1400 loff_t offset = hdr->args.offset;
1401 u32 idx = hdr->pgio_mirror_idx;
1402 int vers;
1403 struct nfs_fh *fh;
1404
1405 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
1406 __func__, hdr->inode->i_ino,
1407 hdr->args.pgbase, (size_t)hdr->args.count, offset);
1408
1409 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
1410 if (!ds)
1411 goto out_failed;
1412
1413 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1414 hdr->inode);
1415 if (IS_ERR(ds_clnt))
1416 goto out_failed;
1417
1418 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
1419 if (IS_ERR(ds_cred))
1420 goto out_failed;
1421
1422 vers = nfs4_ff_layout_ds_version(lseg, idx);
1423
1424 dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__,
1425 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers);
1426
1427 atomic_inc(&ds->ds_clp->cl_count);
1428 hdr->ds_clp = ds->ds_clp;
1429 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1430 if (fh)
1431 hdr->args.fh = fh;
Tom Haynesd67ae822014-12-11 17:02:04 -05001432 /*
1433 * Note that if we ever decide to split across DSes,
1434 * then we may need to handle dense-like offsets.
1435 */
1436 hdr->args.offset = offset;
1437 hdr->mds_offset = offset;
1438
1439 /* Perform an asynchronous read to ds */
1440 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1441 vers == 3 ? &ff_layout_read_call_ops_v3 :
1442 &ff_layout_read_call_ops_v4,
1443 0, RPC_TASK_SOFTCONN);
1444
1445 return PNFS_ATTEMPTED;
1446
1447out_failed:
1448 if (ff_layout_has_available_ds(lseg))
1449 return PNFS_TRY_AGAIN;
1450 return PNFS_NOT_ATTEMPTED;
1451}
1452
1453/* Perform async writes. */
1454static enum pnfs_try_status
1455ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
1456{
1457 struct pnfs_layout_segment *lseg = hdr->lseg;
1458 struct nfs4_pnfs_ds *ds;
1459 struct rpc_clnt *ds_clnt;
1460 struct rpc_cred *ds_cred;
1461 loff_t offset = hdr->args.offset;
1462 int vers;
1463 struct nfs_fh *fh;
1464 int idx = hdr->pgio_mirror_idx;
1465
1466 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1467 if (!ds)
1468 return PNFS_NOT_ATTEMPTED;
1469
1470 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1471 hdr->inode);
1472 if (IS_ERR(ds_clnt))
1473 return PNFS_NOT_ATTEMPTED;
1474
1475 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
1476 if (IS_ERR(ds_cred))
1477 return PNFS_NOT_ATTEMPTED;
1478
1479 vers = nfs4_ff_layout_ds_version(lseg, idx);
1480
1481 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n",
1482 __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
1483 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count),
1484 vers);
1485
1486 hdr->pgio_done_cb = ff_layout_write_done_cb;
1487 atomic_inc(&ds->ds_clp->cl_count);
1488 hdr->ds_clp = ds->ds_clp;
1489 hdr->ds_commit_idx = idx;
1490 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1491 if (fh)
1492 hdr->args.fh = fh;
1493
1494 /*
1495 * Note that if we ever decide to split across DSes,
1496 * then we may need to handle dense-like offsets.
1497 */
1498 hdr->args.offset = offset;
1499
1500 /* Perform an asynchronous write */
1501 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1502 vers == 3 ? &ff_layout_write_call_ops_v3 :
1503 &ff_layout_write_call_ops_v4,
1504 sync, RPC_TASK_SOFTCONN);
1505 return PNFS_ATTEMPTED;
1506}
1507
Tom Haynesd67ae822014-12-11 17:02:04 -05001508static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1509{
1510 return i;
1511}
1512
1513static struct nfs_fh *
1514select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1515{
1516 struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg);
1517
1518 /* FIXME: Assume that there is only one NFS version available
1519 * for the DS.
1520 */
1521 return &flseg->mirror_array[i]->fh_versions[0];
1522}
1523
1524static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how)
1525{
1526 struct pnfs_layout_segment *lseg = data->lseg;
1527 struct nfs4_pnfs_ds *ds;
1528 struct rpc_clnt *ds_clnt;
1529 struct rpc_cred *ds_cred;
1530 u32 idx;
1531 int vers;
1532 struct nfs_fh *fh;
1533
1534 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1535 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1536 if (!ds)
1537 goto out_err;
1538
1539 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1540 data->inode);
1541 if (IS_ERR(ds_clnt))
1542 goto out_err;
1543
1544 ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred);
1545 if (IS_ERR(ds_cred))
1546 goto out_err;
1547
1548 vers = nfs4_ff_layout_ds_version(lseg, idx);
1549
1550 dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__,
1551 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count),
1552 vers);
1553 data->commit_done_cb = ff_layout_commit_done_cb;
1554 data->cred = ds_cred;
1555 atomic_inc(&ds->ds_clp->cl_count);
1556 data->ds_clp = ds->ds_clp;
1557 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1558 if (fh)
1559 data->args.fh = fh;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001560
Tom Haynesd67ae822014-12-11 17:02:04 -05001561 return nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops,
1562 vers == 3 ? &ff_layout_commit_call_ops_v3 :
1563 &ff_layout_commit_call_ops_v4,
1564 how, RPC_TASK_SOFTCONN);
1565out_err:
1566 pnfs_generic_prepare_to_resend_writes(data);
1567 pnfs_generic_commit_release(data);
1568 return -EAGAIN;
1569}
1570
1571static int
1572ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1573 int how, struct nfs_commit_info *cinfo)
1574{
1575 return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
1576 ff_layout_initiate_commit);
1577}
1578
1579static struct pnfs_ds_commit_info *
1580ff_layout_get_ds_info(struct inode *inode)
1581{
1582 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1583
1584 if (layout == NULL)
1585 return NULL;
1586
1587 return &FF_LAYOUT_FROM_HDR(layout)->commit_info;
1588}
1589
1590static void
Trond Myklebustfc877012015-03-09 17:25:14 -04001591ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d)
Tom Haynesd67ae822014-12-11 17:02:04 -05001592{
1593 nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds,
1594 id_node));
1595}
1596
1597static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo,
1598 struct xdr_stream *xdr,
1599 const struct nfs4_layoutreturn_args *args)
1600{
1601 struct pnfs_layout_hdr *hdr = &flo->generic_hdr;
1602 __be32 *start;
1603 int count = 0, ret = 0;
1604
1605 start = xdr_reserve_space(xdr, 4);
1606 if (unlikely(!start))
1607 return -E2BIG;
1608
1609 /* This assume we always return _ALL_ layouts */
1610 spin_lock(&hdr->plh_inode->i_lock);
1611 ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range);
1612 spin_unlock(&hdr->plh_inode->i_lock);
1613
1614 *start = cpu_to_be32(count);
1615
1616 return ret;
1617}
1618
1619/* report nothing for now */
1620static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo,
1621 struct xdr_stream *xdr,
1622 const struct nfs4_layoutreturn_args *args)
1623{
1624 __be32 *p;
1625
1626 p = xdr_reserve_space(xdr, 4);
1627 if (likely(p))
1628 *p = cpu_to_be32(0);
1629}
1630
1631static struct nfs4_deviceid_node *
1632ff_layout_alloc_deviceid_node(struct nfs_server *server,
1633 struct pnfs_device *pdev, gfp_t gfp_flags)
1634{
1635 struct nfs4_ff_layout_ds *dsaddr;
1636
1637 dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags);
1638 if (!dsaddr)
1639 return NULL;
1640 return &dsaddr->id_node;
1641}
1642
1643static void
1644ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo,
1645 struct xdr_stream *xdr,
1646 const struct nfs4_layoutreturn_args *args)
1647{
1648 struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
1649 __be32 *start;
1650
1651 dprintk("%s: Begin\n", __func__);
1652 start = xdr_reserve_space(xdr, 4);
1653 BUG_ON(!start);
1654
1655 if (ff_layout_encode_ioerr(flo, xdr, args))
1656 goto out;
1657
1658 ff_layout_encode_iostats(flo, xdr, args);
1659out:
1660 *start = cpu_to_be32((xdr->p - start - 1) * 4);
1661 dprintk("%s: Return\n", __func__);
1662}
1663
Peng Tao27c43062015-06-23 19:52:02 +08001664static int
1665ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen)
1666{
1667 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
1668
1669 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
1670}
1671
1672static size_t
1673ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf,
1674 const int buflen)
1675{
1676 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
1677 const struct in6_addr *addr = &sin6->sin6_addr;
1678
1679 /*
1680 * RFC 4291, Section 2.2.2
1681 *
1682 * Shorthanded ANY address
1683 */
1684 if (ipv6_addr_any(addr))
1685 return snprintf(buf, buflen, "::");
1686
1687 /*
1688 * RFC 4291, Section 2.2.2
1689 *
1690 * Shorthanded loopback address
1691 */
1692 if (ipv6_addr_loopback(addr))
1693 return snprintf(buf, buflen, "::1");
1694
1695 /*
1696 * RFC 4291, Section 2.2.3
1697 *
1698 * Special presentation address format for mapped v4
1699 * addresses.
1700 */
1701 if (ipv6_addr_v4mapped(addr))
1702 return snprintf(buf, buflen, "::ffff:%pI4",
1703 &addr->s6_addr32[3]);
1704
1705 /*
1706 * RFC 4291, Section 2.2.1
1707 */
1708 return snprintf(buf, buflen, "%pI6c", addr);
1709}
1710
1711/* Derived from rpc_sockaddr2uaddr */
1712static void
1713ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da)
1714{
1715 struct sockaddr *sap = (struct sockaddr *)&da->da_addr;
1716 char portbuf[RPCBIND_MAXUADDRPLEN];
1717 char addrbuf[RPCBIND_MAXUADDRLEN];
1718 char *netid;
1719 unsigned short port;
1720 int len, netid_len;
1721 __be32 *p;
1722
1723 switch (sap->sa_family) {
1724 case AF_INET:
1725 if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
1726 return;
1727 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
1728 netid = "tcp";
1729 netid_len = 3;
1730 break;
1731 case AF_INET6:
1732 if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
1733 return;
1734 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
1735 netid = "tcp6";
1736 netid_len = 4;
1737 break;
1738 default:
1739 /* we only support tcp and tcp6 */
1740 WARN_ON_ONCE(1);
1741 return;
1742 }
1743
1744 snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff);
1745 len = strlcat(addrbuf, portbuf, sizeof(addrbuf));
1746
1747 p = xdr_reserve_space(xdr, 4 + netid_len);
1748 xdr_encode_opaque(p, netid, netid_len);
1749
1750 p = xdr_reserve_space(xdr, 4 + len);
1751 xdr_encode_opaque(p, addrbuf, len);
1752}
1753
1754static void
1755ff_layout_encode_nfstime(struct xdr_stream *xdr,
1756 ktime_t t)
1757{
1758 struct timespec64 ts;
1759 __be32 *p;
1760
1761 p = xdr_reserve_space(xdr, 12);
1762 ts = ktime_to_timespec64(t);
1763 p = xdr_encode_hyper(p, ts.tv_sec);
1764 *p++ = cpu_to_be32(ts.tv_nsec);
1765}
1766
1767static void
1768ff_layout_encode_io_latency(struct xdr_stream *xdr,
1769 struct nfs4_ff_io_stat *stat)
1770{
1771 __be32 *p;
1772
1773 p = xdr_reserve_space(xdr, 5 * 8);
1774 p = xdr_encode_hyper(p, stat->ops_requested);
1775 p = xdr_encode_hyper(p, stat->bytes_requested);
1776 p = xdr_encode_hyper(p, stat->ops_completed);
1777 p = xdr_encode_hyper(p, stat->bytes_completed);
1778 p = xdr_encode_hyper(p, stat->bytes_not_delivered);
1779 ff_layout_encode_nfstime(xdr, stat->total_busy_time);
1780 ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time);
1781}
1782
1783static void
1784ff_layout_encode_layoutstats(struct xdr_stream *xdr,
1785 struct nfs42_layoutstat_args *args,
1786 struct nfs42_layoutstat_devinfo *devinfo)
1787{
1788 struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private;
1789 struct nfs4_pnfs_ds_addr *da;
1790 struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds;
1791 struct nfs_fh *fh = &mirror->fh_versions[0];
1792 __be32 *p, *start;
1793
1794 da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node);
1795 dprintk("%s: DS %s: encoding address %s\n",
1796 __func__, ds->ds_remotestr, da->da_remotestr);
1797 /* layoutupdate length */
1798 start = xdr_reserve_space(xdr, 4);
1799 /* netaddr4 */
1800 ff_layout_encode_netaddr(xdr, da);
1801 /* nfs_fh4 */
1802 p = xdr_reserve_space(xdr, 4 + fh->size);
1803 xdr_encode_opaque(p, fh->data, fh->size);
1804 /* ff_io_latency4 read */
1805 spin_lock(&mirror->lock);
1806 ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat);
1807 /* ff_io_latency4 write */
1808 ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat);
1809 spin_unlock(&mirror->lock);
1810 /* nfstime4 */
1811 ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time));
1812 /* bool */
1813 p = xdr_reserve_space(xdr, 4);
1814 *p = cpu_to_be32(false);
1815
1816 *start = cpu_to_be32((xdr->p - start - 1) * 4);
1817}
1818
Peng Taoad4dc53e2015-06-23 19:52:01 +08001819static bool
1820ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args,
1821 struct pnfs_layout_segment *pls,
1822 int *dev_count, int dev_limit)
1823{
1824 struct nfs4_ff_layout_mirror *mirror;
1825 struct nfs4_deviceid_node *dev;
1826 struct nfs42_layoutstat_devinfo *devinfo;
1827 int i;
1828
1829 for (i = 0; i <= FF_LAYOUT_MIRROR_COUNT(pls); i++) {
1830 if (*dev_count >= dev_limit)
1831 break;
1832 mirror = FF_LAYOUT_COMP(pls, i);
Peng Tao27c43062015-06-23 19:52:02 +08001833 if (!mirror || !mirror->mirror_ds)
1834 continue;
Peng Taoad4dc53e2015-06-23 19:52:01 +08001835 dev = FF_LAYOUT_DEVID_NODE(pls, i);
1836 devinfo = &args->devinfo[*dev_count];
1837 memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);
1838 devinfo->offset = pls->pls_range.offset;
1839 devinfo->length = pls->pls_range.length;
1840 /* well, we don't really know if IO is continuous or not! */
1841 devinfo->read_count = mirror->read_stat.io_stat.bytes_completed;
1842 devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed;
1843 devinfo->write_count = mirror->write_stat.io_stat.bytes_completed;
1844 devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed;
1845 devinfo->layout_type = LAYOUT_FLEX_FILES;
Peng Tao27c43062015-06-23 19:52:02 +08001846 devinfo->layoutstats_encode = ff_layout_encode_layoutstats;
1847 devinfo->layout_private = mirror;
1848 /* lseg refcount put in cleanup_layoutstats */
1849 pnfs_get_lseg(pls);
Peng Taoad4dc53e2015-06-23 19:52:01 +08001850
1851 ++(*dev_count);
1852 }
1853
1854 return *dev_count < dev_limit;
1855}
1856
1857static int
1858ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args)
1859{
1860 struct pnfs_layout_segment *pls;
1861 int dev_count = 0;
1862
1863 spin_lock(&args->inode->i_lock);
1864 list_for_each_entry(pls, &NFS_I(args->inode)->layout->plh_segs, pls_list) {
1865 dev_count += FF_LAYOUT_MIRROR_COUNT(pls);
1866 }
1867 spin_unlock(&args->inode->i_lock);
1868 /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
1869 if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) {
1870 dprintk("%s: truncating devinfo to limit (%d:%d)\n",
1871 __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV);
1872 dev_count = PNFS_LAYOUTSTATS_MAXDEV;
1873 }
1874 args->devinfo = kmalloc(dev_count * sizeof(*args->devinfo), GFP_KERNEL);
1875 if (!args->devinfo)
1876 return -ENOMEM;
1877
1878 dev_count = 0;
1879 spin_lock(&args->inode->i_lock);
1880 list_for_each_entry(pls, &NFS_I(args->inode)->layout->plh_segs, pls_list) {
1881 if (!ff_layout_mirror_prepare_stats(args, pls, &dev_count,
1882 PNFS_LAYOUTSTATS_MAXDEV)) {
1883 break;
1884 }
1885 }
1886 spin_unlock(&args->inode->i_lock);
1887 args->num_dev = dev_count;
1888
1889 return 0;
1890}
1891
Peng Tao27c43062015-06-23 19:52:02 +08001892static void
1893ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data)
1894{
1895 struct nfs4_ff_layout_mirror *mirror;
1896 int i;
1897
1898 for (i = 0; i < data->args.num_dev; i++) {
1899 mirror = data->args.devinfo[i].layout_private;
1900 data->args.devinfo[i].layout_private = NULL;
1901 pnfs_put_lseg(mirror->lseg);
1902 }
1903}
1904
Tom Haynesd67ae822014-12-11 17:02:04 -05001905static struct pnfs_layoutdriver_type flexfilelayout_type = {
1906 .id = LAYOUT_FLEX_FILES,
1907 .name = "LAYOUT_FLEX_FILES",
1908 .owner = THIS_MODULE,
1909 .alloc_layout_hdr = ff_layout_alloc_layout_hdr,
1910 .free_layout_hdr = ff_layout_free_layout_hdr,
1911 .alloc_lseg = ff_layout_alloc_lseg,
1912 .free_lseg = ff_layout_free_lseg,
1913 .pg_read_ops = &ff_layout_pg_read_ops,
1914 .pg_write_ops = &ff_layout_pg_write_ops,
1915 .get_ds_info = ff_layout_get_ds_info,
Trond Myklebustfc877012015-03-09 17:25:14 -04001916 .free_deviceid_node = ff_layout_free_deviceid_node,
Tom Haynes338d00c2015-02-17 14:58:15 -08001917 .mark_request_commit = pnfs_layout_mark_request_commit,
Tom Haynesd67ae822014-12-11 17:02:04 -05001918 .clear_request_commit = pnfs_generic_clear_request_commit,
1919 .scan_commit_lists = pnfs_generic_scan_commit_lists,
1920 .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
1921 .commit_pagelist = ff_layout_commit_pagelist,
1922 .read_pagelist = ff_layout_read_pagelist,
1923 .write_pagelist = ff_layout_write_pagelist,
1924 .alloc_deviceid_node = ff_layout_alloc_deviceid_node,
1925 .encode_layoutreturn = ff_layout_encode_layoutreturn,
Trond Myklebust5bb89b42015-03-25 14:14:42 -04001926 .sync = pnfs_nfs_generic_sync,
Peng Taoad4dc53e2015-06-23 19:52:01 +08001927 .prepare_layoutstats = ff_layout_prepare_layoutstats,
Peng Tao27c43062015-06-23 19:52:02 +08001928 .cleanup_layoutstats = ff_layout_cleanup_layoutstats,
Tom Haynesd67ae822014-12-11 17:02:04 -05001929};
1930
1931static int __init nfs4flexfilelayout_init(void)
1932{
1933 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n",
1934 __func__);
1935 return pnfs_register_layoutdriver(&flexfilelayout_type);
1936}
1937
1938static void __exit nfs4flexfilelayout_exit(void)
1939{
1940 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
1941 __func__);
1942 pnfs_unregister_layoutdriver(&flexfilelayout_type);
1943}
1944
1945MODULE_ALIAS("nfs-layouttype4-4");
1946
1947MODULE_LICENSE("GPL");
1948MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
1949
1950module_init(nfs4flexfilelayout_init);
1951module_exit(nfs4flexfilelayout_exit);