Linux 4.8.3
[linux/fpc-iii.git] / fs / nfs / flexfilelayout / flexfilelayout.c
blob51b51369704c56c00732b10ff0c31edfd0d9c19e
1 /*
2 * Module for pnfs flexfile layout driver.
4 * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
6 * Tao Peng <bergwolf@primarydata.com>
7 */
9 #include <linux/nfs_fs.h>
10 #include <linux/nfs_page.h>
11 #include <linux/module.h>
13 #include <linux/sunrpc/metrics.h>
15 #include "flexfilelayout.h"
16 #include "../nfs4session.h"
17 #include "../nfs4idmap.h"
18 #include "../internal.h"
19 #include "../delegation.h"
20 #include "../nfs4trace.h"
21 #include "../iostat.h"
22 #include "../nfs.h"
23 #include "../nfs42.h"
25 #define NFSDBG_FACILITY NFSDBG_PNFS_LD
27 #define FF_LAYOUT_POLL_RETRY_MAX (15*HZ)
29 static struct group_info *ff_zero_group;
31 static struct pnfs_layout_hdr *
32 ff_layout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
34 struct nfs4_flexfile_layout *ffl;
36 ffl = kzalloc(sizeof(*ffl), gfp_flags);
37 if (ffl) {
38 INIT_LIST_HEAD(&ffl->error_list);
39 INIT_LIST_HEAD(&ffl->mirrors);
40 ffl->last_report_time = ktime_get();
41 return &ffl->generic_hdr;
42 } else
43 return NULL;
46 static void
47 ff_layout_free_layout_hdr(struct pnfs_layout_hdr *lo)
49 struct nfs4_ff_layout_ds_err *err, *n;
51 list_for_each_entry_safe(err, n, &FF_LAYOUT_FROM_HDR(lo)->error_list,
52 list) {
53 list_del(&err->list);
54 kfree(err);
56 kfree(FF_LAYOUT_FROM_HDR(lo));
59 static int decode_pnfs_stateid(struct xdr_stream *xdr, nfs4_stateid *stateid)
61 __be32 *p;
63 p = xdr_inline_decode(xdr, NFS4_STATEID_SIZE);
64 if (unlikely(p == NULL))
65 return -ENOBUFS;
66 stateid->type = NFS4_PNFS_DS_STATEID_TYPE;
67 memcpy(stateid->data, p, NFS4_STATEID_SIZE);
68 dprintk("%s: stateid id= [%x%x%x%x]\n", __func__,
69 p[0], p[1], p[2], p[3]);
70 return 0;
73 static int decode_deviceid(struct xdr_stream *xdr, struct nfs4_deviceid *devid)
75 __be32 *p;
77 p = xdr_inline_decode(xdr, NFS4_DEVICEID4_SIZE);
78 if (unlikely(!p))
79 return -ENOBUFS;
80 memcpy(devid, p, NFS4_DEVICEID4_SIZE);
81 nfs4_print_deviceid(devid);
82 return 0;
85 static int decode_nfs_fh(struct xdr_stream *xdr, struct nfs_fh *fh)
87 __be32 *p;
89 p = xdr_inline_decode(xdr, 4);
90 if (unlikely(!p))
91 return -ENOBUFS;
92 fh->size = be32_to_cpup(p++);
93 if (fh->size > sizeof(struct nfs_fh)) {
94 printk(KERN_ERR "NFS flexfiles: Too big fh received %d\n",
95 fh->size);
96 return -EOVERFLOW;
98 /* fh.data */
99 p = xdr_inline_decode(xdr, fh->size);
100 if (unlikely(!p))
101 return -ENOBUFS;
102 memcpy(&fh->data, p, fh->size);
103 dprintk("%s: fh len %d\n", __func__, fh->size);
105 return 0;
109 * Currently only stringified uids and gids are accepted.
110 * I.e., kerberos is not supported to the DSes, so no pricipals.
112 * That means that one common function will suffice, but when
113 * principals are added, this should be split to accomodate
114 * calls to both nfs_map_name_to_uid() and nfs_map_group_to_gid().
116 static int
117 decode_name(struct xdr_stream *xdr, u32 *id)
119 __be32 *p;
120 int len;
122 /* opaque_length(4)*/
123 p = xdr_inline_decode(xdr, 4);
124 if (unlikely(!p))
125 return -ENOBUFS;
126 len = be32_to_cpup(p++);
127 if (len < 0)
128 return -EINVAL;
130 dprintk("%s: len %u\n", __func__, len);
132 /* opaque body */
133 p = xdr_inline_decode(xdr, len);
134 if (unlikely(!p))
135 return -ENOBUFS;
137 if (!nfs_map_string_to_numeric((char *)p, len, id))
138 return -EINVAL;
140 return 0;
143 static bool ff_mirror_match_fh(const struct nfs4_ff_layout_mirror *m1,
144 const struct nfs4_ff_layout_mirror *m2)
146 int i, j;
148 if (m1->fh_versions_cnt != m2->fh_versions_cnt)
149 return false;
150 for (i = 0; i < m1->fh_versions_cnt; i++) {
151 bool found_fh = false;
152 for (j = 0; j < m2->fh_versions_cnt; j++) {
153 if (nfs_compare_fh(&m1->fh_versions[i],
154 &m2->fh_versions[j]) == 0) {
155 found_fh = true;
156 break;
159 if (!found_fh)
160 return false;
162 return true;
165 static struct nfs4_ff_layout_mirror *
166 ff_layout_add_mirror(struct pnfs_layout_hdr *lo,
167 struct nfs4_ff_layout_mirror *mirror)
169 struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
170 struct nfs4_ff_layout_mirror *pos;
171 struct inode *inode = lo->plh_inode;
173 spin_lock(&inode->i_lock);
174 list_for_each_entry(pos, &ff_layout->mirrors, mirrors) {
175 if (mirror->mirror_ds != pos->mirror_ds)
176 continue;
177 if (!ff_mirror_match_fh(mirror, pos))
178 continue;
179 if (atomic_inc_not_zero(&pos->ref)) {
180 spin_unlock(&inode->i_lock);
181 return pos;
184 list_add(&mirror->mirrors, &ff_layout->mirrors);
185 mirror->layout = lo;
186 spin_unlock(&inode->i_lock);
187 return mirror;
190 static void
191 ff_layout_remove_mirror(struct nfs4_ff_layout_mirror *mirror)
193 struct inode *inode;
194 if (mirror->layout == NULL)
195 return;
196 inode = mirror->layout->plh_inode;
197 spin_lock(&inode->i_lock);
198 list_del(&mirror->mirrors);
199 spin_unlock(&inode->i_lock);
200 mirror->layout = NULL;
203 static struct nfs4_ff_layout_mirror *ff_layout_alloc_mirror(gfp_t gfp_flags)
205 struct nfs4_ff_layout_mirror *mirror;
207 mirror = kzalloc(sizeof(*mirror), gfp_flags);
208 if (mirror != NULL) {
209 spin_lock_init(&mirror->lock);
210 atomic_set(&mirror->ref, 1);
211 INIT_LIST_HEAD(&mirror->mirrors);
213 return mirror;
216 static void ff_layout_free_mirror(struct nfs4_ff_layout_mirror *mirror)
218 struct rpc_cred *cred;
220 ff_layout_remove_mirror(mirror);
221 kfree(mirror->fh_versions);
222 cred = rcu_access_pointer(mirror->ro_cred);
223 if (cred)
224 put_rpccred(cred);
225 cred = rcu_access_pointer(mirror->rw_cred);
226 if (cred)
227 put_rpccred(cred);
228 nfs4_ff_layout_put_deviceid(mirror->mirror_ds);
229 kfree(mirror);
232 static void ff_layout_put_mirror(struct nfs4_ff_layout_mirror *mirror)
234 if (mirror != NULL && atomic_dec_and_test(&mirror->ref))
235 ff_layout_free_mirror(mirror);
238 static void ff_layout_free_mirror_array(struct nfs4_ff_layout_segment *fls)
240 int i;
242 if (fls->mirror_array) {
243 for (i = 0; i < fls->mirror_array_cnt; i++) {
244 /* normally mirror_ds is freed in
245 * .free_deviceid_node but we still do it here
246 * for .alloc_lseg error path */
247 ff_layout_put_mirror(fls->mirror_array[i]);
249 kfree(fls->mirror_array);
250 fls->mirror_array = NULL;
254 static int ff_layout_check_layout(struct nfs4_layoutget_res *lgr)
256 int ret = 0;
258 dprintk("--> %s\n", __func__);
260 /* FIXME: remove this check when layout segment support is added */
261 if (lgr->range.offset != 0 ||
262 lgr->range.length != NFS4_MAX_UINT64) {
263 dprintk("%s Only whole file layouts supported. Use MDS i/o\n",
264 __func__);
265 ret = -EINVAL;
268 dprintk("--> %s returns %d\n", __func__, ret);
269 return ret;
272 static void _ff_layout_free_lseg(struct nfs4_ff_layout_segment *fls)
274 if (fls) {
275 ff_layout_free_mirror_array(fls);
276 kfree(fls);
280 static bool
281 ff_lseg_range_is_after(const struct pnfs_layout_range *l1,
282 const struct pnfs_layout_range *l2)
284 u64 end1, end2;
286 if (l1->iomode != l2->iomode)
287 return l1->iomode != IOMODE_READ;
288 end1 = pnfs_calc_offset_end(l1->offset, l1->length);
289 end2 = pnfs_calc_offset_end(l2->offset, l2->length);
290 if (end1 < l2->offset)
291 return false;
292 if (end2 < l1->offset)
293 return true;
294 return l2->offset <= l1->offset;
297 static bool
298 ff_lseg_merge(struct pnfs_layout_segment *new,
299 struct pnfs_layout_segment *old)
301 u64 new_end, old_end;
303 if (test_bit(NFS_LSEG_LAYOUTRETURN, &old->pls_flags))
304 return false;
305 if (new->pls_range.iomode != old->pls_range.iomode)
306 return false;
307 old_end = pnfs_calc_offset_end(old->pls_range.offset,
308 old->pls_range.length);
309 if (old_end < new->pls_range.offset)
310 return false;
311 new_end = pnfs_calc_offset_end(new->pls_range.offset,
312 new->pls_range.length);
313 if (new_end < old->pls_range.offset)
314 return false;
316 /* Mergeable: copy info from 'old' to 'new' */
317 if (new_end < old_end)
318 new_end = old_end;
319 if (new->pls_range.offset < old->pls_range.offset)
320 new->pls_range.offset = old->pls_range.offset;
321 new->pls_range.length = pnfs_calc_offset_length(new->pls_range.offset,
322 new_end);
323 if (test_bit(NFS_LSEG_ROC, &old->pls_flags))
324 set_bit(NFS_LSEG_ROC, &new->pls_flags);
325 return true;
328 static void
329 ff_layout_add_lseg(struct pnfs_layout_hdr *lo,
330 struct pnfs_layout_segment *lseg,
331 struct list_head *free_me)
333 pnfs_generic_layout_insert_lseg(lo, lseg,
334 ff_lseg_range_is_after,
335 ff_lseg_merge,
336 free_me);
339 static void ff_layout_sort_mirrors(struct nfs4_ff_layout_segment *fls)
341 int i, j;
343 for (i = 0; i < fls->mirror_array_cnt - 1; i++) {
344 for (j = i + 1; j < fls->mirror_array_cnt; j++)
345 if (fls->mirror_array[i]->efficiency <
346 fls->mirror_array[j]->efficiency)
347 swap(fls->mirror_array[i],
348 fls->mirror_array[j]);
352 static void ff_layout_mark_devices_valid(struct nfs4_ff_layout_segment *fls)
354 struct nfs4_deviceid_node *node;
355 int i;
357 if (!(fls->flags & FF_FLAGS_NO_IO_THRU_MDS))
358 return;
359 for (i = 0; i < fls->mirror_array_cnt; i++) {
360 node = &fls->mirror_array[i]->mirror_ds->id_node;
361 clear_bit(NFS_DEVICEID_UNAVAILABLE, &node->flags);
365 static struct pnfs_layout_segment *
366 ff_layout_alloc_lseg(struct pnfs_layout_hdr *lh,
367 struct nfs4_layoutget_res *lgr,
368 gfp_t gfp_flags)
370 struct pnfs_layout_segment *ret;
371 struct nfs4_ff_layout_segment *fls = NULL;
372 struct xdr_stream stream;
373 struct xdr_buf buf;
374 struct page *scratch;
375 u64 stripe_unit;
376 u32 mirror_array_cnt;
377 __be32 *p;
378 int i, rc;
380 dprintk("--> %s\n", __func__);
381 scratch = alloc_page(gfp_flags);
382 if (!scratch)
383 return ERR_PTR(-ENOMEM);
385 xdr_init_decode_pages(&stream, &buf, lgr->layoutp->pages,
386 lgr->layoutp->len);
387 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
389 /* stripe unit and mirror_array_cnt */
390 rc = -EIO;
391 p = xdr_inline_decode(&stream, 8 + 4);
392 if (!p)
393 goto out_err_free;
395 p = xdr_decode_hyper(p, &stripe_unit);
396 mirror_array_cnt = be32_to_cpup(p++);
397 dprintk("%s: stripe_unit=%llu mirror_array_cnt=%u\n", __func__,
398 stripe_unit, mirror_array_cnt);
400 if (mirror_array_cnt > NFS4_FLEXFILE_LAYOUT_MAX_MIRROR_CNT ||
401 mirror_array_cnt == 0)
402 goto out_err_free;
404 rc = -ENOMEM;
405 fls = kzalloc(sizeof(*fls), gfp_flags);
406 if (!fls)
407 goto out_err_free;
409 fls->mirror_array_cnt = mirror_array_cnt;
410 fls->stripe_unit = stripe_unit;
411 fls->mirror_array = kcalloc(fls->mirror_array_cnt,
412 sizeof(fls->mirror_array[0]), gfp_flags);
413 if (fls->mirror_array == NULL)
414 goto out_err_free;
416 for (i = 0; i < fls->mirror_array_cnt; i++) {
417 struct nfs4_ff_layout_mirror *mirror;
418 struct nfs4_deviceid devid;
419 struct nfs4_deviceid_node *idnode;
420 struct auth_cred acred = { .group_info = ff_zero_group };
421 struct rpc_cred __rcu *cred;
422 u32 ds_count, fh_count, id;
423 int j;
425 rc = -EIO;
426 p = xdr_inline_decode(&stream, 4);
427 if (!p)
428 goto out_err_free;
429 ds_count = be32_to_cpup(p);
431 /* FIXME: allow for striping? */
432 if (ds_count != 1)
433 goto out_err_free;
435 fls->mirror_array[i] = ff_layout_alloc_mirror(gfp_flags);
436 if (fls->mirror_array[i] == NULL) {
437 rc = -ENOMEM;
438 goto out_err_free;
441 fls->mirror_array[i]->ds_count = ds_count;
443 /* deviceid */
444 rc = decode_deviceid(&stream, &devid);
445 if (rc)
446 goto out_err_free;
448 idnode = nfs4_find_get_deviceid(NFS_SERVER(lh->plh_inode),
449 &devid, lh->plh_lc_cred,
450 gfp_flags);
452 * upon success, mirror_ds is allocated by previous
453 * getdeviceinfo, or newly by .alloc_deviceid_node
454 * nfs4_find_get_deviceid failure is indeed getdeviceinfo falure
456 if (idnode)
457 fls->mirror_array[i]->mirror_ds =
458 FF_LAYOUT_MIRROR_DS(idnode);
459 else
460 goto out_err_free;
462 /* efficiency */
463 rc = -EIO;
464 p = xdr_inline_decode(&stream, 4);
465 if (!p)
466 goto out_err_free;
467 fls->mirror_array[i]->efficiency = be32_to_cpup(p);
469 /* stateid */
470 rc = decode_pnfs_stateid(&stream, &fls->mirror_array[i]->stateid);
471 if (rc)
472 goto out_err_free;
474 /* fh */
475 p = xdr_inline_decode(&stream, 4);
476 if (!p)
477 goto out_err_free;
478 fh_count = be32_to_cpup(p);
480 fls->mirror_array[i]->fh_versions =
481 kzalloc(fh_count * sizeof(struct nfs_fh),
482 gfp_flags);
483 if (fls->mirror_array[i]->fh_versions == NULL) {
484 rc = -ENOMEM;
485 goto out_err_free;
488 for (j = 0; j < fh_count; j++) {
489 rc = decode_nfs_fh(&stream,
490 &fls->mirror_array[i]->fh_versions[j]);
491 if (rc)
492 goto out_err_free;
495 fls->mirror_array[i]->fh_versions_cnt = fh_count;
497 /* user */
498 rc = decode_name(&stream, &id);
499 if (rc)
500 goto out_err_free;
502 acred.uid = make_kuid(&init_user_ns, id);
504 /* group */
505 rc = decode_name(&stream, &id);
506 if (rc)
507 goto out_err_free;
509 acred.gid = make_kgid(&init_user_ns, id);
511 /* find the cred for it */
512 rcu_assign_pointer(cred, rpc_lookup_generic_cred(&acred, 0, gfp_flags));
513 if (IS_ERR(cred)) {
514 rc = PTR_ERR(cred);
515 goto out_err_free;
518 if (lgr->range.iomode == IOMODE_READ)
519 rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred);
520 else
521 rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred);
523 mirror = ff_layout_add_mirror(lh, fls->mirror_array[i]);
524 if (mirror != fls->mirror_array[i]) {
525 /* swap cred ptrs so free_mirror will clean up old */
526 if (lgr->range.iomode == IOMODE_READ) {
527 cred = xchg(&mirror->ro_cred, cred);
528 rcu_assign_pointer(fls->mirror_array[i]->ro_cred, cred);
529 } else {
530 cred = xchg(&mirror->rw_cred, cred);
531 rcu_assign_pointer(fls->mirror_array[i]->rw_cred, cred);
533 ff_layout_free_mirror(fls->mirror_array[i]);
534 fls->mirror_array[i] = mirror;
537 dprintk("%s: iomode %s uid %u gid %u\n", __func__,
538 lgr->range.iomode == IOMODE_READ ? "READ" : "RW",
539 from_kuid(&init_user_ns, acred.uid),
540 from_kgid(&init_user_ns, acred.gid));
543 p = xdr_inline_decode(&stream, 4);
544 if (!p)
545 goto out_sort_mirrors;
546 fls->flags = be32_to_cpup(p);
548 p = xdr_inline_decode(&stream, 4);
549 if (!p)
550 goto out_sort_mirrors;
551 for (i=0; i < fls->mirror_array_cnt; i++)
552 fls->mirror_array[i]->report_interval = be32_to_cpup(p);
554 out_sort_mirrors:
555 ff_layout_sort_mirrors(fls);
556 rc = ff_layout_check_layout(lgr);
557 if (rc)
558 goto out_err_free;
559 ff_layout_mark_devices_valid(fls);
561 ret = &fls->generic_hdr;
562 dprintk("<-- %s (success)\n", __func__);
563 out_free_page:
564 __free_page(scratch);
565 return ret;
566 out_err_free:
567 _ff_layout_free_lseg(fls);
568 ret = ERR_PTR(rc);
569 dprintk("<-- %s (%d)\n", __func__, rc);
570 goto out_free_page;
573 static bool ff_layout_has_rw_segments(struct pnfs_layout_hdr *layout)
575 struct pnfs_layout_segment *lseg;
577 list_for_each_entry(lseg, &layout->plh_segs, pls_list)
578 if (lseg->pls_range.iomode == IOMODE_RW)
579 return true;
581 return false;
584 static void
585 ff_layout_free_lseg(struct pnfs_layout_segment *lseg)
587 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
589 dprintk("--> %s\n", __func__);
591 if (lseg->pls_range.iomode == IOMODE_RW) {
592 struct nfs4_flexfile_layout *ffl;
593 struct inode *inode;
595 ffl = FF_LAYOUT_FROM_HDR(lseg->pls_layout);
596 inode = ffl->generic_hdr.plh_inode;
597 spin_lock(&inode->i_lock);
598 if (!ff_layout_has_rw_segments(lseg->pls_layout)) {
599 ffl->commit_info.nbuckets = 0;
600 kfree(ffl->commit_info.buckets);
601 ffl->commit_info.buckets = NULL;
603 spin_unlock(&inode->i_lock);
605 _ff_layout_free_lseg(fls);
608 /* Return 1 until we have multiple lsegs support */
609 static int
610 ff_layout_get_lseg_count(struct nfs4_ff_layout_segment *fls)
612 return 1;
615 static void
616 nfs4_ff_start_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
618 /* first IO request? */
619 if (atomic_inc_return(&timer->n_ops) == 1) {
620 timer->start_time = now;
624 static ktime_t
625 nfs4_ff_end_busy_timer(struct nfs4_ff_busy_timer *timer, ktime_t now)
627 ktime_t start;
629 if (atomic_dec_return(&timer->n_ops) < 0)
630 WARN_ON_ONCE(1);
632 start = timer->start_time;
633 timer->start_time = now;
634 return ktime_sub(now, start);
637 static bool
638 nfs4_ff_layoutstat_start_io(struct nfs4_ff_layout_mirror *mirror,
639 struct nfs4_ff_layoutstat *layoutstat,
640 ktime_t now)
642 static const ktime_t notime = {0};
643 s64 report_interval = FF_LAYOUTSTATS_REPORT_INTERVAL;
644 struct nfs4_flexfile_layout *ffl = FF_LAYOUT_FROM_HDR(mirror->layout);
646 nfs4_ff_start_busy_timer(&layoutstat->busy_timer, now);
647 if (ktime_equal(mirror->start_time, notime))
648 mirror->start_time = now;
649 if (mirror->report_interval != 0)
650 report_interval = (s64)mirror->report_interval * 1000LL;
651 else if (layoutstats_timer != 0)
652 report_interval = (s64)layoutstats_timer * 1000LL;
653 if (ktime_to_ms(ktime_sub(now, ffl->last_report_time)) >=
654 report_interval) {
655 ffl->last_report_time = now;
656 return true;
659 return false;
662 static void
663 nfs4_ff_layout_stat_io_update_requested(struct nfs4_ff_layoutstat *layoutstat,
664 __u64 requested)
666 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
668 iostat->ops_requested++;
669 iostat->bytes_requested += requested;
672 static void
673 nfs4_ff_layout_stat_io_update_completed(struct nfs4_ff_layoutstat *layoutstat,
674 __u64 requested,
675 __u64 completed,
676 ktime_t time_completed,
677 ktime_t time_started)
679 struct nfs4_ff_io_stat *iostat = &layoutstat->io_stat;
680 ktime_t completion_time = ktime_sub(time_completed, time_started);
681 ktime_t timer;
683 iostat->ops_completed++;
684 iostat->bytes_completed += completed;
685 iostat->bytes_not_delivered += requested - completed;
687 timer = nfs4_ff_end_busy_timer(&layoutstat->busy_timer, time_completed);
688 iostat->total_busy_time =
689 ktime_add(iostat->total_busy_time, timer);
690 iostat->aggregate_completion_time =
691 ktime_add(iostat->aggregate_completion_time,
692 completion_time);
695 static void
696 nfs4_ff_layout_stat_io_start_read(struct inode *inode,
697 struct nfs4_ff_layout_mirror *mirror,
698 __u64 requested, ktime_t now)
700 bool report;
702 spin_lock(&mirror->lock);
703 report = nfs4_ff_layoutstat_start_io(mirror, &mirror->read_stat, now);
704 nfs4_ff_layout_stat_io_update_requested(&mirror->read_stat, requested);
705 spin_unlock(&mirror->lock);
707 if (report)
708 pnfs_report_layoutstat(inode, GFP_KERNEL);
711 static void
712 nfs4_ff_layout_stat_io_end_read(struct rpc_task *task,
713 struct nfs4_ff_layout_mirror *mirror,
714 __u64 requested,
715 __u64 completed)
717 spin_lock(&mirror->lock);
718 nfs4_ff_layout_stat_io_update_completed(&mirror->read_stat,
719 requested, completed,
720 ktime_get(), task->tk_start);
721 spin_unlock(&mirror->lock);
724 static void
725 nfs4_ff_layout_stat_io_start_write(struct inode *inode,
726 struct nfs4_ff_layout_mirror *mirror,
727 __u64 requested, ktime_t now)
729 bool report;
731 spin_lock(&mirror->lock);
732 report = nfs4_ff_layoutstat_start_io(mirror , &mirror->write_stat, now);
733 nfs4_ff_layout_stat_io_update_requested(&mirror->write_stat, requested);
734 spin_unlock(&mirror->lock);
736 if (report)
737 pnfs_report_layoutstat(inode, GFP_NOIO);
740 static void
741 nfs4_ff_layout_stat_io_end_write(struct rpc_task *task,
742 struct nfs4_ff_layout_mirror *mirror,
743 __u64 requested,
744 __u64 completed,
745 enum nfs3_stable_how committed)
747 if (committed == NFS_UNSTABLE)
748 requested = completed = 0;
750 spin_lock(&mirror->lock);
751 nfs4_ff_layout_stat_io_update_completed(&mirror->write_stat,
752 requested, completed, ktime_get(), task->tk_start);
753 spin_unlock(&mirror->lock);
756 static int
757 ff_layout_alloc_commit_info(struct pnfs_layout_segment *lseg,
758 struct nfs_commit_info *cinfo,
759 gfp_t gfp_flags)
761 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
762 struct pnfs_commit_bucket *buckets;
763 int size;
765 if (cinfo->ds->nbuckets != 0) {
766 /* This assumes there is only one RW lseg per file.
767 * To support multiple lseg per file, we need to
768 * change struct pnfs_commit_bucket to allow dynamic
769 * increasing nbuckets.
771 return 0;
774 size = ff_layout_get_lseg_count(fls) * FF_LAYOUT_MIRROR_COUNT(lseg);
776 buckets = kcalloc(size, sizeof(struct pnfs_commit_bucket),
777 gfp_flags);
778 if (!buckets)
779 return -ENOMEM;
780 else {
781 int i;
783 spin_lock(&cinfo->inode->i_lock);
784 if (cinfo->ds->nbuckets != 0)
785 kfree(buckets);
786 else {
787 cinfo->ds->buckets = buckets;
788 cinfo->ds->nbuckets = size;
789 for (i = 0; i < size; i++) {
790 INIT_LIST_HEAD(&buckets[i].written);
791 INIT_LIST_HEAD(&buckets[i].committing);
792 /* mark direct verifier as unset */
793 buckets[i].direct_verf.committed =
794 NFS_INVALID_STABLE_HOW;
797 spin_unlock(&cinfo->inode->i_lock);
798 return 0;
802 static struct nfs4_pnfs_ds *
803 ff_layout_choose_best_ds_for_read(struct pnfs_layout_segment *lseg,
804 int start_idx,
805 int *best_idx)
807 struct nfs4_ff_layout_segment *fls = FF_LAYOUT_LSEG(lseg);
808 struct nfs4_pnfs_ds *ds;
809 bool fail_return = false;
810 int idx;
812 /* mirrors are sorted by efficiency */
813 for (idx = start_idx; idx < fls->mirror_array_cnt; idx++) {
814 if (idx+1 == fls->mirror_array_cnt)
815 fail_return = true;
816 ds = nfs4_ff_layout_prepare_ds(lseg, idx, fail_return);
817 if (ds) {
818 *best_idx = idx;
819 return ds;
823 return NULL;
826 static void
827 ff_layout_pg_get_read(struct nfs_pageio_descriptor *pgio,
828 struct nfs_page *req,
829 bool strict_iomode)
831 retry_strict:
832 pnfs_put_lseg(pgio->pg_lseg);
833 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
834 req->wb_context,
836 NFS4_MAX_UINT64,
837 IOMODE_READ,
838 strict_iomode,
839 GFP_KERNEL);
840 if (IS_ERR(pgio->pg_lseg)) {
841 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
842 pgio->pg_lseg = NULL;
845 /* If we don't have checking, do get a IOMODE_RW
846 * segment, and the server wants to avoid READs
847 * there, then retry!
849 if (pgio->pg_lseg && !strict_iomode &&
850 ff_layout_avoid_read_on_rw(pgio->pg_lseg)) {
851 strict_iomode = true;
852 goto retry_strict;
856 static void
857 ff_layout_pg_init_read(struct nfs_pageio_descriptor *pgio,
858 struct nfs_page *req)
860 struct nfs_pgio_mirror *pgm;
861 struct nfs4_ff_layout_mirror *mirror;
862 struct nfs4_pnfs_ds *ds;
863 int ds_idx;
865 retry:
866 /* Use full layout for now */
867 if (!pgio->pg_lseg)
868 ff_layout_pg_get_read(pgio, req, false);
869 else if (ff_layout_avoid_read_on_rw(pgio->pg_lseg))
870 ff_layout_pg_get_read(pgio, req, true);
872 /* If no lseg, fall back to read through mds */
873 if (pgio->pg_lseg == NULL)
874 goto out_mds;
876 ds = ff_layout_choose_best_ds_for_read(pgio->pg_lseg, 0, &ds_idx);
877 if (!ds) {
878 if (!ff_layout_no_fallback_to_mds(pgio->pg_lseg))
879 goto out_mds;
880 pnfs_put_lseg(pgio->pg_lseg);
881 pgio->pg_lseg = NULL;
882 /* Sleep for 1 second before retrying */
883 ssleep(1);
884 goto retry;
887 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, ds_idx);
889 pgio->pg_mirror_idx = ds_idx;
891 /* read always uses only one mirror - idx 0 for pgio layer */
892 pgm = &pgio->pg_mirrors[0];
893 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].rsize;
895 return;
896 out_mds:
897 pnfs_put_lseg(pgio->pg_lseg);
898 pgio->pg_lseg = NULL;
899 nfs_pageio_reset_read_mds(pgio);
902 static void
903 ff_layout_pg_init_write(struct nfs_pageio_descriptor *pgio,
904 struct nfs_page *req)
906 struct nfs4_ff_layout_mirror *mirror;
907 struct nfs_pgio_mirror *pgm;
908 struct nfs_commit_info cinfo;
909 struct nfs4_pnfs_ds *ds;
910 int i;
911 int status;
913 retry:
914 if (!pgio->pg_lseg) {
915 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
916 req->wb_context,
918 NFS4_MAX_UINT64,
919 IOMODE_RW,
920 false,
921 GFP_NOFS);
922 if (IS_ERR(pgio->pg_lseg)) {
923 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
924 pgio->pg_lseg = NULL;
925 return;
928 /* If no lseg, fall back to write through mds */
929 if (pgio->pg_lseg == NULL)
930 goto out_mds;
932 nfs_init_cinfo(&cinfo, pgio->pg_inode, pgio->pg_dreq);
933 status = ff_layout_alloc_commit_info(pgio->pg_lseg, &cinfo, GFP_NOFS);
934 if (status < 0)
935 goto out_mds;
937 /* Use a direct mapping of ds_idx to pgio mirror_idx */
938 if (WARN_ON_ONCE(pgio->pg_mirror_count !=
939 FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg)))
940 goto out_mds;
942 for (i = 0; i < pgio->pg_mirror_count; i++) {
943 ds = nfs4_ff_layout_prepare_ds(pgio->pg_lseg, i, true);
944 if (!ds) {
945 if (!ff_layout_no_fallback_to_mds(pgio->pg_lseg))
946 goto out_mds;
947 pnfs_put_lseg(pgio->pg_lseg);
948 pgio->pg_lseg = NULL;
949 /* Sleep for 1 second before retrying */
950 ssleep(1);
951 goto retry;
953 pgm = &pgio->pg_mirrors[i];
954 mirror = FF_LAYOUT_COMP(pgio->pg_lseg, i);
955 pgm->pg_bsize = mirror->mirror_ds->ds_versions[0].wsize;
958 return;
960 out_mds:
961 pnfs_put_lseg(pgio->pg_lseg);
962 pgio->pg_lseg = NULL;
963 nfs_pageio_reset_write_mds(pgio);
966 static unsigned int
967 ff_layout_pg_get_mirror_count_write(struct nfs_pageio_descriptor *pgio,
968 struct nfs_page *req)
970 if (!pgio->pg_lseg) {
971 pgio->pg_lseg = pnfs_update_layout(pgio->pg_inode,
972 req->wb_context,
974 NFS4_MAX_UINT64,
975 IOMODE_RW,
976 false,
977 GFP_NOFS);
978 if (IS_ERR(pgio->pg_lseg)) {
979 pgio->pg_error = PTR_ERR(pgio->pg_lseg);
980 pgio->pg_lseg = NULL;
981 goto out;
984 if (pgio->pg_lseg)
985 return FF_LAYOUT_MIRROR_COUNT(pgio->pg_lseg);
987 /* no lseg means that pnfs is not in use, so no mirroring here */
988 nfs_pageio_reset_write_mds(pgio);
989 out:
990 return 1;
993 static const struct nfs_pageio_ops ff_layout_pg_read_ops = {
994 .pg_init = ff_layout_pg_init_read,
995 .pg_test = pnfs_generic_pg_test,
996 .pg_doio = pnfs_generic_pg_readpages,
997 .pg_cleanup = pnfs_generic_pg_cleanup,
1000 static const struct nfs_pageio_ops ff_layout_pg_write_ops = {
1001 .pg_init = ff_layout_pg_init_write,
1002 .pg_test = pnfs_generic_pg_test,
1003 .pg_doio = pnfs_generic_pg_writepages,
1004 .pg_get_mirror_count = ff_layout_pg_get_mirror_count_write,
1005 .pg_cleanup = pnfs_generic_pg_cleanup,
1008 static void ff_layout_reset_write(struct nfs_pgio_header *hdr, bool retry_pnfs)
1010 struct rpc_task *task = &hdr->task;
1012 pnfs_layoutcommit_inode(hdr->inode, false);
1014 if (retry_pnfs) {
1015 dprintk("%s Reset task %5u for i/o through pNFS "
1016 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1017 hdr->task.tk_pid,
1018 hdr->inode->i_sb->s_id,
1019 (unsigned long long)NFS_FILEID(hdr->inode),
1020 hdr->args.count,
1021 (unsigned long long)hdr->args.offset);
1023 hdr->completion_ops->reschedule_io(hdr);
1024 return;
1027 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1028 dprintk("%s Reset task %5u for i/o through MDS "
1029 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1030 hdr->task.tk_pid,
1031 hdr->inode->i_sb->s_id,
1032 (unsigned long long)NFS_FILEID(hdr->inode),
1033 hdr->args.count,
1034 (unsigned long long)hdr->args.offset);
1036 task->tk_status = pnfs_write_done_resend_to_mds(hdr);
1040 static void ff_layout_reset_read(struct nfs_pgio_header *hdr)
1042 struct rpc_task *task = &hdr->task;
1044 pnfs_layoutcommit_inode(hdr->inode, false);
1046 if (!test_and_set_bit(NFS_IOHDR_REDO, &hdr->flags)) {
1047 dprintk("%s Reset task %5u for i/o through MDS "
1048 "(req %s/%llu, %u bytes @ offset %llu)\n", __func__,
1049 hdr->task.tk_pid,
1050 hdr->inode->i_sb->s_id,
1051 (unsigned long long)NFS_FILEID(hdr->inode),
1052 hdr->args.count,
1053 (unsigned long long)hdr->args.offset);
1055 task->tk_status = pnfs_read_done_resend_to_mds(hdr);
1059 static int ff_layout_async_handle_error_v4(struct rpc_task *task,
1060 struct nfs4_state *state,
1061 struct nfs_client *clp,
1062 struct pnfs_layout_segment *lseg,
1063 int idx)
1065 struct pnfs_layout_hdr *lo = lseg->pls_layout;
1066 struct inode *inode = lo->plh_inode;
1067 struct nfs_server *mds_server = NFS_SERVER(inode);
1069 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1070 struct nfs_client *mds_client = mds_server->nfs_client;
1071 struct nfs4_slot_table *tbl = &clp->cl_session->fc_slot_table;
1073 if (task->tk_status >= 0)
1074 return 0;
1076 switch (task->tk_status) {
1077 /* MDS state errors */
1078 case -NFS4ERR_DELEG_REVOKED:
1079 case -NFS4ERR_ADMIN_REVOKED:
1080 case -NFS4ERR_BAD_STATEID:
1081 if (state == NULL)
1082 break;
1083 nfs_remove_bad_delegation(state->inode);
1084 case -NFS4ERR_OPENMODE:
1085 if (state == NULL)
1086 break;
1087 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
1088 goto out_bad_stateid;
1089 goto wait_on_recovery;
1090 case -NFS4ERR_EXPIRED:
1091 if (state != NULL) {
1092 if (nfs4_schedule_stateid_recovery(mds_server, state) < 0)
1093 goto out_bad_stateid;
1095 nfs4_schedule_lease_recovery(mds_client);
1096 goto wait_on_recovery;
1097 /* DS session errors */
1098 case -NFS4ERR_BADSESSION:
1099 case -NFS4ERR_BADSLOT:
1100 case -NFS4ERR_BAD_HIGH_SLOT:
1101 case -NFS4ERR_DEADSESSION:
1102 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1103 case -NFS4ERR_SEQ_FALSE_RETRY:
1104 case -NFS4ERR_SEQ_MISORDERED:
1105 dprintk("%s ERROR %d, Reset session. Exchangeid "
1106 "flags 0x%x\n", __func__, task->tk_status,
1107 clp->cl_exchange_flags);
1108 nfs4_schedule_session_recovery(clp->cl_session, task->tk_status);
1109 break;
1110 case -NFS4ERR_DELAY:
1111 case -NFS4ERR_GRACE:
1112 rpc_delay(task, FF_LAYOUT_POLL_RETRY_MAX);
1113 break;
1114 case -NFS4ERR_RETRY_UNCACHED_REP:
1115 break;
1116 /* Invalidate Layout errors */
1117 case -NFS4ERR_PNFS_NO_LAYOUT:
1118 case -ESTALE: /* mapped NFS4ERR_STALE */
1119 case -EBADHANDLE: /* mapped NFS4ERR_BADHANDLE */
1120 case -EISDIR: /* mapped NFS4ERR_ISDIR */
1121 case -NFS4ERR_FHEXPIRED:
1122 case -NFS4ERR_WRONG_TYPE:
1123 dprintk("%s Invalid layout error %d\n", __func__,
1124 task->tk_status);
1126 * Destroy layout so new i/o will get a new layout.
1127 * Layout will not be destroyed until all current lseg
1128 * references are put. Mark layout as invalid to resend failed
1129 * i/o and all i/o waiting on the slot table to the MDS until
1130 * layout is destroyed and a new valid layout is obtained.
1132 pnfs_destroy_layout(NFS_I(inode));
1133 rpc_wake_up(&tbl->slot_tbl_waitq);
1134 goto reset;
1135 /* RPC connection errors */
1136 case -ECONNREFUSED:
1137 case -EHOSTDOWN:
1138 case -EHOSTUNREACH:
1139 case -ENETUNREACH:
1140 case -EIO:
1141 case -ETIMEDOUT:
1142 case -EPIPE:
1143 dprintk("%s DS connection error %d\n", __func__,
1144 task->tk_status);
1145 nfs4_mark_deviceid_unavailable(devid);
1146 rpc_wake_up(&tbl->slot_tbl_waitq);
1147 /* fall through */
1148 default:
1149 if (ff_layout_avoid_mds_available_ds(lseg))
1150 return -NFS4ERR_RESET_TO_PNFS;
1151 reset:
1152 dprintk("%s Retry through MDS. Error %d\n", __func__,
1153 task->tk_status);
1154 return -NFS4ERR_RESET_TO_MDS;
1156 out:
1157 task->tk_status = 0;
1158 return -EAGAIN;
1159 out_bad_stateid:
1160 task->tk_status = -EIO;
1161 return 0;
1162 wait_on_recovery:
1163 rpc_sleep_on(&mds_client->cl_rpcwaitq, task, NULL);
1164 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &mds_client->cl_state) == 0)
1165 rpc_wake_up_queued_task(&mds_client->cl_rpcwaitq, task);
1166 goto out;
1169 /* Retry all errors through either pNFS or MDS except for -EJUKEBOX */
1170 static int ff_layout_async_handle_error_v3(struct rpc_task *task,
1171 struct pnfs_layout_segment *lseg,
1172 int idx)
1174 struct nfs4_deviceid_node *devid = FF_LAYOUT_DEVID_NODE(lseg, idx);
1176 if (task->tk_status >= 0)
1177 return 0;
1179 switch (task->tk_status) {
1180 /* File access problems. Don't mark the device as unavailable */
1181 case -EACCES:
1182 case -ESTALE:
1183 case -EISDIR:
1184 case -EBADHANDLE:
1185 case -ELOOP:
1186 case -ENOSPC:
1187 break;
1188 case -EJUKEBOX:
1189 nfs_inc_stats(lseg->pls_layout->plh_inode, NFSIOS_DELAY);
1190 goto out_retry;
1191 default:
1192 dprintk("%s DS connection error %d\n", __func__,
1193 task->tk_status);
1194 nfs4_mark_deviceid_unavailable(devid);
1196 /* FIXME: Need to prevent infinite looping here. */
1197 return -NFS4ERR_RESET_TO_PNFS;
1198 out_retry:
1199 task->tk_status = 0;
1200 rpc_restart_call_prepare(task);
1201 rpc_delay(task, NFS_JUKEBOX_RETRY_TIME);
1202 return -EAGAIN;
1205 static int ff_layout_async_handle_error(struct rpc_task *task,
1206 struct nfs4_state *state,
1207 struct nfs_client *clp,
1208 struct pnfs_layout_segment *lseg,
1209 int idx)
1211 int vers = clp->cl_nfs_mod->rpc_vers->number;
1213 switch (vers) {
1214 case 3:
1215 return ff_layout_async_handle_error_v3(task, lseg, idx);
1216 case 4:
1217 return ff_layout_async_handle_error_v4(task, state, clp,
1218 lseg, idx);
1219 default:
1220 /* should never happen */
1221 WARN_ON_ONCE(1);
1222 return 0;
1226 static void ff_layout_io_track_ds_error(struct pnfs_layout_segment *lseg,
1227 int idx, u64 offset, u64 length,
1228 u32 status, int opnum, int error)
1230 struct nfs4_ff_layout_mirror *mirror;
1231 int err;
1233 if (status == 0) {
1234 switch (error) {
1235 case -ETIMEDOUT:
1236 case -EPFNOSUPPORT:
1237 case -EPROTONOSUPPORT:
1238 case -EOPNOTSUPP:
1239 case -ECONNREFUSED:
1240 case -ECONNRESET:
1241 case -EHOSTDOWN:
1242 case -EHOSTUNREACH:
1243 case -ENETUNREACH:
1244 case -EADDRINUSE:
1245 case -ENOBUFS:
1246 case -EPIPE:
1247 case -EPERM:
1248 status = NFS4ERR_NXIO;
1249 break;
1250 case -EACCES:
1251 status = NFS4ERR_ACCESS;
1252 break;
1253 default:
1254 return;
1258 switch (status) {
1259 case NFS4ERR_DELAY:
1260 case NFS4ERR_GRACE:
1261 return;
1262 default:
1263 break;
1266 mirror = FF_LAYOUT_COMP(lseg, idx);
1267 err = ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
1268 mirror, offset, length, status, opnum,
1269 GFP_NOIO);
1270 pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode, lseg);
1271 dprintk("%s: err %d op %d status %u\n", __func__, err, opnum, status);
1274 /* NFS_PROTO call done callback routines */
1275 static int ff_layout_read_done_cb(struct rpc_task *task,
1276 struct nfs_pgio_header *hdr)
1278 int err;
1280 trace_nfs4_pnfs_read(hdr, task->tk_status);
1281 if (task->tk_status < 0)
1282 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1283 hdr->args.offset, hdr->args.count,
1284 hdr->res.op_status, OP_READ,
1285 task->tk_status);
1286 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1287 hdr->ds_clp, hdr->lseg,
1288 hdr->pgio_mirror_idx);
1290 switch (err) {
1291 case -NFS4ERR_RESET_TO_PNFS:
1292 if (ff_layout_choose_best_ds_for_read(hdr->lseg,
1293 hdr->pgio_mirror_idx + 1,
1294 &hdr->pgio_mirror_idx))
1295 goto out_eagain;
1296 pnfs_read_resend_pnfs(hdr);
1297 return task->tk_status;
1298 case -NFS4ERR_RESET_TO_MDS:
1299 ff_layout_reset_read(hdr);
1300 return task->tk_status;
1301 case -EAGAIN:
1302 goto out_eagain;
1305 return 0;
1306 out_eagain:
1307 rpc_restart_call_prepare(task);
1308 return -EAGAIN;
1311 static bool
1312 ff_layout_need_layoutcommit(struct pnfs_layout_segment *lseg)
1314 return !(FF_LAYOUT_LSEG(lseg)->flags & FF_FLAGS_NO_LAYOUTCOMMIT);
1318 * We reference the rpc_cred of the first WRITE that triggers the need for
1319 * a LAYOUTCOMMIT, and use it to send the layoutcommit compound.
1320 * rfc5661 is not clear about which credential should be used.
1322 * Flexlayout client should treat DS replied FILE_SYNC as DATA_SYNC, so
1323 * to follow http://www.rfc-editor.org/errata_search.php?rfc=5661&eid=2751
1324 * we always send layoutcommit after DS writes.
1326 static void
1327 ff_layout_set_layoutcommit(struct inode *inode,
1328 struct pnfs_layout_segment *lseg,
1329 loff_t end_offset)
1331 if (!ff_layout_need_layoutcommit(lseg))
1332 return;
1334 pnfs_set_layoutcommit(inode, lseg, end_offset);
1335 dprintk("%s inode %lu pls_end_pos %llu\n", __func__, inode->i_ino,
1336 (unsigned long long) NFS_I(inode)->layout->plh_lwb);
1339 static bool
1340 ff_layout_device_unavailable(struct pnfs_layout_segment *lseg, int idx)
1342 /* No mirroring for now */
1343 struct nfs4_deviceid_node *node = FF_LAYOUT_DEVID_NODE(lseg, idx);
1345 return ff_layout_test_devid_unavailable(node);
1348 static void ff_layout_read_record_layoutstats_start(struct rpc_task *task,
1349 struct nfs_pgio_header *hdr)
1351 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1352 return;
1353 nfs4_ff_layout_stat_io_start_read(hdr->inode,
1354 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1355 hdr->args.count,
1356 task->tk_start);
1359 static void ff_layout_read_record_layoutstats_done(struct rpc_task *task,
1360 struct nfs_pgio_header *hdr)
1362 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1363 return;
1364 nfs4_ff_layout_stat_io_end_read(task,
1365 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1366 hdr->args.count,
1367 hdr->res.count);
1370 static int ff_layout_read_prepare_common(struct rpc_task *task,
1371 struct nfs_pgio_header *hdr)
1373 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1374 rpc_exit(task, -EIO);
1375 return -EIO;
1377 if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) {
1378 rpc_exit(task, -EHOSTDOWN);
1379 return -EAGAIN;
1382 ff_layout_read_record_layoutstats_start(task, hdr);
1383 return 0;
1387 * Call ops for the async read/write cases
1388 * In the case of dense layouts, the offset needs to be reset to its
1389 * original value.
1391 static void ff_layout_read_prepare_v3(struct rpc_task *task, void *data)
1393 struct nfs_pgio_header *hdr = data;
1395 if (ff_layout_read_prepare_common(task, hdr))
1396 return;
1398 rpc_call_start(task);
1401 static int ff_layout_setup_sequence(struct nfs_client *ds_clp,
1402 struct nfs4_sequence_args *args,
1403 struct nfs4_sequence_res *res,
1404 struct rpc_task *task)
1406 if (ds_clp->cl_session)
1407 return nfs41_setup_sequence(ds_clp->cl_session,
1408 args,
1409 res,
1410 task);
1411 return nfs40_setup_sequence(ds_clp->cl_slot_tbl,
1412 args,
1413 res,
1414 task);
1417 static void ff_layout_read_prepare_v4(struct rpc_task *task, void *data)
1419 struct nfs_pgio_header *hdr = data;
1421 if (ff_layout_setup_sequence(hdr->ds_clp,
1422 &hdr->args.seq_args,
1423 &hdr->res.seq_res,
1424 task))
1425 return;
1427 if (ff_layout_read_prepare_common(task, hdr))
1428 return;
1430 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1431 hdr->args.lock_context, FMODE_READ) == -EIO)
1432 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1435 static void ff_layout_read_call_done(struct rpc_task *task, void *data)
1437 struct nfs_pgio_header *hdr = data;
1439 dprintk("--> %s task->tk_status %d\n", __func__, task->tk_status);
1441 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1442 task->tk_status == 0) {
1443 nfs4_sequence_done(task, &hdr->res.seq_res);
1444 return;
1447 /* Note this may cause RPC to be resent */
1448 hdr->mds_ops->rpc_call_done(task, hdr);
1451 static void ff_layout_read_count_stats(struct rpc_task *task, void *data)
1453 struct nfs_pgio_header *hdr = data;
1455 ff_layout_read_record_layoutstats_done(task, hdr);
1456 rpc_count_iostats_metrics(task,
1457 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_READ]);
1460 static void ff_layout_read_release(void *data)
1462 struct nfs_pgio_header *hdr = data;
1464 ff_layout_read_record_layoutstats_done(&hdr->task, hdr);
1465 pnfs_generic_rw_release(data);
1469 static int ff_layout_write_done_cb(struct rpc_task *task,
1470 struct nfs_pgio_header *hdr)
1472 loff_t end_offs = 0;
1473 int err;
1475 trace_nfs4_pnfs_write(hdr, task->tk_status);
1476 if (task->tk_status < 0)
1477 ff_layout_io_track_ds_error(hdr->lseg, hdr->pgio_mirror_idx,
1478 hdr->args.offset, hdr->args.count,
1479 hdr->res.op_status, OP_WRITE,
1480 task->tk_status);
1481 err = ff_layout_async_handle_error(task, hdr->args.context->state,
1482 hdr->ds_clp, hdr->lseg,
1483 hdr->pgio_mirror_idx);
1485 switch (err) {
1486 case -NFS4ERR_RESET_TO_PNFS:
1487 ff_layout_reset_write(hdr, true);
1488 return task->tk_status;
1489 case -NFS4ERR_RESET_TO_MDS:
1490 ff_layout_reset_write(hdr, false);
1491 return task->tk_status;
1492 case -EAGAIN:
1493 return -EAGAIN;
1496 if (hdr->res.verf->committed == NFS_FILE_SYNC ||
1497 hdr->res.verf->committed == NFS_DATA_SYNC)
1498 end_offs = hdr->mds_offset + (loff_t)hdr->res.count;
1500 /* Note: if the write is unstable, don't set end_offs until commit */
1501 ff_layout_set_layoutcommit(hdr->inode, hdr->lseg, end_offs);
1503 /* zero out fattr since we don't care DS attr at all */
1504 hdr->fattr.valid = 0;
1505 if (task->tk_status >= 0)
1506 nfs_writeback_update_inode(hdr);
1508 return 0;
1511 static int ff_layout_commit_done_cb(struct rpc_task *task,
1512 struct nfs_commit_data *data)
1514 int err;
1516 trace_nfs4_pnfs_commit_ds(data, task->tk_status);
1517 if (task->tk_status < 0)
1518 ff_layout_io_track_ds_error(data->lseg, data->ds_commit_index,
1519 data->args.offset, data->args.count,
1520 data->res.op_status, OP_COMMIT,
1521 task->tk_status);
1522 err = ff_layout_async_handle_error(task, NULL, data->ds_clp,
1523 data->lseg, data->ds_commit_index);
1525 switch (err) {
1526 case -NFS4ERR_RESET_TO_PNFS:
1527 pnfs_generic_prepare_to_resend_writes(data);
1528 return -EAGAIN;
1529 case -NFS4ERR_RESET_TO_MDS:
1530 pnfs_generic_prepare_to_resend_writes(data);
1531 return -EAGAIN;
1532 case -EAGAIN:
1533 rpc_restart_call_prepare(task);
1534 return -EAGAIN;
1537 ff_layout_set_layoutcommit(data->inode, data->lseg, data->lwb);
1539 return 0;
1542 static void ff_layout_write_record_layoutstats_start(struct rpc_task *task,
1543 struct nfs_pgio_header *hdr)
1545 if (test_and_set_bit(NFS_IOHDR_STAT, &hdr->flags))
1546 return;
1547 nfs4_ff_layout_stat_io_start_write(hdr->inode,
1548 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1549 hdr->args.count,
1550 task->tk_start);
1553 static void ff_layout_write_record_layoutstats_done(struct rpc_task *task,
1554 struct nfs_pgio_header *hdr)
1556 if (!test_and_clear_bit(NFS_IOHDR_STAT, &hdr->flags))
1557 return;
1558 nfs4_ff_layout_stat_io_end_write(task,
1559 FF_LAYOUT_COMP(hdr->lseg, hdr->pgio_mirror_idx),
1560 hdr->args.count, hdr->res.count,
1561 hdr->res.verf->committed);
1564 static int ff_layout_write_prepare_common(struct rpc_task *task,
1565 struct nfs_pgio_header *hdr)
1567 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags))) {
1568 rpc_exit(task, -EIO);
1569 return -EIO;
1572 if (ff_layout_device_unavailable(hdr->lseg, hdr->pgio_mirror_idx)) {
1573 rpc_exit(task, -EHOSTDOWN);
1574 return -EAGAIN;
1577 ff_layout_write_record_layoutstats_start(task, hdr);
1578 return 0;
1581 static void ff_layout_write_prepare_v3(struct rpc_task *task, void *data)
1583 struct nfs_pgio_header *hdr = data;
1585 if (ff_layout_write_prepare_common(task, hdr))
1586 return;
1588 rpc_call_start(task);
1591 static void ff_layout_write_prepare_v4(struct rpc_task *task, void *data)
1593 struct nfs_pgio_header *hdr = data;
1595 if (ff_layout_setup_sequence(hdr->ds_clp,
1596 &hdr->args.seq_args,
1597 &hdr->res.seq_res,
1598 task))
1599 return;
1601 if (ff_layout_write_prepare_common(task, hdr))
1602 return;
1604 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
1605 hdr->args.lock_context, FMODE_WRITE) == -EIO)
1606 rpc_exit(task, -EIO); /* lost lock, terminate I/O */
1609 static void ff_layout_write_call_done(struct rpc_task *task, void *data)
1611 struct nfs_pgio_header *hdr = data;
1613 if (test_bit(NFS_IOHDR_REDO, &hdr->flags) &&
1614 task->tk_status == 0) {
1615 nfs4_sequence_done(task, &hdr->res.seq_res);
1616 return;
1619 /* Note this may cause RPC to be resent */
1620 hdr->mds_ops->rpc_call_done(task, hdr);
1623 static void ff_layout_write_count_stats(struct rpc_task *task, void *data)
1625 struct nfs_pgio_header *hdr = data;
1627 ff_layout_write_record_layoutstats_done(task, hdr);
1628 rpc_count_iostats_metrics(task,
1629 &NFS_CLIENT(hdr->inode)->cl_metrics[NFSPROC4_CLNT_WRITE]);
1632 static void ff_layout_write_release(void *data)
1634 struct nfs_pgio_header *hdr = data;
1636 ff_layout_write_record_layoutstats_done(&hdr->task, hdr);
1637 pnfs_generic_rw_release(data);
1640 static void ff_layout_commit_record_layoutstats_start(struct rpc_task *task,
1641 struct nfs_commit_data *cdata)
1643 if (test_and_set_bit(NFS_IOHDR_STAT, &cdata->flags))
1644 return;
1645 nfs4_ff_layout_stat_io_start_write(cdata->inode,
1646 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1647 0, task->tk_start);
1650 static void ff_layout_commit_record_layoutstats_done(struct rpc_task *task,
1651 struct nfs_commit_data *cdata)
1653 struct nfs_page *req;
1654 __u64 count = 0;
1656 if (!test_and_clear_bit(NFS_IOHDR_STAT, &cdata->flags))
1657 return;
1659 if (task->tk_status == 0) {
1660 list_for_each_entry(req, &cdata->pages, wb_list)
1661 count += req->wb_bytes;
1663 nfs4_ff_layout_stat_io_end_write(task,
1664 FF_LAYOUT_COMP(cdata->lseg, cdata->ds_commit_index),
1665 count, count, NFS_FILE_SYNC);
1668 static void ff_layout_commit_prepare_common(struct rpc_task *task,
1669 struct nfs_commit_data *cdata)
1671 ff_layout_commit_record_layoutstats_start(task, cdata);
1674 static void ff_layout_commit_prepare_v3(struct rpc_task *task, void *data)
1676 ff_layout_commit_prepare_common(task, data);
1677 rpc_call_start(task);
1680 static void ff_layout_commit_prepare_v4(struct rpc_task *task, void *data)
1682 struct nfs_commit_data *wdata = data;
1684 if (ff_layout_setup_sequence(wdata->ds_clp,
1685 &wdata->args.seq_args,
1686 &wdata->res.seq_res,
1687 task))
1688 return;
1689 ff_layout_commit_prepare_common(task, data);
1692 static void ff_layout_commit_done(struct rpc_task *task, void *data)
1694 pnfs_generic_write_commit_done(task, data);
1697 static void ff_layout_commit_count_stats(struct rpc_task *task, void *data)
1699 struct nfs_commit_data *cdata = data;
1701 ff_layout_commit_record_layoutstats_done(task, cdata);
1702 rpc_count_iostats_metrics(task,
1703 &NFS_CLIENT(cdata->inode)->cl_metrics[NFSPROC4_CLNT_COMMIT]);
1706 static void ff_layout_commit_release(void *data)
1708 struct nfs_commit_data *cdata = data;
1710 ff_layout_commit_record_layoutstats_done(&cdata->task, cdata);
1711 pnfs_generic_commit_release(data);
1714 static const struct rpc_call_ops ff_layout_read_call_ops_v3 = {
1715 .rpc_call_prepare = ff_layout_read_prepare_v3,
1716 .rpc_call_done = ff_layout_read_call_done,
1717 .rpc_count_stats = ff_layout_read_count_stats,
1718 .rpc_release = ff_layout_read_release,
1721 static const struct rpc_call_ops ff_layout_read_call_ops_v4 = {
1722 .rpc_call_prepare = ff_layout_read_prepare_v4,
1723 .rpc_call_done = ff_layout_read_call_done,
1724 .rpc_count_stats = ff_layout_read_count_stats,
1725 .rpc_release = ff_layout_read_release,
1728 static const struct rpc_call_ops ff_layout_write_call_ops_v3 = {
1729 .rpc_call_prepare = ff_layout_write_prepare_v3,
1730 .rpc_call_done = ff_layout_write_call_done,
1731 .rpc_count_stats = ff_layout_write_count_stats,
1732 .rpc_release = ff_layout_write_release,
1735 static const struct rpc_call_ops ff_layout_write_call_ops_v4 = {
1736 .rpc_call_prepare = ff_layout_write_prepare_v4,
1737 .rpc_call_done = ff_layout_write_call_done,
1738 .rpc_count_stats = ff_layout_write_count_stats,
1739 .rpc_release = ff_layout_write_release,
1742 static const struct rpc_call_ops ff_layout_commit_call_ops_v3 = {
1743 .rpc_call_prepare = ff_layout_commit_prepare_v3,
1744 .rpc_call_done = ff_layout_commit_done,
1745 .rpc_count_stats = ff_layout_commit_count_stats,
1746 .rpc_release = ff_layout_commit_release,
1749 static const struct rpc_call_ops ff_layout_commit_call_ops_v4 = {
1750 .rpc_call_prepare = ff_layout_commit_prepare_v4,
1751 .rpc_call_done = ff_layout_commit_done,
1752 .rpc_count_stats = ff_layout_commit_count_stats,
1753 .rpc_release = ff_layout_commit_release,
1756 static enum pnfs_try_status
1757 ff_layout_read_pagelist(struct nfs_pgio_header *hdr)
1759 struct pnfs_layout_segment *lseg = hdr->lseg;
1760 struct nfs4_pnfs_ds *ds;
1761 struct rpc_clnt *ds_clnt;
1762 struct rpc_cred *ds_cred;
1763 loff_t offset = hdr->args.offset;
1764 u32 idx = hdr->pgio_mirror_idx;
1765 int vers;
1766 struct nfs_fh *fh;
1768 dprintk("--> %s ino %lu pgbase %u req %Zu@%llu\n",
1769 __func__, hdr->inode->i_ino,
1770 hdr->args.pgbase, (size_t)hdr->args.count, offset);
1772 ds = nfs4_ff_layout_prepare_ds(lseg, idx, false);
1773 if (!ds)
1774 goto out_failed;
1776 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1777 hdr->inode);
1778 if (IS_ERR(ds_clnt))
1779 goto out_failed;
1781 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
1782 if (!ds_cred)
1783 goto out_failed;
1785 vers = nfs4_ff_layout_ds_version(lseg, idx);
1787 dprintk("%s USE DS: %s cl_count %d vers %d\n", __func__,
1788 ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count), vers);
1790 hdr->pgio_done_cb = ff_layout_read_done_cb;
1791 atomic_inc(&ds->ds_clp->cl_count);
1792 hdr->ds_clp = ds->ds_clp;
1793 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1794 if (fh)
1795 hdr->args.fh = fh;
1797 * Note that if we ever decide to split across DSes,
1798 * then we may need to handle dense-like offsets.
1800 hdr->args.offset = offset;
1801 hdr->mds_offset = offset;
1803 /* Perform an asynchronous read to ds */
1804 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1805 vers == 3 ? &ff_layout_read_call_ops_v3 :
1806 &ff_layout_read_call_ops_v4,
1807 0, RPC_TASK_SOFTCONN);
1808 put_rpccred(ds_cred);
1809 return PNFS_ATTEMPTED;
1811 out_failed:
1812 if (ff_layout_avoid_mds_available_ds(lseg))
1813 return PNFS_TRY_AGAIN;
1814 return PNFS_NOT_ATTEMPTED;
1817 /* Perform async writes. */
1818 static enum pnfs_try_status
1819 ff_layout_write_pagelist(struct nfs_pgio_header *hdr, int sync)
1821 struct pnfs_layout_segment *lseg = hdr->lseg;
1822 struct nfs4_pnfs_ds *ds;
1823 struct rpc_clnt *ds_clnt;
1824 struct rpc_cred *ds_cred;
1825 loff_t offset = hdr->args.offset;
1826 int vers;
1827 struct nfs_fh *fh;
1828 int idx = hdr->pgio_mirror_idx;
1830 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1831 if (!ds)
1832 return PNFS_NOT_ATTEMPTED;
1834 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1835 hdr->inode);
1836 if (IS_ERR(ds_clnt))
1837 return PNFS_NOT_ATTEMPTED;
1839 ds_cred = ff_layout_get_ds_cred(lseg, idx, hdr->cred);
1840 if (!ds_cred)
1841 return PNFS_NOT_ATTEMPTED;
1843 vers = nfs4_ff_layout_ds_version(lseg, idx);
1845 dprintk("%s ino %lu sync %d req %Zu@%llu DS: %s cl_count %d vers %d\n",
1846 __func__, hdr->inode->i_ino, sync, (size_t) hdr->args.count,
1847 offset, ds->ds_remotestr, atomic_read(&ds->ds_clp->cl_count),
1848 vers);
1850 hdr->pgio_done_cb = ff_layout_write_done_cb;
1851 atomic_inc(&ds->ds_clp->cl_count);
1852 hdr->ds_clp = ds->ds_clp;
1853 hdr->ds_commit_idx = idx;
1854 fh = nfs4_ff_layout_select_ds_fh(lseg, idx);
1855 if (fh)
1856 hdr->args.fh = fh;
1859 * Note that if we ever decide to split across DSes,
1860 * then we may need to handle dense-like offsets.
1862 hdr->args.offset = offset;
1864 /* Perform an asynchronous write */
1865 nfs_initiate_pgio(ds_clnt, hdr, ds_cred, ds->ds_clp->rpc_ops,
1866 vers == 3 ? &ff_layout_write_call_ops_v3 :
1867 &ff_layout_write_call_ops_v4,
1868 sync, RPC_TASK_SOFTCONN);
1869 put_rpccred(ds_cred);
1870 return PNFS_ATTEMPTED;
1873 static u32 calc_ds_index_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1875 return i;
1878 static struct nfs_fh *
1879 select_ds_fh_from_commit(struct pnfs_layout_segment *lseg, u32 i)
1881 struct nfs4_ff_layout_segment *flseg = FF_LAYOUT_LSEG(lseg);
1883 /* FIXME: Assume that there is only one NFS version available
1884 * for the DS.
1886 return &flseg->mirror_array[i]->fh_versions[0];
1889 static int ff_layout_initiate_commit(struct nfs_commit_data *data, int how)
1891 struct pnfs_layout_segment *lseg = data->lseg;
1892 struct nfs4_pnfs_ds *ds;
1893 struct rpc_clnt *ds_clnt;
1894 struct rpc_cred *ds_cred;
1895 u32 idx;
1896 int vers, ret;
1897 struct nfs_fh *fh;
1899 idx = calc_ds_index_from_commit(lseg, data->ds_commit_index);
1900 ds = nfs4_ff_layout_prepare_ds(lseg, idx, true);
1901 if (!ds)
1902 goto out_err;
1904 ds_clnt = nfs4_ff_find_or_create_ds_client(lseg, idx, ds->ds_clp,
1905 data->inode);
1906 if (IS_ERR(ds_clnt))
1907 goto out_err;
1909 ds_cred = ff_layout_get_ds_cred(lseg, idx, data->cred);
1910 if (!ds_cred)
1911 goto out_err;
1913 vers = nfs4_ff_layout_ds_version(lseg, idx);
1915 dprintk("%s ino %lu, how %d cl_count %d vers %d\n", __func__,
1916 data->inode->i_ino, how, atomic_read(&ds->ds_clp->cl_count),
1917 vers);
1918 data->commit_done_cb = ff_layout_commit_done_cb;
1919 data->cred = ds_cred;
1920 atomic_inc(&ds->ds_clp->cl_count);
1921 data->ds_clp = ds->ds_clp;
1922 fh = select_ds_fh_from_commit(lseg, data->ds_commit_index);
1923 if (fh)
1924 data->args.fh = fh;
1926 ret = nfs_initiate_commit(ds_clnt, data, ds->ds_clp->rpc_ops,
1927 vers == 3 ? &ff_layout_commit_call_ops_v3 :
1928 &ff_layout_commit_call_ops_v4,
1929 how, RPC_TASK_SOFTCONN);
1930 put_rpccred(ds_cred);
1931 return ret;
1932 out_err:
1933 pnfs_generic_prepare_to_resend_writes(data);
1934 pnfs_generic_commit_release(data);
1935 return -EAGAIN;
1938 static int
1939 ff_layout_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
1940 int how, struct nfs_commit_info *cinfo)
1942 return pnfs_generic_commit_pagelist(inode, mds_pages, how, cinfo,
1943 ff_layout_initiate_commit);
1946 static struct pnfs_ds_commit_info *
1947 ff_layout_get_ds_info(struct inode *inode)
1949 struct pnfs_layout_hdr *layout = NFS_I(inode)->layout;
1951 if (layout == NULL)
1952 return NULL;
1954 return &FF_LAYOUT_FROM_HDR(layout)->commit_info;
1957 static void
1958 ff_layout_free_deviceid_node(struct nfs4_deviceid_node *d)
1960 nfs4_ff_layout_free_deviceid(container_of(d, struct nfs4_ff_layout_ds,
1961 id_node));
1964 static int ff_layout_encode_ioerr(struct nfs4_flexfile_layout *flo,
1965 struct xdr_stream *xdr,
1966 const struct nfs4_layoutreturn_args *args)
1968 struct pnfs_layout_hdr *hdr = &flo->generic_hdr;
1969 __be32 *start;
1970 int count = 0, ret = 0;
1972 start = xdr_reserve_space(xdr, 4);
1973 if (unlikely(!start))
1974 return -E2BIG;
1976 /* This assume we always return _ALL_ layouts */
1977 spin_lock(&hdr->plh_inode->i_lock);
1978 ret = ff_layout_encode_ds_ioerr(flo, xdr, &count, &args->range);
1979 spin_unlock(&hdr->plh_inode->i_lock);
1981 *start = cpu_to_be32(count);
1983 return ret;
1986 /* report nothing for now */
1987 static void ff_layout_encode_iostats(struct nfs4_flexfile_layout *flo,
1988 struct xdr_stream *xdr,
1989 const struct nfs4_layoutreturn_args *args)
1991 __be32 *p;
1993 p = xdr_reserve_space(xdr, 4);
1994 if (likely(p))
1995 *p = cpu_to_be32(0);
1998 static struct nfs4_deviceid_node *
1999 ff_layout_alloc_deviceid_node(struct nfs_server *server,
2000 struct pnfs_device *pdev, gfp_t gfp_flags)
2002 struct nfs4_ff_layout_ds *dsaddr;
2004 dsaddr = nfs4_ff_alloc_deviceid_node(server, pdev, gfp_flags);
2005 if (!dsaddr)
2006 return NULL;
2007 return &dsaddr->id_node;
2010 static void
2011 ff_layout_encode_layoutreturn(struct pnfs_layout_hdr *lo,
2012 struct xdr_stream *xdr,
2013 const struct nfs4_layoutreturn_args *args)
2015 struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
2016 __be32 *start;
2018 dprintk("%s: Begin\n", __func__);
2019 start = xdr_reserve_space(xdr, 4);
2020 BUG_ON(!start);
2022 ff_layout_encode_ioerr(flo, xdr, args);
2023 ff_layout_encode_iostats(flo, xdr, args);
2025 *start = cpu_to_be32((xdr->p - start - 1) * 4);
2026 dprintk("%s: Return\n", __func__);
2029 static int
2030 ff_layout_ntop4(const struct sockaddr *sap, char *buf, const size_t buflen)
2032 const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
2034 return snprintf(buf, buflen, "%pI4", &sin->sin_addr);
2037 static size_t
2038 ff_layout_ntop6_noscopeid(const struct sockaddr *sap, char *buf,
2039 const int buflen)
2041 const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
2042 const struct in6_addr *addr = &sin6->sin6_addr;
2045 * RFC 4291, Section 2.2.2
2047 * Shorthanded ANY address
2049 if (ipv6_addr_any(addr))
2050 return snprintf(buf, buflen, "::");
2053 * RFC 4291, Section 2.2.2
2055 * Shorthanded loopback address
2057 if (ipv6_addr_loopback(addr))
2058 return snprintf(buf, buflen, "::1");
2061 * RFC 4291, Section 2.2.3
2063 * Special presentation address format for mapped v4
2064 * addresses.
2066 if (ipv6_addr_v4mapped(addr))
2067 return snprintf(buf, buflen, "::ffff:%pI4",
2068 &addr->s6_addr32[3]);
2071 * RFC 4291, Section 2.2.1
2073 return snprintf(buf, buflen, "%pI6c", addr);
2076 /* Derived from rpc_sockaddr2uaddr */
2077 static void
2078 ff_layout_encode_netaddr(struct xdr_stream *xdr, struct nfs4_pnfs_ds_addr *da)
2080 struct sockaddr *sap = (struct sockaddr *)&da->da_addr;
2081 char portbuf[RPCBIND_MAXUADDRPLEN];
2082 char addrbuf[RPCBIND_MAXUADDRLEN];
2083 char *netid;
2084 unsigned short port;
2085 int len, netid_len;
2086 __be32 *p;
2088 switch (sap->sa_family) {
2089 case AF_INET:
2090 if (ff_layout_ntop4(sap, addrbuf, sizeof(addrbuf)) == 0)
2091 return;
2092 port = ntohs(((struct sockaddr_in *)sap)->sin_port);
2093 netid = "tcp";
2094 netid_len = 3;
2095 break;
2096 case AF_INET6:
2097 if (ff_layout_ntop6_noscopeid(sap, addrbuf, sizeof(addrbuf)) == 0)
2098 return;
2099 port = ntohs(((struct sockaddr_in6 *)sap)->sin6_port);
2100 netid = "tcp6";
2101 netid_len = 4;
2102 break;
2103 default:
2104 /* we only support tcp and tcp6 */
2105 WARN_ON_ONCE(1);
2106 return;
2109 snprintf(portbuf, sizeof(portbuf), ".%u.%u", port >> 8, port & 0xff);
2110 len = strlcat(addrbuf, portbuf, sizeof(addrbuf));
2112 p = xdr_reserve_space(xdr, 4 + netid_len);
2113 xdr_encode_opaque(p, netid, netid_len);
2115 p = xdr_reserve_space(xdr, 4 + len);
2116 xdr_encode_opaque(p, addrbuf, len);
2119 static void
2120 ff_layout_encode_nfstime(struct xdr_stream *xdr,
2121 ktime_t t)
2123 struct timespec64 ts;
2124 __be32 *p;
2126 p = xdr_reserve_space(xdr, 12);
2127 ts = ktime_to_timespec64(t);
2128 p = xdr_encode_hyper(p, ts.tv_sec);
2129 *p++ = cpu_to_be32(ts.tv_nsec);
2132 static void
2133 ff_layout_encode_io_latency(struct xdr_stream *xdr,
2134 struct nfs4_ff_io_stat *stat)
2136 __be32 *p;
2138 p = xdr_reserve_space(xdr, 5 * 8);
2139 p = xdr_encode_hyper(p, stat->ops_requested);
2140 p = xdr_encode_hyper(p, stat->bytes_requested);
2141 p = xdr_encode_hyper(p, stat->ops_completed);
2142 p = xdr_encode_hyper(p, stat->bytes_completed);
2143 p = xdr_encode_hyper(p, stat->bytes_not_delivered);
2144 ff_layout_encode_nfstime(xdr, stat->total_busy_time);
2145 ff_layout_encode_nfstime(xdr, stat->aggregate_completion_time);
2148 static void
2149 ff_layout_encode_layoutstats(struct xdr_stream *xdr,
2150 struct nfs42_layoutstat_args *args,
2151 struct nfs42_layoutstat_devinfo *devinfo)
2153 struct nfs4_ff_layout_mirror *mirror = devinfo->layout_private;
2154 struct nfs4_pnfs_ds_addr *da;
2155 struct nfs4_pnfs_ds *ds = mirror->mirror_ds->ds;
2156 struct nfs_fh *fh = &mirror->fh_versions[0];
2157 __be32 *p, *start;
2159 da = list_first_entry(&ds->ds_addrs, struct nfs4_pnfs_ds_addr, da_node);
2160 dprintk("%s: DS %s: encoding address %s\n",
2161 __func__, ds->ds_remotestr, da->da_remotestr);
2162 /* layoutupdate length */
2163 start = xdr_reserve_space(xdr, 4);
2164 /* netaddr4 */
2165 ff_layout_encode_netaddr(xdr, da);
2166 /* nfs_fh4 */
2167 p = xdr_reserve_space(xdr, 4 + fh->size);
2168 xdr_encode_opaque(p, fh->data, fh->size);
2169 /* ff_io_latency4 read */
2170 spin_lock(&mirror->lock);
2171 ff_layout_encode_io_latency(xdr, &mirror->read_stat.io_stat);
2172 /* ff_io_latency4 write */
2173 ff_layout_encode_io_latency(xdr, &mirror->write_stat.io_stat);
2174 spin_unlock(&mirror->lock);
2175 /* nfstime4 */
2176 ff_layout_encode_nfstime(xdr, ktime_sub(ktime_get(), mirror->start_time));
2177 /* bool */
2178 p = xdr_reserve_space(xdr, 4);
2179 *p = cpu_to_be32(false);
2181 *start = cpu_to_be32((xdr->p - start - 1) * 4);
2184 static int
2185 ff_layout_mirror_prepare_stats(struct nfs42_layoutstat_args *args,
2186 struct pnfs_layout_hdr *lo,
2187 int dev_limit)
2189 struct nfs4_flexfile_layout *ff_layout = FF_LAYOUT_FROM_HDR(lo);
2190 struct nfs4_ff_layout_mirror *mirror;
2191 struct nfs4_deviceid_node *dev;
2192 struct nfs42_layoutstat_devinfo *devinfo;
2193 int i = 0;
2195 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2196 if (i >= dev_limit)
2197 break;
2198 if (!mirror->mirror_ds)
2199 continue;
2200 /* mirror refcount put in cleanup_layoutstats */
2201 if (!atomic_inc_not_zero(&mirror->ref))
2202 continue;
2203 dev = &mirror->mirror_ds->id_node;
2204 devinfo = &args->devinfo[i];
2205 memcpy(&devinfo->dev_id, &dev->deviceid, NFS4_DEVICEID4_SIZE);
2206 devinfo->offset = 0;
2207 devinfo->length = NFS4_MAX_UINT64;
2208 devinfo->read_count = mirror->read_stat.io_stat.ops_completed;
2209 devinfo->read_bytes = mirror->read_stat.io_stat.bytes_completed;
2210 devinfo->write_count = mirror->write_stat.io_stat.ops_completed;
2211 devinfo->write_bytes = mirror->write_stat.io_stat.bytes_completed;
2212 devinfo->layout_type = LAYOUT_FLEX_FILES;
2213 devinfo->layoutstats_encode = ff_layout_encode_layoutstats;
2214 devinfo->layout_private = mirror;
2216 i++;
2218 return i;
2221 static int
2222 ff_layout_prepare_layoutstats(struct nfs42_layoutstat_args *args)
2224 struct nfs4_flexfile_layout *ff_layout;
2225 struct nfs4_ff_layout_mirror *mirror;
2226 int dev_count = 0;
2228 spin_lock(&args->inode->i_lock);
2229 ff_layout = FF_LAYOUT_FROM_HDR(NFS_I(args->inode)->layout);
2230 list_for_each_entry(mirror, &ff_layout->mirrors, mirrors) {
2231 if (atomic_read(&mirror->ref) != 0)
2232 dev_count ++;
2234 spin_unlock(&args->inode->i_lock);
2235 /* For now, send at most PNFS_LAYOUTSTATS_MAXDEV statistics */
2236 if (dev_count > PNFS_LAYOUTSTATS_MAXDEV) {
2237 dprintk("%s: truncating devinfo to limit (%d:%d)\n",
2238 __func__, dev_count, PNFS_LAYOUTSTATS_MAXDEV);
2239 dev_count = PNFS_LAYOUTSTATS_MAXDEV;
2241 args->devinfo = kmalloc_array(dev_count, sizeof(*args->devinfo), GFP_NOIO);
2242 if (!args->devinfo)
2243 return -ENOMEM;
2245 spin_lock(&args->inode->i_lock);
2246 args->num_dev = ff_layout_mirror_prepare_stats(args,
2247 &ff_layout->generic_hdr, dev_count);
2248 spin_unlock(&args->inode->i_lock);
2250 return 0;
2253 static void
2254 ff_layout_cleanup_layoutstats(struct nfs42_layoutstat_data *data)
2256 struct nfs4_ff_layout_mirror *mirror;
2257 int i;
2259 for (i = 0; i < data->args.num_dev; i++) {
2260 mirror = data->args.devinfo[i].layout_private;
2261 data->args.devinfo[i].layout_private = NULL;
2262 ff_layout_put_mirror(mirror);
2266 static struct pnfs_layoutdriver_type flexfilelayout_type = {
2267 .id = LAYOUT_FLEX_FILES,
2268 .name = "LAYOUT_FLEX_FILES",
2269 .owner = THIS_MODULE,
2270 .alloc_layout_hdr = ff_layout_alloc_layout_hdr,
2271 .free_layout_hdr = ff_layout_free_layout_hdr,
2272 .alloc_lseg = ff_layout_alloc_lseg,
2273 .free_lseg = ff_layout_free_lseg,
2274 .add_lseg = ff_layout_add_lseg,
2275 .pg_read_ops = &ff_layout_pg_read_ops,
2276 .pg_write_ops = &ff_layout_pg_write_ops,
2277 .get_ds_info = ff_layout_get_ds_info,
2278 .free_deviceid_node = ff_layout_free_deviceid_node,
2279 .mark_request_commit = pnfs_layout_mark_request_commit,
2280 .clear_request_commit = pnfs_generic_clear_request_commit,
2281 .scan_commit_lists = pnfs_generic_scan_commit_lists,
2282 .recover_commit_reqs = pnfs_generic_recover_commit_reqs,
2283 .commit_pagelist = ff_layout_commit_pagelist,
2284 .read_pagelist = ff_layout_read_pagelist,
2285 .write_pagelist = ff_layout_write_pagelist,
2286 .alloc_deviceid_node = ff_layout_alloc_deviceid_node,
2287 .encode_layoutreturn = ff_layout_encode_layoutreturn,
2288 .sync = pnfs_nfs_generic_sync,
2289 .prepare_layoutstats = ff_layout_prepare_layoutstats,
2290 .cleanup_layoutstats = ff_layout_cleanup_layoutstats,
2293 static int __init nfs4flexfilelayout_init(void)
2295 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Registering...\n",
2296 __func__);
2297 if (!ff_zero_group) {
2298 ff_zero_group = groups_alloc(0);
2299 if (!ff_zero_group)
2300 return -ENOMEM;
2302 return pnfs_register_layoutdriver(&flexfilelayout_type);
2305 static void __exit nfs4flexfilelayout_exit(void)
2307 printk(KERN_INFO "%s: NFSv4 Flexfile Layout Driver Unregistering...\n",
2308 __func__);
2309 pnfs_unregister_layoutdriver(&flexfilelayout_type);
2310 if (ff_zero_group) {
2311 put_group_info(ff_zero_group);
2312 ff_zero_group = NULL;
2316 MODULE_ALIAS("nfs-layouttype4-4");
2318 MODULE_LICENSE("GPL");
2319 MODULE_DESCRIPTION("The NFSv4 flexfile layout driver");
2321 module_init(nfs4flexfilelayout_init);
2322 module_exit(nfs4flexfilelayout_exit);