blob: d3b3b623671143b73ee64616d807448d95759715 [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
Peng Tao97ba3752015-06-23 19:52:04 +0800455static bool
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};
Peng Tao97ba3752015-06-23 19:52:04 +0800460 ktime_t now = ktime_get();
Peng Taod9838032015-06-23 19:52:00 +0800461
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800462 nfs4_ff_start_busy_timer(&layoutstat->busy_timer);
Peng Tao97ba3752015-06-23 19:52:04 +0800463 cmpxchg(&mirror->start_time.tv64, notime.tv64, now.tv64);
464 cmpxchg(&mirror->last_report_time.tv64, notime.tv64, now.tv64);
465 if (ktime_to_ms(ktime_sub(now, mirror->last_report_time)) >=
466 FF_LAYOUTSTATS_REPORT_INTERVAL) {
467 mirror->last_report_time = now;
468 return true;
469 }
470
471 return false;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800472}
473
474static void
475nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat,
476 __u64 requested)
477{
478 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
479
480 iostat->ops_requested++;
481 iostat->bytes_requested += requested;
482}
483
484static void
485nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat,
486 __u64 requested,
487 __u64 completed,
488 ktime_t time_completed)
489{
490 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
491 ktime_t timer;
492
493 iostat->ops_completed++;
494 iostat->bytes_completed += completed;
495 iostat->bytes_not_delivered += requested - completed;
496
497 timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer);
498 iostat->total_busy_time =
499 ktime_add(iostat->total_busy_time, timer);
500 iostat->aggregate_completion_time =
501 ktime_add(iostat->aggregate_completion_time, time_completed);
502}
503
504static void
505nfs4_ff_layout_stat_io_start_read(struct nfs4_ff_layout_mirror *mirror,
506 __u64 requested)
507{
Peng Tao97ba3752015-06-23 19:52:04 +0800508 bool report;
509
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800510 spin_lock(&mirror->lock);
Peng Tao97ba3752015-06-23 19:52:04 +0800511 report = nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800512 nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested);
513 spin_unlock(&mirror->lock);
Peng Tao97ba3752015-06-23 19:52:04 +0800514
515 if (report)
516 pnfs_report_layoutstat(mirror->lseg->pls_layout->plh_inode);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800517}
518
519static void
520nfs4_ff_layout_stat_io_end_read(struct rpc_task *task,
521 struct nfs4_ff_layout_mirror *mirror,
522 __u64 requested,
523 __u64 completed)
524{
525 spin_lock(&mirror->lock);
526 nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat,
527 requested, completed,
528 nfs4_ff_layout_calc_completion_time(task));
529 spin_unlock(&mirror->lock);
530}
531
532static void
533nfs4_ff_layout_stat_io_start_write(struct nfs4_ff_layout_mirror *mirror,
534 __u64 requested)
535{
Peng Tao97ba3752015-06-23 19:52:04 +0800536 bool report;
537
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800538 spin_lock(&mirror->lock);
Peng Tao97ba3752015-06-23 19:52:04 +0800539 report = nfs4_ff_layoutstat_start_io(mirror , &mirror->write_stat);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800540 nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested);
541 spin_unlock(&mirror->lock);
Peng Tao97ba3752015-06-23 19:52:04 +0800542
543 if (report)
544 pnfs_report_layoutstat(mirror->lseg->pls_layout->plh_inode);
Trond Myklebustabcb7bf2015-06-23 19:51:59 +0800545}
546
547static void
548nfs4_ff_layout_stat_io_end_write(struct rpc_task *task,
549 struct nfs4_ff_layout_mirror *mirror,
550 __u64 requested,
551 __u64 completed,
552 enum nfs3_stable_how committed)
553{
554 if (committed == NFS_UNSTABLE)
555 requested = completed = 0;
556
557 spin_lock(&mirror->lock);
558 nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat,
559 requested, completed,
560 nfs4_ff_layout_calc_completion_time(task));
561 spin_unlock(&mirror->lock);
562}
563
Tom Haynesd67ae822014-12-11 17:02:04 -0500564static int
565ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg,
566 struct nfs_commit_info *cinfo,
567 gfp_t gfp_flags)
568{
569 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
570 struct pnfs_commit_bucket *buckets;
571 int size;
572
573 if (cinfo->ds->nbuckets != 0) {
574 /* This assumes there is only one RW lseg per file.
575 * To support multiple lseg per file, we need to
576 * change struct pnfs_commit_bucket to allow dynamic
577 * increasing nbuckets.
578 */
579 return 0;
580 }
581
582 size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg);
583
584 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
585 gfp_flags);
586 if (!buckets)
587 return -ENOMEM;
588 else {
589 int i;
590
591 spin_lock(cinfo->lock);
592 if (cinfo->ds->nbuckets != 0)
593 kfree(buckets);
594 else {
595 cinfo->ds->buckets = buckets;
596 cinfo->ds->nbuckets = size;
597 for (i = 0; i < size; i++) {
598 INIT_LIST_HEAD(&buckets[i].written);
599 INIT_LIST_HEAD(&buckets[i].committing);
600 /* mark direct verifier as unset */
601 buckets[i].direct_verf.committed =
602 NFS_INVALID_STABLE_HOW;
603 }
604 }
605 spin_unlock(cinfo->lock);
606 return 0;
607 }
608}
609
610static struct nfs4_pnfs_ds *
611ff_layout_choose_best_ds_for_read(struct nfs_pageio_descriptor *pgio,
612 int *best_idx)
613{
614 struct nfs4_ff_layout_segment *fls;
615 struct nfs4_pnfs_ds *ds;
616 int idx;
617
618 fls = FF_LAYOUT_LSEG(pgio->pg_lseg);
619 /* mirrors are sorted by efficiency */
620 for (idx = 0; idx < fls->mirror_array_cnt; idx++) {
621 ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, idx, false);
622 if (ds) {
623 *best_idx = idx;
624 return ds;
625 }
626 }
627
628 return NULL;
629}
630
631static void
632ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
633 struct nfs_page *req)
634{
635 struct nfs_pgio_mirror *pgm;
636 struct nfs4_ff_layout_mirror *mirror;
637 struct nfs4_pnfs_ds *ds;
638 int ds_idx;
639
640 /* Use full layout for now */
641 if (!pgio->pg_lseg)
642 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
643 req->wb_context,
644 0,
645 NFS4_MAX_UINT64,
646 IOMODE_READ,
647 GFP_KERNEL);
648 /* If no lseg, fall back to read through mds */
649 if (pgio->pg_lseg == NULL)
650 goto out_mds;
651
652 ds = ff_layout_choose_best_ds_for_read(pgio, &ds_idx);
653 if (!ds)
654 goto out_mds;
655 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx);
656
657 pgio->pg_mirror_idx = ds_idx;
658
659 /* read always uses only one mirror - idx 0 for pgio layer */
660 pgm = &pgio->pg_mirrors[0];
661 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize;
662
663 return;
664out_mds:
665 pnfs_put_lseg(pgio->pg_lseg);
666 pgio->pg_lseg = NULL;
667 nfs_pageio_reset_read_mds(pgio);
668}
669
670static void
671ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
672 struct nfs_page *req)
673{
674 struct nfs4_ff_layout_mirror *mirror;
675 struct nfs_pgio_mirror *pgm;
676 struct nfs_commit_info cinfo;
677 struct nfs4_pnfs_ds *ds;
678 int i;
679 int status;
680
681 if (!pgio->pg_lseg)
682 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
683 req->wb_context,
684 0,
685 NFS4_MAX_UINT64,
686 IOMODE_RW,
687 GFP_NOFS);
688 /* If no lseg, fall back to write through mds */
689 if (pgio->pg_lseg == NULL)
690 goto out_mds;
691
692 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
693 status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
694 if (status < 0)
695 goto out_mds;
696
697 /* Use a direct mapping of ds_idx to pgio mirror_idx */
698 if (WARN_ON_ONCE(pgio->pg_mirror_count !=
699 FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg)))
700 goto out_mds;
701
702 for (i = 0; i < pgio->pg_mirror_count; i++) {
703 ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true);
704 if (!ds)
705 goto out_mds;
706 pgm = &pgio->pg_mirrors[i];
707 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i);
708 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize;
709 }
710
711 return;
712
713out_mds:
714 pnfs_put_lseg(pgio->pg_lseg);
715 pgio->pg_lseg = NULL;
716 nfs_pageio_reset_write_mds(pgio);
717}
718
719static unsigned int
720ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
721 struct nfs_page *req)
722{
723 if (!pgio->pg_lseg)
724 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
725 req->wb_context,
726 0,
727 NFS4_MAX_UINT64,
728 IOMODE_RW,
729 GFP_NOFS);
730 if (pgio->pg_lseg)
731 return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
732
733 /* no lseg means that pnfs is not in use, so no mirroring here */
734 pnfs_put_lseg(pgio->pg_lseg);
735 pgio->pg_lseg = NULL;
736 nfs_pageio_reset_write_mds(pgio);
737 return 1;
738}
739
740static const struct nfs_pageio_ops ff_layout_pg_read_ops = {
741 .pg_init = ff_layout_pg_init_read,
742 .pg_test = pnfs_generic_pg_test,
743 .pg_doio = pnfs_generic_pg_readpages,
744 .pg_cleanup = pnfs_generic_pg_cleanup,
745};
746
747static const struct nfs_pageio_ops ff_layout_pg_write_ops = {
748 .pg_init = ff_layout_pg_init_write,
749 .pg_test = pnfs_generic_pg_test,
750 .pg_doio = pnfs_generic_pg_writepages,
751 .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write,
752 .pg_cleanup = pnfs_generic_pg_cleanup,
753};
754
755static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs)
756{
757 struct rpc_task *task = &hdr->task;
758
759 pnfs_layoutcommit_inode(hdr->inode, false);
760
761 if (retry_pnfs) {
762 dprintk("%s Reset task %5u for i/o through pNFS "
763 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
764 hdr->task.tk_pid,
765 hdr->inode->i_sb->s_id,
766 (unsigned long long)NFS_FILEID(hdr->inode),
767 hdr->args.count,
768 (unsigned long long)hdr->args.offset);
769
770 if (!hdr->dreq) {
771 struct nfs_open_context *ctx;
772
773 ctx = nfs_list_entry(hdr->pages.next)->wb_context;
774 set_bit(NFS_CONTEXT_RESEND_WRITES, &ctx->flags);
775 hdr->completion_ops->error_cleanup(&hdr->pages);
776 } else {
777 nfs_direct_set_resched_writes(hdr->dreq);
778 /* fake unstable write to let common nfs resend pages */
779 hdr->verf.committed = NFS_UNSTABLE;
780 hdr->good_bytes = 0;
781 }
782 return;
783 }
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_write_done_resend_to_mds(hdr);
795 }
796}
797
798static void ff_layout_reset_read(struct nfs_pgio_header *hdr)
799{
800 struct rpc_task *task = &hdr->task;
801
802 pnfs_layoutcommit_inode(hdr->inode, false);
803
804 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
805 dprintk("%s Reset task %5u for i/o through MDS "
806 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
807 hdr->task.tk_pid,
808 hdr->inode->i_sb->s_id,
809 (unsigned long long)NFS_FILEID(hdr->inode),
810 hdr->args.count,
811 (unsigned long long)hdr->args.offset);
812
813 task->tk_status = pnfs_read_done_resend_to_mds(hdr);
814 }
815}
816
817static int ff_layout_async_handle_error_v4(struct rpc_task *task,
818 struct nfs4_state *state,
819 struct nfs_client *clp,
820 struct pnfs_layout_segment *lseg,
821 int idx)
822{
823 struct pnfs_layout_hdr *lo = lseg->pls_layout;
824 struct inode *inode = lo->plh_inode;
825 struct nfs_server *mds_server = NFS_SERVER(inode);
826
827 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
828 struct nfs_client *mds_client = mds_server->nfs_client;
829 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
830
831 if (task->tk_status >= 0)
832 return 0;
833
834 switch (task->tk_status) {
835 /* MDS state errors */
836 case -NFS4ERR_DELEG_REVOKED:
837 case -NFS4ERR_ADMIN_REVOKED:
838 case -NFS4ERR_BAD_STATEID:
839 if (state == NULL)
840 break;
841 nfs_remove_bad_delegation(state->inode);
842 case -NFS4ERR_OPENMODE:
843 if (state == NULL)
844 break;
845 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
846 goto out_bad_stateid;
847 goto wait_on_recovery;
848 case -NFS4ERR_EXPIRED:
849 if (state != NULL) {
850 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
851 goto out_bad_stateid;
852 }
853 nfs4_schedule_lease_recovery(mds_client);
854 goto wait_on_recovery;
855 /* DS session errors */
856 case -NFS4ERR_BADSESSION:
857 case -NFS4ERR_BADSLOT:
858 case -NFS4ERR_BAD_HIGH_SLOT:
859 case -NFS4ERR_DEADSESSION:
860 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
861 case -NFS4ERR_SEQ_FALSE_RETRY:
862 case -NFS4ERR_SEQ_MISORDERED:
863 dprintk("%s ERROR %d, Reset session. Exchangeid "
864 "flags 0x%x\n", __func__, task->tk_status,
865 clp->cl_exchange_flags);
866 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
867 break;
868 case -NFS4ERR_DELAY:
869 case -NFS4ERR_GRACE:
870 rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX);
871 break;
872 case -NFS4ERR_RETRY_UNCACHED_REP:
873 break;
874 /* Invalidate Layout errors */
875 case -NFS4ERR_PNFS_NO_LAYOUT:
876 case -ESTALE: /* mapped NFS4ERR_STALE */
877 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
878 case -EISDIR: /* mapped NFS4ERR_ISDIR */
879 case -NFS4ERR_FHEXPIRED:
880 case -NFS4ERR_WRONG_TYPE:
881 dprintk("%s Invalid layout error %d\n", __func__,
882 task->tk_status);
883 /*
884 * Destroy layout so new i/o will get a new layout.
885 * Layout will not be destroyed until all current lseg
886 * references are put. Mark layout as invalid to resend failed
887 * i/o and all i/o waiting on the slot table to the MDS until
888 * layout is destroyed and a new valid layout is obtained.
889 */
890 pnfs_destroy_layout(NFS_I(inode));
891 rpc_wake_up(&tbl->slot_tbl_waitq);
892 goto reset;
893 /* RPC connection errors */
894 case -ECONNREFUSED:
895 case -EHOSTDOWN:
896 case -EHOSTUNREACH:
897 case -ENETUNREACH:
898 case -EIO:
899 case -ETIMEDOUT:
900 case -EPIPE:
901 dprintk("%s DS connection error %d\n", __func__,
902 task->tk_status);
903 nfs4_mark_deviceid_unavailable(devid);
904 rpc_wake_up(&tbl->slot_tbl_waitq);
905 /* fall through */
906 default:
907 if (ff_layout_has_available_ds(lseg))
908 return -NFS4ERR_RESET_TO_PNFS;
909reset:
910 dprintk("%s Retry through MDS. Error %d\n", __func__,
911 task->tk_status);
912 return -NFS4ERR_RESET_TO_MDS;
913 }
914out:
915 task->tk_status = 0;
916 return -EAGAIN;
917out_bad_stateid:
918 task->tk_status = -EIO;
919 return 0;
920wait_on_recovery:
921 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
922 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
923 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
924 goto out;
925}
926
927/* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
928static int ff_layout_async_handle_error_v3(struct rpc_task *task,
929 struct pnfs_layout_segment *lseg,
930 int idx)
931{
932 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
933
934 if (task->tk_status >= 0)
935 return 0;
936
937 if (task->tk_status != -EJUKEBOX) {
938 dprintk("%s DS connection error %d\n", __func__,
939 task->tk_status);
940 nfs4_mark_deviceid_unavailable(devid);
941 if (ff_layout_has_available_ds(lseg))
942 return -NFS4ERR_RESET_TO_PNFS;
943 else
944 return -NFS4ERR_RESET_TO_MDS;
945 }
946
947 if (task->tk_status == -EJUKEBOX)
948 nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY);
949 task->tk_status = 0;
950 rpc_restart_call(task);
951 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
952 return -EAGAIN;
953}
954
955static int ff_layout_async_handle_error(struct rpc_task *task,
956 struct nfs4_state *state,
957 struct nfs_client *clp,
958 struct pnfs_layout_segment *lseg,
959 int idx)
960{
961 int vers = clp->cl_nfs_mod->rpc_vers->number;
962
963 switch (vers) {
964 case 3:
965 return ff_layout_async_handle_error_v3(task, lseg, idx);
966 case 4:
967 return ff_layout_async_handle_error_v4(task, state, clp,
968 lseg, idx);
969 default:
970 /* should never happen */
971 WARN_ON_ONCE(1);
972 return 0;
973 }
974}
975
976static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
977 int idx, u64 offset, u64 length,
978 u32 status, int opnum)
979{
980 struct nfs4_ff_layout_mirror *mirror;
981 int err;
982
983 mirror = FF_LAYOUT_COMP(lseg, idx);
984 err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
985 mirror, offset, length, status, opnum,
986 GFP_NOIO);
987 dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status);
988}
989
990/* NFS_PROTO call done callback routines */
991
992static int ff_layout_read_done_cb(struct rpc_task *task,
993 struct nfs_pgio_header *hdr)
994{
995 struct inode *inode;
996 int err;
997
998 trace_nfs4_pnfs_read(hdr, task->tk_status);
999 if (task->tk_status == -ETIMEDOUT && !hdr->res.op_status)
1000 hdr->res.op_status = NFS4ERR_NXIO;
1001 if (task->tk_status < 0 && hdr->res.op_status)
1002 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1003 hdr->args.offset, hdr->args.count,
1004 hdr->res.op_status, OP_READ);
1005 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1006 hdr->ds_clp, hdr->lseg,
1007 hdr->pgio_mirror_idx);
1008
1009 switch (err) {
1010 case -NFS4ERR_RESET_TO_PNFS:
1011 set_bit(NFS_LAYOUT_RETURN_BEFORE_CLOSE,
1012 &hdr->lseg->pls_layout->plh_flags);
1013 pnfs_read_resend_pnfs(hdr);
1014 return task->tk_status;
1015 case -NFS4ERR_RESET_TO_MDS:
1016 inode = hdr->lseg->pls_layout->plh_inode;
1017 pnfs_error_mark_layout_for_return(inode, hdr->lseg);
1018 ff_layout_reset_read(hdr);
1019 return task->tk_status;
1020 case -EAGAIN:
1021 rpc_restart_call_prepare(task);
1022 return -EAGAIN;
1023 }
1024
1025 return 0;
1026}
1027
1028/*
1029 * We reference the rpc_cred of the first WRITE that triggers the need for
1030 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
1031 * rfc5661 is not clear about which credential should be used.
1032 *
1033 * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
1034 * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
1035 * we always send layoutcommit after DS writes.
1036 */
1037static void
1038ff_layout_set_layoutcommit(struct nfs_pgio_header *hdr)
1039{
Trond Myklebust67af7612015-03-25 20:40:38 -04001040 pnfs_set_layoutcommit(hdr->inode, hdr->lseg,
1041 hdr->mds_offset + hdr->res.count);
Tom Haynesd67ae822014-12-11 17:02:04 -05001042 dprintk("%s inode %lu pls_end_pos %lu\n", __func__, hdr->inode->i_ino,
1043 (unsigned long) NFS_I(hdr->inode)->layout->plh_lwb);
1044}
1045
1046static bool
1047ff_layout_reset_to_mds(struct pnfs_layout_segment *lseg, int idx)
1048{
1049 /* No mirroring for now */
1050 struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx);
1051
1052 return ff_layout_test_devid_unavailable(node);
1053}
1054
1055static int ff_layout_read_prepare_common(struct rpc_task *task,
1056 struct nfs_pgio_header *hdr)
1057{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001058 nfs4_ff_layout_stat_io_start_read(
1059 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1060 hdr->args.count);
1061
Tom Haynesd67ae822014-12-11 17:02:04 -05001062 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1063 rpc_exit(task, -EIO);
1064 return -EIO;
1065 }
1066 if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) {
1067 dprintk("%s task %u reset io to MDS\n", __func__, task->tk_pid);
1068 if (ff_layout_has_available_ds(hdr->lseg))
1069 pnfs_read_resend_pnfs(hdr);
1070 else
1071 ff_layout_reset_read(hdr);
1072 rpc_exit(task, 0);
1073 return -EAGAIN;
1074 }
1075 hdr->pgio_done_cb = ff_layout_read_done_cb;
1076
1077 return 0;
1078}
1079
1080/*
1081 * Call ops for the async read/write cases
1082 * In the case of dense layouts, the offset needs to be reset to its
1083 * original value.
1084 */
1085static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data)
1086{
1087 struct nfs_pgio_header *hdr = data;
1088
1089 if (ff_layout_read_prepare_common(task, hdr))
1090 return;
1091
1092 rpc_call_start(task);
1093}
1094
1095static int ff_layout_setup_sequence(struct nfs_client *ds_clp,
1096 struct nfs4_sequence_args *args,
1097 struct nfs4_sequence_res *res,
1098 struct rpc_task *task)
1099{
1100 if (ds_clp->cl_session)
1101 return nfs41_setup_sequence(ds_clp->cl_session,
1102 args,
1103 res,
1104 task);
1105 return nfs40_setup_sequence(ds_clp->cl_slot_tbl,
1106 args,
1107 res,
1108 task);
1109}
1110
1111static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data)
1112{
1113 struct nfs_pgio_header *hdr = data;
1114
Tom Haynesd67ae822014-12-11 17:02:04 -05001115 if (ff_layout_setup_sequence(hdr->ds_clp,
1116 &hdr->args.seq_args,
1117 &hdr->res.seq_res,
1118 task))
1119 return;
1120
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001121 if (ff_layout_read_prepare_common(task, hdr))
1122 return;
1123
Tom Haynesd67ae822014-12-11 17:02:04 -05001124 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1125 hdr->args.lock_context, FMODE_READ) == -EIO)
1126 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1127}
1128
1129static void ff_layout_read_call_done(struct rpc_task *task, void *data)
1130{
1131 struct nfs_pgio_header *hdr = data;
1132
1133 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
1134
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001135 nfs4_ff_layout_stat_io_end_read(task,
1136 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1137 hdr->args.count, hdr->res.count);
1138
Tom Haynesd67ae822014-12-11 17:02:04 -05001139 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1140 task->tk_status == 0) {
1141 nfs4_sequence_done(task, &hdr->res.seq_res);
1142 return;
1143 }
1144
1145 /* Note this may cause RPC to be resent */
1146 hdr->mds_ops->rpc_call_done(task, hdr);
1147}
1148
1149static void ff_layout_read_count_stats(struct rpc_task *task, void *data)
1150{
1151 struct nfs_pgio_header *hdr = data;
1152
1153 rpc_count_iostats_metrics(task,
1154 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]);
1155}
1156
1157static int ff_layout_write_done_cb(struct rpc_task *task,
1158 struct nfs_pgio_header *hdr)
1159{
1160 struct inode *inode;
1161 int err;
1162
1163 trace_nfs4_pnfs_write(hdr, task->tk_status);
1164 if (task->tk_status == -ETIMEDOUT && !hdr->res.op_status)
1165 hdr->res.op_status = NFS4ERR_NXIO;
1166 if (task->tk_status < 0 && hdr->res.op_status)
1167 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1168 hdr->args.offset, hdr->args.count,
1169 hdr->res.op_status, OP_WRITE);
1170 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1171 hdr->ds_clp, hdr->lseg,
1172 hdr->pgio_mirror_idx);
1173
1174 switch (err) {
1175 case -NFS4ERR_RESET_TO_PNFS:
1176 case -NFS4ERR_RESET_TO_MDS:
1177 inode = hdr->lseg->pls_layout->plh_inode;
1178 pnfs_error_mark_layout_for_return(inode, hdr->lseg);
1179 if (err == -NFS4ERR_RESET_TO_PNFS) {
1180 pnfs_set_retry_layoutget(hdr->lseg->pls_layout);
1181 ff_layout_reset_write(hdr, true);
1182 } else {
1183 pnfs_clear_retry_layoutget(hdr->lseg->pls_layout);
1184 ff_layout_reset_write(hdr, false);
1185 }
1186 return task->tk_status;
1187 case -EAGAIN:
1188 rpc_restart_call_prepare(task);
1189 return -EAGAIN;
1190 }
1191
1192 if (hdr->res.verf->committed == NFS_FILE_SYNC ||
1193 hdr->res.verf->committed == NFS_DATA_SYNC)
1194 ff_layout_set_layoutcommit(hdr);
1195
1196 return 0;
1197}
1198
1199static int ff_layout_commit_done_cb(struct rpc_task *task,
1200 struct nfs_commit_data *data)
1201{
1202 struct inode *inode;
1203 int err;
1204
1205 trace_nfs4_pnfs_commit_ds(data, task->tk_status);
1206 if (task->tk_status == -ETIMEDOUT && !data->res.op_status)
1207 data->res.op_status = NFS4ERR_NXIO;
1208 if (task->tk_status < 0 && data->res.op_status)
1209 ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index,
1210 data->args.offset, data->args.count,
1211 data->res.op_status, OP_COMMIT);
1212 err = ff_layout_async_handle_error(task, NULL, data->ds_clp,
1213 data->lseg, data->ds_commit_index);
1214
1215 switch (err) {
1216 case -NFS4ERR_RESET_TO_PNFS:
1217 case -NFS4ERR_RESET_TO_MDS:
1218 inode = data->lseg->pls_layout->plh_inode;
1219 pnfs_error_mark_layout_for_return(inode, data->lseg);
1220 if (err == -NFS4ERR_RESET_TO_PNFS)
1221 pnfs_set_retry_layoutget(data->lseg->pls_layout);
1222 else
1223 pnfs_clear_retry_layoutget(data->lseg->pls_layout);
1224 pnfs_generic_prepare_to_resend_writes(data);
1225 return -EAGAIN;
1226 case -EAGAIN:
1227 rpc_restart_call_prepare(task);
1228 return -EAGAIN;
1229 }
1230
1231 if (data->verf.committed == NFS_UNSTABLE)
Trond Myklebust67af7612015-03-25 20:40:38 -04001232 pnfs_set_layoutcommit(data->inode, data->lseg, data->lwb);
Tom Haynesd67ae822014-12-11 17:02:04 -05001233
1234 return 0;
1235}
1236
1237static int ff_layout_write_prepare_common(struct rpc_task *task,
1238 struct nfs_pgio_header *hdr)
1239{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001240 nfs4_ff_layout_stat_io_start_write(
1241 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1242 hdr->args.count);
1243
Tom Haynesd67ae822014-12-11 17:02:04 -05001244 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1245 rpc_exit(task, -EIO);
1246 return -EIO;
1247 }
1248
1249 if (ff_layout_reset_to_mds(hdr->lseg, hdr->pgio_mirror_idx)) {
1250 bool retry_pnfs;
1251
1252 retry_pnfs = ff_layout_has_available_ds(hdr->lseg);
1253 dprintk("%s task %u reset io to %s\n", __func__,
1254 task->tk_pid, retry_pnfs ? "pNFS" : "MDS");
1255 ff_layout_reset_write(hdr, retry_pnfs);
1256 rpc_exit(task, 0);
1257 return -EAGAIN;
1258 }
1259
1260 return 0;
1261}
1262
1263static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data)
1264{
1265 struct nfs_pgio_header *hdr = data;
1266
1267 if (ff_layout_write_prepare_common(task, hdr))
1268 return;
1269
1270 rpc_call_start(task);
1271}
1272
1273static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data)
1274{
1275 struct nfs_pgio_header *hdr = data;
1276
Tom Haynesd67ae822014-12-11 17:02:04 -05001277 if (ff_layout_setup_sequence(hdr->ds_clp,
1278 &hdr->args.seq_args,
1279 &hdr->res.seq_res,
1280 task))
1281 return;
1282
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001283 if (ff_layout_write_prepare_common(task, hdr))
1284 return;
1285
Tom Haynesd67ae822014-12-11 17:02:04 -05001286 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1287 hdr->args.lock_context, FMODE_WRITE) == -EIO)
1288 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1289}
1290
1291static void ff_layout_write_call_done(struct rpc_task *task, void *data)
1292{
1293 struct nfs_pgio_header *hdr = data;
1294
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001295 nfs4_ff_layout_stat_io_end_write(task,
1296 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1297 hdr->args.count, hdr->res.count,
1298 hdr->res.verf->committed);
1299
Tom Haynesd67ae822014-12-11 17:02:04 -05001300 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1301 task->tk_status == 0) {
1302 nfs4_sequence_done(task, &hdr->res.seq_res);
1303 return;
1304 }
1305
1306 /* Note this may cause RPC to be resent */
1307 hdr->mds_ops->rpc_call_done(task, hdr);
1308}
1309
1310static void ff_layout_write_count_stats(struct rpc_task *task, void *data)
1311{
1312 struct nfs_pgio_header *hdr = data;
1313
1314 rpc_count_iostats_metrics(task,
1315 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]);
1316}
1317
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001318static void ff_layout_commit_prepare_common(struct rpc_task *task,
1319 struct nfs_commit_data *cdata)
1320{
1321 nfs4_ff_layout_stat_io_start_write(
1322 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1323 0);
1324}
1325
Tom Haynesd67ae822014-12-11 17:02:04 -05001326static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data)
1327{
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001328 ff_layout_commit_prepare_common(task, data);
Tom Haynesd67ae822014-12-11 17:02:04 -05001329 rpc_call_start(task);
1330}
1331
1332static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data)
1333{
1334 struct nfs_commit_data *wdata = data;
1335
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001336 if (ff_layout_setup_sequence(wdata->ds_clp,
Tom Haynesd67ae822014-12-11 17:02:04 -05001337 &wdata->args.seq_args,
1338 &wdata->res.seq_res,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001339 task))
1340 return;
1341 ff_layout_commit_prepare_common(task, data);
1342}
1343
1344static void ff_layout_commit_done(struct rpc_task *task, void *data)
1345{
1346 struct nfs_commit_data *cdata = data;
1347 struct nfs_page *req;
1348 __u64 count = 0;
1349
1350 if (task->tk_status == 0) {
1351 list_for_each_entry(req, &cdata->pages, wb_list)
1352 count += req->wb_bytes;
1353 }
1354
1355 nfs4_ff_layout_stat_io_end_write(task,
1356 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1357 count, count, NFS_FILE_SYNC);
1358
1359 pnfs_generic_write_commit_done(task, data);
Tom Haynesd67ae822014-12-11 17:02:04 -05001360}
1361
1362static void ff_layout_commit_count_stats(struct rpc_task *task, void *data)
1363{
1364 struct nfs_commit_data *cdata = data;
1365
1366 rpc_count_iostats_metrics(task,
1367 &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]);
1368}
1369
1370static const struct rpc_call_ops ff_layout_read_call_ops_v3 = {
1371 .rpc_call_prepare = ff_layout_read_prepare_v3,
1372 .rpc_call_done = ff_layout_read_call_done,
1373 .rpc_count_stats = ff_layout_read_count_stats,
1374 .rpc_release = pnfs_generic_rw_release,
1375};
1376
1377static const struct rpc_call_ops ff_layout_read_call_ops_v4 = {
1378 .rpc_call_prepare = ff_layout_read_prepare_v4,
1379 .rpc_call_done = ff_layout_read_call_done,
1380 .rpc_count_stats = ff_layout_read_count_stats,
1381 .rpc_release = pnfs_generic_rw_release,
1382};
1383
1384static const struct rpc_call_ops ff_layout_write_call_ops_v3 = {
1385 .rpc_call_prepare = ff_layout_write_prepare_v3,
1386 .rpc_call_done = ff_layout_write_call_done,
1387 .rpc_count_stats = ff_layout_write_count_stats,
1388 .rpc_release = pnfs_generic_rw_release,
1389};
1390
1391static const struct rpc_call_ops ff_layout_write_call_ops_v4 = {
1392 .rpc_call_prepare = ff_layout_write_prepare_v4,
1393 .rpc_call_done = ff_layout_write_call_done,
1394 .rpc_count_stats = ff_layout_write_count_stats,
1395 .rpc_release = pnfs_generic_rw_release,
1396};
1397
1398static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = {
1399 .rpc_call_prepare = ff_layout_commit_prepare_v3,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001400 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001401 .rpc_count_stats = ff_layout_commit_count_stats,
1402 .rpc_release = pnfs_generic_commit_release,
1403};
1404
1405static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = {
1406 .rpc_call_prepare = ff_layout_commit_prepare_v4,
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001407 .rpc_call_done = ff_layout_commit_done,
Tom Haynesd67ae822014-12-11 17:02:04 -05001408 .rpc_count_stats = ff_layout_commit_count_stats,
1409 .rpc_release = pnfs_generic_commit_release,
1410};
1411
1412static enum pnfs_try_status
1413ff_layout_read_pagelist(struct nfs_pgio_header *hdr)
1414{
1415 struct pnfs_layout_segment *lseg = hdr->lseg;
1416 struct nfs4_pnfs_ds *ds;
1417 struct rpc_clnt *ds_clnt;
1418 struct rpc_cred *ds_cred;
1419 loff_t offset = hdr->args.offset;
1420 u32 idx = hdr->pgio_mirror_idx;
1421 int vers;
1422 struct nfs_fh *fh;
1423
1424 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
1425 __func__, hdr->inode->i_ino,
1426 hdr->args.pgbase, (size_t)hdr->args.count, offset);
1427
1428 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
1429 if (!ds)
1430 goto out_failed;
1431
1432 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1433 hdr->inode);
1434 if (IS_ERR(ds_clnt))
1435 goto out_failed;
1436
1437 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
1438 if (IS_ERR(ds_cred))
1439 goto out_failed;
1440
1441 vers = nfs4_ff_layout_ds_version(lseg, idx);
1442
1443 dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__,
1444 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers);
1445
1446 atomic_inc(&ds->ds_clp->cl_count);
1447 hdr->ds_clp = ds->ds_clp;
1448 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1449 if (fh)
1450 hdr->args.fh = fh;
Tom Haynesd67ae822014-12-11 17:02:04 -05001451 /*
1452 * Note that if we ever decide to split across DSes,
1453 * then we may need to handle dense-like offsets.
1454 */
1455 hdr->args.offset = offset;
1456 hdr->mds_offset = offset;
1457
1458 /* Perform an asynchronous read to ds */
1459 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1460 vers == 3 ? &ff_layout_read_call_ops_v3 :
1461 &ff_layout_read_call_ops_v4,
1462 0, RPC_TASK_SOFTCONN);
1463
1464 return PNFS_ATTEMPTED;
1465
1466out_failed:
1467 if (ff_layout_has_available_ds(lseg))
1468 return PNFS_TRY_AGAIN;
1469 return PNFS_NOT_ATTEMPTED;
1470}
1471
1472/* Perform async writes. */
1473static enum pnfs_try_status
1474ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
1475{
1476 struct pnfs_layout_segment *lseg = hdr->lseg;
1477 struct nfs4_pnfs_ds *ds;
1478 struct rpc_clnt *ds_clnt;
1479 struct rpc_cred *ds_cred;
1480 loff_t offset = hdr->args.offset;
1481 int vers;
1482 struct nfs_fh *fh;
1483 int idx = hdr->pgio_mirror_idx;
1484
1485 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1486 if (!ds)
1487 return PNFS_NOT_ATTEMPTED;
1488
1489 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1490 hdr->inode);
1491 if (IS_ERR(ds_clnt))
1492 return PNFS_NOT_ATTEMPTED;
1493
1494 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
1495 if (IS_ERR(ds_cred))
1496 return PNFS_NOT_ATTEMPTED;
1497
1498 vers = nfs4_ff_layout_ds_version(lseg, idx);
1499
1500 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n",
1501 __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
1502 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count),
1503 vers);
1504
1505 hdr->pgio_done_cb = ff_layout_write_done_cb;
1506 atomic_inc(&ds->ds_clp->cl_count);
1507 hdr->ds_clp = ds->ds_clp;
1508 hdr->ds_commit_idx = idx;
1509 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1510 if (fh)
1511 hdr->args.fh = fh;
1512
1513 /*
1514 * Note that if we ever decide to split across DSes,
1515 * then we may need to handle dense-like offsets.
1516 */
1517 hdr->args.offset = offset;
1518
1519 /* Perform an asynchronous write */
1520 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1521 vers == 3 ? &ff_layout_write_call_ops_v3 :
1522 &ff_layout_write_call_ops_v4,
1523 sync, RPC_TASK_SOFTCONN);
1524 return PNFS_ATTEMPTED;
1525}
1526
Tom Haynesd67ae822014-12-11 17:02:04 -05001527static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1528{
1529 return i;
1530}
1531
1532static struct nfs_fh *
1533select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1534{
1535 struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg);
1536
1537 /* FIXME: Assume that there is only one NFS version available
1538 * for the DS.
1539 */
1540 return &flseg->mirror_array[i]->fh_versions[0];
1541}
1542
1543static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how)
1544{
1545 struct pnfs_layout_segment *lseg = data->lseg;
1546 struct nfs4_pnfs_ds *ds;
1547 struct rpc_clnt *ds_clnt;
1548 struct rpc_cred *ds_cred;
1549 u32 idx;
1550 int vers;
1551 struct nfs_fh *fh;
1552
1553 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1554 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1555 if (!ds)
1556 goto out_err;
1557
1558 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1559 data->inode);
1560 if (IS_ERR(ds_clnt))
1561 goto out_err;
1562
1563 ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred);
1564 if (IS_ERR(ds_cred))
1565 goto out_err;
1566
1567 vers = nfs4_ff_layout_ds_version(lseg, idx);
1568
1569 dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__,
1570 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count),
1571 vers);
1572 data->commit_done_cb = ff_layout_commit_done_cb;
1573 data->cred = ds_cred;
1574 atomic_inc(&ds->ds_clp->cl_count);
1575 data->ds_clp = ds->ds_clp;
1576 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1577 if (fh)
1578 data->args.fh = fh;
Trond Myklebustabcb7bf2015-06-23 19:51:59 +08001579
Tom Haynesd67ae822014-12-11 17:02:04 -05001580 return nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops,
1581 vers == 3 ? &ff_layout_commit_call_ops_v3 :
1582 &ff_layout_commit_call_ops_v4,
1583 how, RPC_TASK_SOFTCONN);
1584out_err:
1585 pnfs_generic_prepare_to_resend_writes(data);
1586 pnfs_generic_commit_release(data);
1587 return -EAGAIN;
1588}
1589
1590static int
1591ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1592 int how, struct nfs_commit_info *cinfo)
1593{
1594 return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
1595 ff_layout_initiate_commit);
1596}
1597
1598static struct pnfs_ds_commit_info *
1599ff_layout_get_ds_info(struct inode *inode)
1600{
1601 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1602
1603 if (layout == NULL)
1604 return NULL;
1605
1606 return &FF_LAYOUT_FROM_HDR(layout)->commit_info;
1607}
1608
1609static void
Trond Myklebustfc877012015-03-09 17:25:14 -04001610ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d)
Tom Haynesd67ae822014-12-11 17:02:04 -05001611{
1612 nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds,
1613 id_node));
1614}
1615
1616static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo,
1617 struct xdr_stream *xdr,
1618 const struct nfs4_layoutreturn_args *args)
1619{
1620 struct pnfs_layout_hdr *hdr = &flo->generic_hdr;
1621 __be32 *start;
1622 int count = 0, ret = 0;
1623
1624 start = xdr_reserve_space(xdr, 4);
1625 if (unlikely(!start))
1626 return -E2BIG;
1627
1628 /* This assume we always return _ALL_ layouts */
1629 spin_lock(&hdr->plh_inode->i_lock);
1630 ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range);
1631 spin_unlock(&hdr->plh_inode->i_lock);
1632
1633 *start = cpu_to_be32(count);
1634
1635 return ret;
1636}
1637
1638/* report nothing for now */
1639static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo,
1640 struct xdr_stream *xdr,
1641 const struct nfs4_layoutreturn_args *args)
1642{
1643 __be32 *p;
1644
1645 p = xdr_reserve_space(xdr, 4);
1646 if (likely(p))
1647 *p = cpu_to_be32(0);
1648}
1649
1650static struct nfs4_deviceid_node *
1651ff_layout_alloc_deviceid_node(struct nfs_server *server,
1652 struct pnfs_device *pdev, gfp_t gfp_flags)
1653{
1654 struct nfs4_ff_layout_ds *dsaddr;
1655
1656 dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags);
1657 if (!dsaddr)
1658 return NULL;
1659 return &dsaddr->id_node;
1660}
1661
1662static void
1663ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo,
1664 struct xdr_stream *xdr,
1665 const struct nfs4_layoutreturn_args *args)
1666{
1667 struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
1668 __be32 *start;
1669
1670 dprintk("%s: Begin\n", __func__);
1671 start = xdr_reserve_space(xdr, 4);
1672 BUG_ON(!start);
1673
1674 if (ff_layout_encode_ioerr(flo, xdr, args))
1675 goto out;
1676
1677 ff_layout_encode_iostats(flo, xdr, args);
1678out:
1679 *start = cpu_to_be32((xdr->p - start - 1) * 4);
1680 dprintk("%s: Return\n", __func__);
1681}
1682
Peng Tao27c43062015-06-23 19:52:02 +08001683static int
1684ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen)
1685{
1686 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
1687
1688 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
1689}
1690
1691static size_t
1692ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf,
1693 const int buflen)
1694{
1695 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
1696 const struct in6_addr *addr = &sin6->sin6_addr;
1697
1698 /*
1699 * RFC 4291, Section 2.2.2
1700 *
1701 * Shorthanded ANY address
1702 */
1703 if (ipv6_addr_any(addr))
1704 return snprintf(buf, buflen, "::");
1705
1706 /*
1707 * RFC 4291, Section 2.2.2
1708 *
1709 * Shorthanded loopback address
1710 */
1711 if (ipv6_addr_loopback(addr))
1712 return snprintf(buf, buflen, "::1");
1713
1714 /*
1715 * RFC 4291, Section 2.2.3
1716 *
1717 * Special presentation address format for mapped v4
1718 * addresses.
1719 */
1720 if (ipv6_addr_v4mapped(addr))
1721 return snprintf(buf, buflen, "::ffff:%pI4",
1722 &addr->s6_addr32[3]);
1723
1724 /*
1725 * RFC 4291, Section 2.2.1
1726 */
1727 return snprintf(buf, buflen, "%pI6c", addr);
1728}
1729
1730/* Derived from rpc_sockaddr2uaddr */
1731static void
1732ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da)
1733{
1734 struct sockaddr *sap = (struct sockaddr *)&da->da_addr;
1735 char portbuf[RPCBIND_MAXUADDRPLEN];
1736 char addrbuf[RPCBIND_MAXUADDRLEN];
1737 char *netid;
1738 unsigned short port;
1739 int len, netid_len;
1740 __be32 *p;
1741
1742 switch (sap->sa_family) {
1743 case AF_INET:
1744 if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
1745 return;
1746 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
1747 netid = "tcp";
1748 netid_len = 3;
1749 break;
1750 case AF_INET6:
1751 if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
1752 return;
1753 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
1754 netid = "tcp6";
1755 netid_len = 4;
1756 break;
1757 default:
1758 /* we only support tcp and tcp6 */
1759 WARN_ON_ONCE(1);
1760 return;
1761 }
1762
1763 snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff);
1764 len = strlcat(addrbuf, portbuf, sizeof(addrbuf));
1765
1766 p = xdr_reserve_space(xdr, 4 + netid_len);
1767 xdr_encode_opaque(p, netid, netid_len);
1768
1769 p = xdr_reserve_space(xdr, 4 + len);
1770 xdr_encode_opaque(p, addrbuf, len);
1771}
1772
1773static void
1774ff_layout_encode_nfstime(struct xdr_stream *xdr,
1775 ktime_t t)
1776{
1777 struct timespec64 ts;
1778 __be32 *p;
1779
1780 p = xdr_reserve_space(xdr, 12);
1781 ts = ktime_to_timespec64(t);
1782 p = xdr_encode_hyper(p, ts.tv_sec);
1783 *p++ = cpu_to_be32(ts.tv_nsec);
1784}
1785
1786static void
1787ff_layout_encode_io_latency(struct xdr_stream *xdr,
1788 struct nfs4_ff_io_stat *stat)
1789{
1790 __be32 *p;
1791
1792 p = xdr_reserve_space(xdr, 5 * 8);
1793 p = xdr_encode_hyper(p, stat->ops_requested);
1794 p = xdr_encode_hyper(p, stat->bytes_requested);
1795 p = xdr_encode_hyper(p, stat->ops_completed);
1796 p = xdr_encode_hyper(p, stat->bytes_completed);
1797 p = xdr_encode_hyper(p, stat->bytes_not_delivered);
1798 ff_layout_encode_nfstime(xdr, stat->total_busy_time);
1799 ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time);
1800}
1801
1802static void
1803ff_layout_encode_layoutstats(struct xdr_stream *xdr,
1804 struct nfs42_layoutstat_args *args,
1805 struct nfs42_layoutstat_devinfo *devinfo)
1806{
1807 struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private;
1808 struct nfs4_pnfs_ds_addr *da;
1809 struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds;
1810 struct nfs_fh *fh = &mirror->fh_versions[0];
1811 __be32 *p, *start;
1812
1813 da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node);
1814 dprintk("%s: DS %s: encoding address %s\n",
1815 __func__, ds->ds_remotestr, da->da_remotestr);
1816 /* layoutupdate length */
1817 start = xdr_reserve_space(xdr, 4);
1818 /* netaddr4 */
1819 ff_layout_encode_netaddr(xdr, da);
1820 /* nfs_fh4 */
1821 p = xdr_reserve_space(xdr, 4 + fh->size);
1822 xdr_encode_opaque(p, fh->data, fh->size);
1823 /* ff_io_latency4 read */
1824 spin_lock(&mirror->lock);
1825 ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat);
1826 /* ff_io_latency4 write */
1827 ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat);
1828 spin_unlock(&mirror->lock);
1829 /* nfstime4 */
1830 ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time));
1831 /* bool */
1832 p = xdr_reserve_space(xdr, 4);
1833 *p = cpu_to_be32(false);
1834
1835 *start = cpu_to_be32((xdr->p - start - 1) * 4);
1836}
1837
Peng Taoad4dc53e2015-06-23 19:52:01 +08001838static bool
1839ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args,
1840 struct pnfs_layout_segment *pls,
1841 int *dev_count, int dev_limit)
1842{
1843 struct nfs4_ff_layout_mirror *mirror;
1844 struct nfs4_deviceid_node *dev;
1845 struct nfs42_layoutstat_devinfo *devinfo;
1846 int i;
1847
1848 for (i = 0; i <= FF_LAYOUT_MIRROR_COUNT(pls); i++) {
1849 if (*dev_count >= dev_limit)
1850 break;
1851 mirror = FF_LAYOUT_COMP(pls, i);
Peng Tao27c43062015-06-23 19:52:02 +08001852 if (!mirror || !mirror->mirror_ds)
1853 continue;
Peng Taoad4dc53e2015-06-23 19:52:01 +08001854 dev = FF_LAYOUT_DEVID_NODE(pls, i);
1855 devinfo = &args->devinfo[*dev_count];
1856 memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);
1857 devinfo->offset = pls->pls_range.offset;
1858 devinfo->length = pls->pls_range.length;
1859 /* well, we don't really know if IO is continuous or not! */
1860 devinfo->read_count = mirror->read_stat.io_stat.bytes_completed;
1861 devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed;
1862 devinfo->write_count = mirror->write_stat.io_stat.bytes_completed;
1863 devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed;
1864 devinfo->layout_type = LAYOUT_FLEX_FILES;
Peng Tao27c43062015-06-23 19:52:02 +08001865 devinfo->layoutstats_encode = ff_layout_encode_layoutstats;
1866 devinfo->layout_private = mirror;
1867 /* lseg refcount put in cleanup_layoutstats */
1868 pnfs_get_lseg(pls);
Peng Taoad4dc53e2015-06-23 19:52:01 +08001869
1870 ++(*dev_count);
1871 }
1872
1873 return *dev_count < dev_limit;
1874}
1875
1876static int
1877ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args)
1878{
1879 struct pnfs_layout_segment *pls;
1880 int dev_count = 0;
1881
1882 spin_lock(&args->inode->i_lock);
1883 list_for_each_entry(pls, &NFS_I(args->inode)->layout->plh_segs, pls_list) {
1884 dev_count += FF_LAYOUT_MIRROR_COUNT(pls);
1885 }
1886 spin_unlock(&args->inode->i_lock);
1887 /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
1888 if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) {
1889 dprintk("%s: truncating devinfo to limit (%d:%d)\n",
1890 __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV);
1891 dev_count = PNFS_LAYOUTSTATS_MAXDEV;
1892 }
1893 args->devinfo = kmalloc(dev_count * sizeof(*args->devinfo), GFP_KERNEL);
1894 if (!args->devinfo)
1895 return -ENOMEM;
1896
1897 dev_count = 0;
1898 spin_lock(&args->inode->i_lock);
1899 list_for_each_entry(pls, &NFS_I(args->inode)->layout->plh_segs, pls_list) {
1900 if (!ff_layout_mirror_prepare_stats(args, pls, &dev_count,
1901 PNFS_LAYOUTSTATS_MAXDEV)) {
1902 break;
1903 }
1904 }
1905 spin_unlock(&args->inode->i_lock);
1906 args->num_dev = dev_count;
1907
1908 return 0;
1909}
1910
Peng Tao27c43062015-06-23 19:52:02 +08001911static void
1912ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data)
1913{
1914 struct nfs4_ff_layout_mirror *mirror;
1915 int i;
1916
1917 for (i = 0; i < data->args.num_dev; i++) {
1918 mirror = data->args.devinfo[i].layout_private;
1919 data->args.devinfo[i].layout_private = NULL;
1920 pnfs_put_lseg(mirror->lseg);
1921 }
1922}
1923
Tom Haynesd67ae822014-12-11 17:02:04 -05001924static struct pnfs_layoutdriver_type flexfilelayout_type = {
1925 .id = LAYOUT_FLEX_FILES,
1926 .name = "LAYOUT_FLEX_FILES",
1927 .owner = THIS_MODULE,
1928 .alloc_layout_hdr = ff_layout_alloc_layout_hdr,
1929 .free_layout_hdr = ff_layout_free_layout_hdr,
1930 .alloc_lseg = ff_layout_alloc_lseg,
1931 .free_lseg = ff_layout_free_lseg,
1932 .pg_read_ops = &ff_layout_pg_read_ops,
1933 .pg_write_ops = &ff_layout_pg_write_ops,
1934 .get_ds_info = ff_layout_get_ds_info,
Trond Myklebustfc877012015-03-09 17:25:14 -04001935 .free_deviceid_node = ff_layout_free_deviceid_node,
Tom Haynes338d00c2015-02-17 14:58:15 -08001936 .mark_request_commit = pnfs_layout_mark_request_commit,
Tom Haynesd67ae822014-12-11 17:02:04 -05001937 .clear_request_commit = pnfs_generic_clear_request_commit,
1938 .scan_commit_lists = pnfs_generic_scan_commit_lists,
1939 .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
1940 .commit_pagelist = ff_layout_commit_pagelist,
1941 .read_pagelist = ff_layout_read_pagelist,
1942 .write_pagelist = ff_layout_write_pagelist,
1943 .alloc_deviceid_node = ff_layout_alloc_deviceid_node,
1944 .encode_layoutreturn = ff_layout_encode_layoutreturn,
Trond Myklebust5bb89b42015-03-25 14:14:42 -04001945 .sync = pnfs_nfs_generic_sync,
Peng Taoad4dc53e2015-06-23 19:52:01 +08001946 .prepare_layoutstats = ff_layout_prepare_layoutstats,
Peng Tao27c43062015-06-23 19:52:02 +08001947 .cleanup_layoutstats = ff_layout_cleanup_layoutstats,
Tom Haynesd67ae822014-12-11 17:02:04 -05001948};
1949
1950static int __init nfs4flexfilelayout_init(void)
1951{
1952 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n",
1953 __func__);
1954 return pnfs_register_layoutdriver(&flexfilelayout_type);
1955}
1956
1957static void __exit nfs4flexfilelayout_exit(void)
1958{
1959 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
1960 __func__);
1961 pnfs_unregister_layoutdriver(&flexfilelayout_type);
1962}
1963
1964MODULE_ALIAS("nfs-layouttype4-4");
1965
1966MODULE_LICENSE("GPL");
1967MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
1968
1969module_init(nfs4flexfilelayout_init);
1970module_exit(nfs4flexfilelayout_exit);