GNU Linux-libre 4.19.211-gnu1
[releases.git] / fs / nfs / pnfs_nfs.c
1 /*
2  * Common NFS I/O  operations for the pnfs file based
3  * layout drivers.
4  *
5  * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
6  *
7  * Tom Haynes <loghyr@primarydata.com>
8  */
9
10 #include <linux/nfs_fs.h>
11 #include <linux/nfs_page.h>
12 #include <linux/sunrpc/addr.h>
13 #include <linux/module.h>
14
15 #include "nfs4session.h"
16 #include "internal.h"
17 #include "pnfs.h"
18
19 #define NFSDBG_FACILITY         NFSDBG_PNFS
20
21 void pnfs_generic_rw_release(void *data)
22 {
23         struct nfs_pgio_header *hdr = data;
24
25         nfs_put_client(hdr->ds_clp);
26         hdr->mds_ops->rpc_release(data);
27 }
28 EXPORT_SYMBOL_GPL(pnfs_generic_rw_release);
29
30 /* Fake up some data that will cause nfs_commit_release to retry the writes. */
31 void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data)
32 {
33         struct nfs_writeverf *verf = data->res.verf;
34
35         data->task.tk_status = 0;
36         memset(&verf->verifier, 0, sizeof(verf->verifier));
37         verf->committed = NFS_UNSTABLE;
38 }
39 EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes);
40
41 void pnfs_generic_write_commit_done(struct rpc_task *task, void *data)
42 {
43         struct nfs_commit_data *wdata = data;
44
45         /* Note this may cause RPC to be resent */
46         wdata->mds_ops->rpc_call_done(task, data);
47 }
48 EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done);
49
50 void pnfs_generic_commit_release(void *calldata)
51 {
52         struct nfs_commit_data *data = calldata;
53
54         data->completion_ops->completion(data);
55         pnfs_put_lseg(data->lseg);
56         nfs_put_client(data->ds_clp);
57         nfs_commitdata_release(data);
58 }
59 EXPORT_SYMBOL_GPL(pnfs_generic_commit_release);
60
61 /* The generic layer is about to remove the req from the commit list.
62  * If this will make the bucket empty, it will need to put the lseg reference.
63  * Note this must be called holding nfsi->commit_mutex
64  */
65 void
66 pnfs_generic_clear_request_commit(struct nfs_page *req,
67                                   struct nfs_commit_info *cinfo)
68 {
69         struct pnfs_layout_segment *freeme = NULL;
70
71         if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
72                 goto out;
73         cinfo->ds->nwritten--;
74         if (list_is_singular(&req->wb_list)) {
75                 struct pnfs_commit_bucket *bucket;
76
77                 bucket = list_first_entry(&req->wb_list,
78                                           struct pnfs_commit_bucket,
79                                           written);
80                 freeme = bucket->wlseg;
81                 bucket->wlseg = NULL;
82         }
83 out:
84         nfs_request_remove_commit_list(req, cinfo);
85         pnfs_put_lseg(freeme);
86 }
87 EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
88
89 static int
90 pnfs_generic_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
91                                  struct nfs_commit_info *cinfo,
92                                  int max)
93 {
94         struct list_head *src = &bucket->written;
95         struct list_head *dst = &bucket->committing;
96         int ret;
97
98         lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
99         ret = nfs_scan_commit_list(src, dst, cinfo, max);
100         if (ret) {
101                 cinfo->ds->nwritten -= ret;
102                 cinfo->ds->ncommitting += ret;
103                 if (bucket->clseg == NULL)
104                         bucket->clseg = pnfs_get_lseg(bucket->wlseg);
105                 if (list_empty(src)) {
106                         pnfs_put_lseg(bucket->wlseg);
107                         bucket->wlseg = NULL;
108                 }
109         }
110         return ret;
111 }
112
113 /* Move reqs from written to committing lists, returning count
114  * of number moved.
115  */
116 int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo,
117                                    int max)
118 {
119         int i, rv = 0, cnt;
120
121         lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
122         for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) {
123                 cnt = pnfs_generic_scan_ds_commit_list(&cinfo->ds->buckets[i],
124                                                        cinfo, max);
125                 max -= cnt;
126                 rv += cnt;
127         }
128         return rv;
129 }
130 EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
131
132 /* Pull everything off the committing lists and dump into @dst.  */
133 void pnfs_generic_recover_commit_reqs(struct list_head *dst,
134                                       struct nfs_commit_info *cinfo)
135 {
136         struct pnfs_commit_bucket *b;
137         struct pnfs_layout_segment *freeme;
138         int nwritten;
139         int i;
140
141         lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
142 restart:
143         for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) {
144                 nwritten = nfs_scan_commit_list(&b->written, dst, cinfo, 0);
145                 if (!nwritten)
146                         continue;
147                 cinfo->ds->nwritten -= nwritten;
148                 if (list_empty(&b->written)) {
149                         freeme = b->wlseg;
150                         b->wlseg = NULL;
151                         pnfs_put_lseg(freeme);
152                         goto restart;
153                 }
154         }
155 }
156 EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
157
158 static void pnfs_generic_retry_commit(struct nfs_commit_info *cinfo, int idx)
159 {
160         struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
161         struct pnfs_commit_bucket *bucket;
162         struct pnfs_layout_segment *freeme;
163         struct list_head *pos;
164         LIST_HEAD(pages);
165         int i;
166
167         mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
168         for (i = idx; i < fl_cinfo->nbuckets; i++) {
169                 bucket = &fl_cinfo->buckets[i];
170                 if (list_empty(&bucket->committing))
171                         continue;
172                 freeme = bucket->clseg;
173                 bucket->clseg = NULL;
174                 list_for_each(pos, &bucket->committing)
175                         cinfo->ds->ncommitting--;
176                 list_splice_init(&bucket->committing, &pages);
177                 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
178                 nfs_retry_commit(&pages, freeme, cinfo, i);
179                 pnfs_put_lseg(freeme);
180                 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
181         }
182         mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
183 }
184
185 static unsigned int
186 pnfs_generic_alloc_ds_commits(struct nfs_commit_info *cinfo,
187                               struct list_head *list)
188 {
189         struct pnfs_ds_commit_info *fl_cinfo;
190         struct pnfs_commit_bucket *bucket;
191         struct nfs_commit_data *data;
192         int i;
193         unsigned int nreq = 0;
194
195         fl_cinfo = cinfo->ds;
196         bucket = fl_cinfo->buckets;
197         for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) {
198                 if (list_empty(&bucket->committing))
199                         continue;
200                 data = nfs_commitdata_alloc(false);
201                 if (!data)
202                         break;
203                 data->ds_commit_index = i;
204                 list_add(&data->pages, list);
205                 nreq++;
206         }
207
208         /* Clean up on error */
209         pnfs_generic_retry_commit(cinfo, i);
210         return nreq;
211 }
212
213 static inline
214 void pnfs_fetch_commit_bucket_list(struct list_head *pages,
215                 struct nfs_commit_data *data,
216                 struct nfs_commit_info *cinfo)
217 {
218         struct pnfs_commit_bucket *bucket;
219         struct list_head *pos;
220
221         bucket = &cinfo->ds->buckets[data->ds_commit_index];
222         mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
223         list_for_each(pos, &bucket->committing)
224                 cinfo->ds->ncommitting--;
225         list_splice_init(&bucket->committing, pages);
226         data->lseg = bucket->clseg;
227         bucket->clseg = NULL;
228         mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
229
230 }
231
232 /* Helper function for pnfs_generic_commit_pagelist to catch an empty
233  * page list. This can happen when two commits race.
234  *
235  * This must be called instead of nfs_init_commit - call one or the other, but
236  * not both!
237  */
238 static bool
239 pnfs_generic_commit_cancel_empty_pagelist(struct list_head *pages,
240                                           struct nfs_commit_data *data,
241                                           struct nfs_commit_info *cinfo)
242 {
243         if (list_empty(pages)) {
244                 if (atomic_dec_and_test(&cinfo->mds->rpcs_out))
245                         wake_up_var(&cinfo->mds->rpcs_out);
246                 /* don't call nfs_commitdata_release - it tries to put
247                  * the open_context which is not acquired until nfs_init_commit
248                  * which has not been called on @data */
249                 WARN_ON_ONCE(data->context);
250                 nfs_commit_free(data);
251                 return true;
252         }
253
254         return false;
255 }
256
257 /* This follows nfs_commit_list pretty closely */
258 int
259 pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
260                              int how, struct nfs_commit_info *cinfo,
261                              int (*initiate_commit)(struct nfs_commit_data *data,
262                                                     int how))
263 {
264         struct nfs_commit_data *data, *tmp;
265         LIST_HEAD(list);
266         unsigned int nreq = 0;
267
268         if (!list_empty(mds_pages)) {
269                 data = nfs_commitdata_alloc(true);
270                 data->ds_commit_index = -1;
271                 list_add(&data->pages, &list);
272                 nreq++;
273         }
274
275         nreq += pnfs_generic_alloc_ds_commits(cinfo, &list);
276
277         if (nreq == 0)
278                 goto out;
279
280         atomic_add(nreq, &cinfo->mds->rpcs_out);
281
282         list_for_each_entry_safe(data, tmp, &list, pages) {
283                 list_del_init(&data->pages);
284                 if (data->ds_commit_index < 0) {
285                         /* another commit raced with us */
286                         if (pnfs_generic_commit_cancel_empty_pagelist(mds_pages,
287                                 data, cinfo))
288                                 continue;
289
290                         nfs_init_commit(data, mds_pages, NULL, cinfo);
291                         nfs_initiate_commit(NFS_CLIENT(inode), data,
292                                             NFS_PROTO(data->inode),
293                                             data->mds_ops, how, 0);
294                 } else {
295                         LIST_HEAD(pages);
296
297                         pnfs_fetch_commit_bucket_list(&pages, data, cinfo);
298
299                         /* another commit raced with us */
300                         if (pnfs_generic_commit_cancel_empty_pagelist(&pages,
301                                 data, cinfo))
302                                 continue;
303
304                         nfs_init_commit(data, &pages, data->lseg, cinfo);
305                         initiate_commit(data, how);
306                 }
307         }
308 out:
309         return PNFS_ATTEMPTED;
310 }
311 EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
312
313 /*
314  * Data server cache
315  *
316  * Data servers can be mapped to different device ids.
317  * nfs4_pnfs_ds reference counting
318  *   - set to 1 on allocation
319  *   - incremented when a device id maps a data server already in the cache.
320  *   - decremented when deviceid is removed from the cache.
321  */
322 static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
323 static LIST_HEAD(nfs4_data_server_cache);
324
325 /* Debug routines */
326 static void
327 print_ds(struct nfs4_pnfs_ds *ds)
328 {
329         if (ds == NULL) {
330                 printk(KERN_WARNING "%s NULL device\n", __func__);
331                 return;
332         }
333         printk(KERN_WARNING "        ds %s\n"
334                 "        ref count %d\n"
335                 "        client %p\n"
336                 "        cl_exchange_flags %x\n",
337                 ds->ds_remotestr,
338                 refcount_read(&ds->ds_count), ds->ds_clp,
339                 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
340 }
341
342 static bool
343 same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
344 {
345         struct sockaddr_in *a, *b;
346         struct sockaddr_in6 *a6, *b6;
347
348         if (addr1->sa_family != addr2->sa_family)
349                 return false;
350
351         switch (addr1->sa_family) {
352         case AF_INET:
353                 a = (struct sockaddr_in *)addr1;
354                 b = (struct sockaddr_in *)addr2;
355
356                 if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
357                     a->sin_port == b->sin_port)
358                         return true;
359                 break;
360
361         case AF_INET6:
362                 a6 = (struct sockaddr_in6 *)addr1;
363                 b6 = (struct sockaddr_in6 *)addr2;
364
365                 /* LINKLOCAL addresses must have matching scope_id */
366                 if (ipv6_addr_src_scope(&a6->sin6_addr) ==
367                     IPV6_ADDR_SCOPE_LINKLOCAL &&
368                     a6->sin6_scope_id != b6->sin6_scope_id)
369                         return false;
370
371                 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
372                     a6->sin6_port == b6->sin6_port)
373                         return true;
374                 break;
375
376         default:
377                 dprintk("%s: unhandled address family: %u\n",
378                         __func__, addr1->sa_family);
379                 return false;
380         }
381
382         return false;
383 }
384
385 /*
386  * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
387  * declare a match.
388  */
389 static bool
390 _same_data_server_addrs_locked(const struct list_head *dsaddrs1,
391                                const struct list_head *dsaddrs2)
392 {
393         struct nfs4_pnfs_ds_addr *da1, *da2;
394         struct sockaddr *sa1, *sa2;
395         bool match = false;
396
397         list_for_each_entry(da1, dsaddrs1, da_node) {
398                 sa1 = (struct sockaddr *)&da1->da_addr;
399                 match = false;
400                 list_for_each_entry(da2, dsaddrs2, da_node) {
401                         sa2 = (struct sockaddr *)&da2->da_addr;
402                         match = same_sockaddr(sa1, sa2);
403                         if (match)
404                                 break;
405                 }
406                 if (!match)
407                         break;
408         }
409         return match;
410 }
411
412 /*
413  * Lookup DS by addresses.  nfs4_ds_cache_lock is held
414  */
415 static struct nfs4_pnfs_ds *
416 _data_server_lookup_locked(const struct list_head *dsaddrs)
417 {
418         struct nfs4_pnfs_ds *ds;
419
420         list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
421                 if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
422                         return ds;
423         return NULL;
424 }
425
426 static void destroy_ds(struct nfs4_pnfs_ds *ds)
427 {
428         struct nfs4_pnfs_ds_addr *da;
429
430         dprintk("--> %s\n", __func__);
431         ifdebug(FACILITY)
432                 print_ds(ds);
433
434         nfs_put_client(ds->ds_clp);
435
436         while (!list_empty(&ds->ds_addrs)) {
437                 da = list_first_entry(&ds->ds_addrs,
438                                       struct nfs4_pnfs_ds_addr,
439                                       da_node);
440                 list_del_init(&da->da_node);
441                 kfree(da->da_remotestr);
442                 kfree(da);
443         }
444
445         kfree(ds->ds_remotestr);
446         kfree(ds);
447 }
448
449 void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
450 {
451         if (refcount_dec_and_lock(&ds->ds_count,
452                                 &nfs4_ds_cache_lock)) {
453                 list_del_init(&ds->ds_node);
454                 spin_unlock(&nfs4_ds_cache_lock);
455                 destroy_ds(ds);
456         }
457 }
458 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
459
460 /*
461  * Create a string with a human readable address and port to avoid
462  * complicated setup around many dprinks.
463  */
464 static char *
465 nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
466 {
467         struct nfs4_pnfs_ds_addr *da;
468         char *remotestr;
469         size_t len;
470         char *p;
471
472         len = 3;        /* '{', '}' and eol */
473         list_for_each_entry(da, dsaddrs, da_node) {
474                 len += strlen(da->da_remotestr) + 1;    /* string plus comma */
475         }
476
477         remotestr = kzalloc(len, gfp_flags);
478         if (!remotestr)
479                 return NULL;
480
481         p = remotestr;
482         *(p++) = '{';
483         len--;
484         list_for_each_entry(da, dsaddrs, da_node) {
485                 size_t ll = strlen(da->da_remotestr);
486
487                 if (ll > len)
488                         goto out_err;
489
490                 memcpy(p, da->da_remotestr, ll);
491                 p += ll;
492                 len -= ll;
493
494                 if (len < 1)
495                         goto out_err;
496                 (*p++) = ',';
497                 len--;
498         }
499         if (len < 2)
500                 goto out_err;
501         *(p++) = '}';
502         *p = '\0';
503         return remotestr;
504 out_err:
505         kfree(remotestr);
506         return NULL;
507 }
508
509 /*
510  * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
511  * uncached and return cached struct nfs4_pnfs_ds.
512  */
513 struct nfs4_pnfs_ds *
514 nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
515 {
516         struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
517         char *remotestr;
518
519         if (list_empty(dsaddrs)) {
520                 dprintk("%s: no addresses defined\n", __func__);
521                 goto out;
522         }
523
524         ds = kzalloc(sizeof(*ds), gfp_flags);
525         if (!ds)
526                 goto out;
527
528         /* this is only used for debugging, so it's ok if its NULL */
529         remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
530
531         spin_lock(&nfs4_ds_cache_lock);
532         tmp_ds = _data_server_lookup_locked(dsaddrs);
533         if (tmp_ds == NULL) {
534                 INIT_LIST_HEAD(&ds->ds_addrs);
535                 list_splice_init(dsaddrs, &ds->ds_addrs);
536                 ds->ds_remotestr = remotestr;
537                 refcount_set(&ds->ds_count, 1);
538                 INIT_LIST_HEAD(&ds->ds_node);
539                 ds->ds_clp = NULL;
540                 list_add(&ds->ds_node, &nfs4_data_server_cache);
541                 dprintk("%s add new data server %s\n", __func__,
542                         ds->ds_remotestr);
543         } else {
544                 kfree(remotestr);
545                 kfree(ds);
546                 refcount_inc(&tmp_ds->ds_count);
547                 dprintk("%s data server %s found, inc'ed ds_count to %d\n",
548                         __func__, tmp_ds->ds_remotestr,
549                         refcount_read(&tmp_ds->ds_count));
550                 ds = tmp_ds;
551         }
552         spin_unlock(&nfs4_ds_cache_lock);
553 out:
554         return ds;
555 }
556 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
557
558 static int nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
559 {
560         might_sleep();
561         return wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING, TASK_KILLABLE);
562 }
563
564 static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
565 {
566         smp_mb__before_atomic();
567         clear_and_wake_up_bit(NFS4DS_CONNECTING, &ds->ds_state);
568 }
569
570 static struct nfs_client *(*get_v3_ds_connect)(
571                         struct nfs_server *mds_srv,
572                         const struct sockaddr *ds_addr,
573                         int ds_addrlen,
574                         int ds_proto,
575                         unsigned int ds_timeo,
576                         unsigned int ds_retrans);
577
578 static bool load_v3_ds_connect(void)
579 {
580         if (!get_v3_ds_connect) {
581                 get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
582                 WARN_ON_ONCE(!get_v3_ds_connect);
583         }
584
585         return(get_v3_ds_connect != NULL);
586 }
587
588 void nfs4_pnfs_v3_ds_connect_unload(void)
589 {
590         if (get_v3_ds_connect) {
591                 symbol_put(nfs3_set_ds_client);
592                 get_v3_ds_connect = NULL;
593         }
594 }
595
596 static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
597                                  struct nfs4_pnfs_ds *ds,
598                                  unsigned int timeo,
599                                  unsigned int retrans)
600 {
601         struct nfs_client *clp = ERR_PTR(-EIO);
602         struct nfs4_pnfs_ds_addr *da;
603         int status = 0;
604
605         dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
606
607         if (!load_v3_ds_connect())
608                 goto out;
609
610         list_for_each_entry(da, &ds->ds_addrs, da_node) {
611                 dprintk("%s: DS %s: trying address %s\n",
612                         __func__, ds->ds_remotestr, da->da_remotestr);
613
614                 if (!IS_ERR(clp)) {
615                         struct xprt_create xprt_args = {
616                                 .ident = XPRT_TRANSPORT_TCP,
617                                 .net = clp->cl_net,
618                                 .dstaddr = (struct sockaddr *)&da->da_addr,
619                                 .addrlen = da->da_addrlen,
620                                 .servername = clp->cl_hostname,
621                         };
622                         /* Add this address as an alias */
623                         rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
624                                         rpc_clnt_test_and_add_xprt, NULL);
625                 } else
626                         clp = get_v3_ds_connect(mds_srv,
627                                         (struct sockaddr *)&da->da_addr,
628                                         da->da_addrlen, IPPROTO_TCP,
629                                         timeo, retrans);
630         }
631
632         if (IS_ERR(clp)) {
633                 status = PTR_ERR(clp);
634                 goto out;
635         }
636
637         smp_wmb();
638         ds->ds_clp = clp;
639         dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
640 out:
641         return status;
642 }
643
644 static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
645                                  struct nfs4_pnfs_ds *ds,
646                                  unsigned int timeo,
647                                  unsigned int retrans,
648                                  u32 minor_version)
649 {
650         struct nfs_client *clp = ERR_PTR(-EIO);
651         struct nfs4_pnfs_ds_addr *da;
652         int status = 0;
653
654         dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
655
656         list_for_each_entry(da, &ds->ds_addrs, da_node) {
657                 dprintk("%s: DS %s: trying address %s\n",
658                         __func__, ds->ds_remotestr, da->da_remotestr);
659
660                 if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) {
661                         struct xprt_create xprt_args = {
662                                 .ident = XPRT_TRANSPORT_TCP,
663                                 .net = clp->cl_net,
664                                 .dstaddr = (struct sockaddr *)&da->da_addr,
665                                 .addrlen = da->da_addrlen,
666                                 .servername = clp->cl_hostname,
667                         };
668                         struct nfs4_add_xprt_data xprtdata = {
669                                 .clp = clp,
670                                 .cred = nfs4_get_clid_cred(clp),
671                         };
672                         struct rpc_add_xprt_test rpcdata = {
673                                 .add_xprt_test = clp->cl_mvops->session_trunk,
674                                 .data = &xprtdata,
675                         };
676
677                         /**
678                         * Test this address for session trunking and
679                         * add as an alias
680                         */
681                         rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
682                                           rpc_clnt_setup_test_and_add_xprt,
683                                           &rpcdata);
684                         if (xprtdata.cred)
685                                 put_rpccred(xprtdata.cred);
686                 } else {
687                         clp = nfs4_set_ds_client(mds_srv,
688                                                 (struct sockaddr *)&da->da_addr,
689                                                 da->da_addrlen, IPPROTO_TCP,
690                                                 timeo, retrans, minor_version);
691                         if (IS_ERR(clp))
692                                 continue;
693
694                         status = nfs4_init_ds_session(clp,
695                                         mds_srv->nfs_client->cl_lease_time);
696                         if (status) {
697                                 nfs_put_client(clp);
698                                 clp = ERR_PTR(-EIO);
699                                 continue;
700                         }
701
702                 }
703         }
704
705         if (IS_ERR(clp)) {
706                 status = PTR_ERR(clp);
707                 goto out;
708         }
709
710         smp_wmb();
711         ds->ds_clp = clp;
712         dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
713 out:
714         return status;
715 }
716
717 /*
718  * Create an rpc connection to the nfs4_pnfs_ds data server.
719  * Currently only supports IPv4 and IPv6 addresses.
720  * If connection fails, make devid unavailable and return a -errno.
721  */
722 int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
723                           struct nfs4_deviceid_node *devid, unsigned int timeo,
724                           unsigned int retrans, u32 version, u32 minor_version)
725 {
726         int err;
727
728         do {
729                 err = nfs4_wait_ds_connect(ds);
730                 if (err || ds->ds_clp)
731                         goto out;
732                 if (nfs4_test_deviceid_unavailable(devid))
733                         return -ENODEV;
734         } while (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) != 0);
735
736         if (ds->ds_clp)
737                 goto connect_done;
738
739         switch (version) {
740         case 3:
741                 err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo, retrans);
742                 break;
743         case 4:
744                 err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo, retrans,
745                                                minor_version);
746                 break;
747         default:
748                 dprintk("%s: unsupported DS version %d\n", __func__, version);
749                 err = -EPROTONOSUPPORT;
750         }
751
752 connect_done:
753         nfs4_clear_ds_conn_bit(ds);
754 out:
755         /*
756          * At this point the ds->ds_clp should be ready, but it might have
757          * hit an error.
758          */
759         if (!err) {
760                 if (!ds->ds_clp || !nfs_client_init_is_complete(ds->ds_clp)) {
761                         WARN_ON_ONCE(ds->ds_clp ||
762                                 !nfs4_test_deviceid_unavailable(devid));
763                         return -EINVAL;
764                 }
765                 err = nfs_client_init_status(ds->ds_clp);
766         }
767
768         return err;
769 }
770 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
771
772 /*
773  * Currently only supports ipv4, ipv6 and one multi-path address.
774  */
775 struct nfs4_pnfs_ds_addr *
776 nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
777 {
778         struct nfs4_pnfs_ds_addr *da = NULL;
779         char *buf, *portstr;
780         __be16 port;
781         int nlen, rlen;
782         int tmp[2];
783         __be32 *p;
784         char *netid, *match_netid;
785         size_t len, match_netid_len;
786         char *startsep = "";
787         char *endsep = "";
788
789
790         /* r_netid */
791         p = xdr_inline_decode(xdr, 4);
792         if (unlikely(!p))
793                 goto out_err;
794         nlen = be32_to_cpup(p++);
795
796         p = xdr_inline_decode(xdr, nlen);
797         if (unlikely(!p))
798                 goto out_err;
799
800         netid = kmalloc(nlen+1, gfp_flags);
801         if (unlikely(!netid))
802                 goto out_err;
803
804         netid[nlen] = '\0';
805         memcpy(netid, p, nlen);
806
807         /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
808         p = xdr_inline_decode(xdr, 4);
809         if (unlikely(!p))
810                 goto out_free_netid;
811         rlen = be32_to_cpup(p);
812
813         p = xdr_inline_decode(xdr, rlen);
814         if (unlikely(!p))
815                 goto out_free_netid;
816
817         /* port is ".ABC.DEF", 8 chars max */
818         if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
819                 dprintk("%s: Invalid address, length %d\n", __func__,
820                         rlen);
821                 goto out_free_netid;
822         }
823         buf = kmalloc(rlen + 1, gfp_flags);
824         if (!buf) {
825                 dprintk("%s: Not enough memory\n", __func__);
826                 goto out_free_netid;
827         }
828         buf[rlen] = '\0';
829         memcpy(buf, p, rlen);
830
831         /* replace port '.' with '-' */
832         portstr = strrchr(buf, '.');
833         if (!portstr) {
834                 dprintk("%s: Failed finding expected dot in port\n",
835                         __func__);
836                 goto out_free_buf;
837         }
838         *portstr = '-';
839
840         /* find '.' between address and port */
841         portstr = strrchr(buf, '.');
842         if (!portstr) {
843                 dprintk("%s: Failed finding expected dot between address and "
844                         "port\n", __func__);
845                 goto out_free_buf;
846         }
847         *portstr = '\0';
848
849         da = kzalloc(sizeof(*da), gfp_flags);
850         if (unlikely(!da))
851                 goto out_free_buf;
852
853         INIT_LIST_HEAD(&da->da_node);
854
855         if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
856                       sizeof(da->da_addr))) {
857                 dprintk("%s: error parsing address %s\n", __func__, buf);
858                 goto out_free_da;
859         }
860
861         portstr++;
862         sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
863         port = htons((tmp[0] << 8) | (tmp[1]));
864
865         switch (da->da_addr.ss_family) {
866         case AF_INET:
867                 ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
868                 da->da_addrlen = sizeof(struct sockaddr_in);
869                 match_netid = "tcp";
870                 match_netid_len = 3;
871                 break;
872
873         case AF_INET6:
874                 ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
875                 da->da_addrlen = sizeof(struct sockaddr_in6);
876                 match_netid = "tcp6";
877                 match_netid_len = 4;
878                 startsep = "[";
879                 endsep = "]";
880                 break;
881
882         default:
883                 dprintk("%s: unsupported address family: %u\n",
884                         __func__, da->da_addr.ss_family);
885                 goto out_free_da;
886         }
887
888         if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
889                 dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
890                         __func__, netid, match_netid);
891                 goto out_free_da;
892         }
893
894         /* save human readable address */
895         len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
896         da->da_remotestr = kzalloc(len, gfp_flags);
897
898         /* NULL is ok, only used for dprintk */
899         if (da->da_remotestr)
900                 snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
901                          buf, endsep, ntohs(port));
902
903         dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
904         kfree(buf);
905         kfree(netid);
906         return da;
907
908 out_free_da:
909         kfree(da);
910 out_free_buf:
911         dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
912         kfree(buf);
913 out_free_netid:
914         kfree(netid);
915 out_err:
916         return NULL;
917 }
918 EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
919
920 void
921 pnfs_layout_mark_request_commit(struct nfs_page *req,
922                                 struct pnfs_layout_segment *lseg,
923                                 struct nfs_commit_info *cinfo,
924                                 u32 ds_commit_idx)
925 {
926         struct list_head *list;
927         struct pnfs_commit_bucket *buckets;
928
929         mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
930         buckets = cinfo->ds->buckets;
931         list = &buckets[ds_commit_idx].written;
932         if (list_empty(list)) {
933                 if (!pnfs_is_valid_lseg(lseg)) {
934                         mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
935                         cinfo->completion_ops->resched_write(cinfo, req);
936                         return;
937                 }
938                 /* Non-empty buckets hold a reference on the lseg.  That ref
939                  * is normally transferred to the COMMIT call and released
940                  * there.  It could also be released if the last req is pulled
941                  * off due to a rewrite, in which case it will be done in
942                  * pnfs_common_clear_request_commit
943                  */
944                 WARN_ON_ONCE(buckets[ds_commit_idx].wlseg != NULL);
945                 buckets[ds_commit_idx].wlseg = pnfs_get_lseg(lseg);
946         }
947         set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
948         cinfo->ds->nwritten++;
949
950         nfs_request_add_commit_list_locked(req, list, cinfo);
951         mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
952         nfs_mark_page_unstable(req->wb_page, cinfo);
953 }
954 EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
955
956 int
957 pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
958 {
959         int ret;
960
961         if (!pnfs_layoutcommit_outstanding(inode))
962                 return 0;
963         ret = nfs_commit_inode(inode, FLUSH_SYNC);
964         if (ret < 0)
965                 return ret;
966         if (datasync)
967                 return 0;
968         return pnfs_layoutcommit_inode(inode, true);
969 }
970 EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);
971