Linux 4.19.133
[linux/fpc-iii.git] / fs / nfs / pnfs_nfs.c
blobacfb52bc0007dcf401be1f82ad6d1f9acc6c7f23
1 /*
2 * Common NFS I/O operations for the pnfs file based
3 * layout drivers.
5 * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
7 * Tom Haynes <loghyr@primarydata.com>
8 */
10 #include <linux/nfs_fs.h>
11 #include <linux/nfs_page.h>
12 #include <linux/sunrpc/addr.h>
13 #include <linux/module.h>
15 #include "nfs4session.h"
16 #include "internal.h"
17 #include "pnfs.h"
19 #define NFSDBG_FACILITY NFSDBG_PNFS
21 void pnfs_generic_rw_release(void *data)
23 struct nfs_pgio_header *hdr = data;
25 nfs_put_client(hdr->ds_clp);
26 hdr->mds_ops->rpc_release(data);
28 EXPORT_SYMBOL_GPL(pnfs_generic_rw_release);
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)
33 struct nfs_writeverf *verf = data->res.verf;
35 data->task.tk_status = 0;
36 memset(&verf->verifier, 0, sizeof(verf->verifier));
37 verf->committed = NFS_UNSTABLE;
39 EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes);
41 void pnfs_generic_write_commit_done(struct rpc_task *task, void *data)
43 struct nfs_commit_data *wdata = data;
45 /* Note this may cause RPC to be resent */
46 wdata->mds_ops->rpc_call_done(task, data);
48 EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done);
50 void pnfs_generic_commit_release(void *calldata)
52 struct nfs_commit_data *data = calldata;
54 data->completion_ops->completion(data);
55 pnfs_put_lseg(data->lseg);
56 nfs_put_client(data->ds_clp);
57 nfs_commitdata_release(data);
59 EXPORT_SYMBOL_GPL(pnfs_generic_commit_release);
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
65 void
66 pnfs_generic_clear_request_commit(struct nfs_page *req,
67 struct nfs_commit_info *cinfo)
69 struct pnfs_layout_segment *freeme = NULL;
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;
77 bucket = list_first_entry(&req->wb_list,
78 struct pnfs_commit_bucket,
79 written);
80 freeme = bucket->wlseg;
81 bucket->wlseg = NULL;
83 out:
84 nfs_request_remove_commit_list(req, cinfo);
85 pnfs_put_lseg(freeme);
87 EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
89 static int
90 pnfs_generic_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
91 struct nfs_commit_info *cinfo,
92 int max)
94 struct list_head *src = &bucket->written;
95 struct list_head *dst = &bucket->committing;
96 int ret;
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;
110 return ret;
113 /* Move reqs from written to committing lists, returning count
114 * of number moved.
116 int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo,
117 int max)
119 int i, rv = 0, cnt;
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;
128 return rv;
130 EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
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)
136 struct pnfs_commit_bucket *b;
137 struct pnfs_layout_segment *freeme;
138 int nwritten;
139 int i;
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;
156 EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
158 static void pnfs_generic_retry_commit(struct nfs_commit_info *cinfo, int idx)
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;
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);
182 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
185 static unsigned int
186 pnfs_generic_alloc_ds_commits(struct nfs_commit_info *cinfo,
187 struct list_head *list)
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;
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++;
208 /* Clean up on error */
209 pnfs_generic_retry_commit(cinfo, i);
210 return nreq;
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)
218 struct pnfs_commit_bucket *bucket;
219 struct list_head *pos;
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);
232 /* Helper function for pnfs_generic_commit_pagelist to catch an empty
233 * page list. This can happen when two commits race.
235 * This must be called instead of nfs_init_commit - call one or the other, but
236 * not both!
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)
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;
254 return false;
257 /* This follows nfs_commit_list pretty closely */
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))
264 struct nfs_commit_data *data, *tmp;
265 LIST_HEAD(list);
266 unsigned int nreq = 0;
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++;
275 nreq += pnfs_generic_alloc_ds_commits(cinfo, &list);
277 if (nreq == 0)
278 goto out;
280 atomic_add(nreq, &cinfo->mds->rpcs_out);
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;
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);
297 pnfs_fetch_commit_bucket_list(&pages, data, cinfo);
299 /* another commit raced with us */
300 if (pnfs_generic_commit_cancel_empty_pagelist(&pages,
301 data, cinfo))
302 continue;
304 nfs_init_commit(data, &pages, data->lseg, cinfo);
305 initiate_commit(data, how);
308 out:
309 return PNFS_ATTEMPTED;
311 EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
314 * Data server cache
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.
322 static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
323 static LIST_HEAD(nfs4_data_server_cache);
325 /* Debug routines */
326 static void
327 print_ds(struct nfs4_pnfs_ds *ds)
329 if (ds == NULL) {
330 printk(KERN_WARNING "%s NULL device\n", __func__);
331 return;
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);
342 static bool
343 same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
345 struct sockaddr_in *a, *b;
346 struct sockaddr_in6 *a6, *b6;
348 if (addr1->sa_family != addr2->sa_family)
349 return false;
351 switch (addr1->sa_family) {
352 case AF_INET:
353 a = (struct sockaddr_in *)addr1;
354 b = (struct sockaddr_in *)addr2;
356 if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
357 a->sin_port == b->sin_port)
358 return true;
359 break;
361 case AF_INET6:
362 a6 = (struct sockaddr_in6 *)addr1;
363 b6 = (struct sockaddr_in6 *)addr2;
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;
371 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
372 a6->sin6_port == b6->sin6_port)
373 return true;
374 break;
376 default:
377 dprintk("%s: unhandled address family: %u\n",
378 __func__, addr1->sa_family);
379 return false;
382 return false;
386 * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
387 * declare a match.
389 static bool
390 _same_data_server_addrs_locked(const struct list_head *dsaddrs1,
391 const struct list_head *dsaddrs2)
393 struct nfs4_pnfs_ds_addr *da1, *da2;
394 struct sockaddr *sa1, *sa2;
395 bool match = false;
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;
406 if (!match)
407 break;
409 return match;
413 * Lookup DS by addresses. nfs4_ds_cache_lock is held
415 static struct nfs4_pnfs_ds *
416 _data_server_lookup_locked(const struct list_head *dsaddrs)
418 struct nfs4_pnfs_ds *ds;
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;
426 static void destroy_ds(struct nfs4_pnfs_ds *ds)
428 struct nfs4_pnfs_ds_addr *da;
430 dprintk("--> %s\n", __func__);
431 ifdebug(FACILITY)
432 print_ds(ds);
434 nfs_put_client(ds->ds_clp);
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);
445 kfree(ds->ds_remotestr);
446 kfree(ds);
449 void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
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);
458 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
461 * Create a string with a human readable address and port to avoid
462 * complicated setup around many dprinks.
464 static char *
465 nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
467 struct nfs4_pnfs_ds_addr *da;
468 char *remotestr;
469 size_t len;
470 char *p;
472 len = 3; /* '{', '}' and eol */
473 list_for_each_entry(da, dsaddrs, da_node) {
474 len += strlen(da->da_remotestr) + 1; /* string plus comma */
477 remotestr = kzalloc(len, gfp_flags);
478 if (!remotestr)
479 return NULL;
481 p = remotestr;
482 *(p++) = '{';
483 len--;
484 list_for_each_entry(da, dsaddrs, da_node) {
485 size_t ll = strlen(da->da_remotestr);
487 if (ll > len)
488 goto out_err;
490 memcpy(p, da->da_remotestr, ll);
491 p += ll;
492 len -= ll;
494 if (len < 1)
495 goto out_err;
496 (*p++) = ',';
497 len--;
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;
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.
513 struct nfs4_pnfs_ds *
514 nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
516 struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
517 char *remotestr;
519 if (list_empty(dsaddrs)) {
520 dprintk("%s: no addresses defined\n", __func__);
521 goto out;
524 ds = kzalloc(sizeof(*ds), gfp_flags);
525 if (!ds)
526 goto out;
528 /* this is only used for debugging, so it's ok if its NULL */
529 remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
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;
552 spin_unlock(&nfs4_ds_cache_lock);
553 out:
554 return ds;
556 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
558 static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
560 might_sleep();
561 wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
562 TASK_KILLABLE);
565 static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
567 smp_mb__before_atomic();
568 clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
569 smp_mb__after_atomic();
570 wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
573 static struct nfs_client *(*get_v3_ds_connect)(
574 struct nfs_server *mds_srv,
575 const struct sockaddr *ds_addr,
576 int ds_addrlen,
577 int ds_proto,
578 unsigned int ds_timeo,
579 unsigned int ds_retrans);
581 static bool load_v3_ds_connect(void)
583 if (!get_v3_ds_connect) {
584 get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
585 WARN_ON_ONCE(!get_v3_ds_connect);
588 return(get_v3_ds_connect != NULL);
591 void nfs4_pnfs_v3_ds_connect_unload(void)
593 if (get_v3_ds_connect) {
594 symbol_put(nfs3_set_ds_client);
595 get_v3_ds_connect = NULL;
599 static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
600 struct nfs4_pnfs_ds *ds,
601 unsigned int timeo,
602 unsigned int retrans)
604 struct nfs_client *clp = ERR_PTR(-EIO);
605 struct nfs4_pnfs_ds_addr *da;
606 int status = 0;
608 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
610 if (!load_v3_ds_connect())
611 goto out;
613 list_for_each_entry(da, &ds->ds_addrs, da_node) {
614 dprintk("%s: DS %s: trying address %s\n",
615 __func__, ds->ds_remotestr, da->da_remotestr);
617 if (!IS_ERR(clp)) {
618 struct xprt_create xprt_args = {
619 .ident = XPRT_TRANSPORT_TCP,
620 .net = clp->cl_net,
621 .dstaddr = (struct sockaddr *)&da->da_addr,
622 .addrlen = da->da_addrlen,
623 .servername = clp->cl_hostname,
625 /* Add this address as an alias */
626 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
627 rpc_clnt_test_and_add_xprt, NULL);
628 } else
629 clp = get_v3_ds_connect(mds_srv,
630 (struct sockaddr *)&da->da_addr,
631 da->da_addrlen, IPPROTO_TCP,
632 timeo, retrans);
635 if (IS_ERR(clp)) {
636 status = PTR_ERR(clp);
637 goto out;
640 smp_wmb();
641 ds->ds_clp = clp;
642 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
643 out:
644 return status;
647 static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
648 struct nfs4_pnfs_ds *ds,
649 unsigned int timeo,
650 unsigned int retrans,
651 u32 minor_version)
653 struct nfs_client *clp = ERR_PTR(-EIO);
654 struct nfs4_pnfs_ds_addr *da;
655 int status = 0;
657 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
659 list_for_each_entry(da, &ds->ds_addrs, da_node) {
660 dprintk("%s: DS %s: trying address %s\n",
661 __func__, ds->ds_remotestr, da->da_remotestr);
663 if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) {
664 struct xprt_create xprt_args = {
665 .ident = XPRT_TRANSPORT_TCP,
666 .net = clp->cl_net,
667 .dstaddr = (struct sockaddr *)&da->da_addr,
668 .addrlen = da->da_addrlen,
669 .servername = clp->cl_hostname,
671 struct nfs4_add_xprt_data xprtdata = {
672 .clp = clp,
673 .cred = nfs4_get_clid_cred(clp),
675 struct rpc_add_xprt_test rpcdata = {
676 .add_xprt_test = clp->cl_mvops->session_trunk,
677 .data = &xprtdata,
681 * Test this address for session trunking and
682 * add as an alias
684 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
685 rpc_clnt_setup_test_and_add_xprt,
686 &rpcdata);
687 if (xprtdata.cred)
688 put_rpccred(xprtdata.cred);
689 } else {
690 clp = nfs4_set_ds_client(mds_srv,
691 (struct sockaddr *)&da->da_addr,
692 da->da_addrlen, IPPROTO_TCP,
693 timeo, retrans, minor_version);
694 if (IS_ERR(clp))
695 continue;
697 status = nfs4_init_ds_session(clp,
698 mds_srv->nfs_client->cl_lease_time);
699 if (status) {
700 nfs_put_client(clp);
701 clp = ERR_PTR(-EIO);
702 continue;
708 if (IS_ERR(clp)) {
709 status = PTR_ERR(clp);
710 goto out;
713 smp_wmb();
714 ds->ds_clp = clp;
715 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
716 out:
717 return status;
721 * Create an rpc connection to the nfs4_pnfs_ds data server.
722 * Currently only supports IPv4 and IPv6 addresses.
723 * If connection fails, make devid unavailable and return a -errno.
725 int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
726 struct nfs4_deviceid_node *devid, unsigned int timeo,
727 unsigned int retrans, u32 version, u32 minor_version)
729 int err;
731 again:
732 err = 0;
733 if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
734 if (version == 3) {
735 err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo,
736 retrans);
737 } else if (version == 4) {
738 err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo,
739 retrans, minor_version);
740 } else {
741 dprintk("%s: unsupported DS version %d\n", __func__,
742 version);
743 err = -EPROTONOSUPPORT;
746 nfs4_clear_ds_conn_bit(ds);
747 } else {
748 nfs4_wait_ds_connect(ds);
750 /* what was waited on didn't connect AND didn't mark unavail */
751 if (!ds->ds_clp && !nfs4_test_deviceid_unavailable(devid))
752 goto again;
756 * At this point the ds->ds_clp should be ready, but it might have
757 * hit an error.
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;
765 err = nfs_client_init_status(ds->ds_clp);
768 return err;
770 EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
773 * Currently only supports ipv4, ipv6 and one multi-path address.
775 struct nfs4_pnfs_ds_addr *
776 nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
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 = "";
790 /* r_netid */
791 p = xdr_inline_decode(xdr, 4);
792 if (unlikely(!p))
793 goto out_err;
794 nlen = be32_to_cpup(p++);
796 p = xdr_inline_decode(xdr, nlen);
797 if (unlikely(!p))
798 goto out_err;
800 netid = kmalloc(nlen+1, gfp_flags);
801 if (unlikely(!netid))
802 goto out_err;
804 netid[nlen] = '\0';
805 memcpy(netid, p, nlen);
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);
813 p = xdr_inline_decode(xdr, rlen);
814 if (unlikely(!p))
815 goto out_free_netid;
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;
823 buf = kmalloc(rlen + 1, gfp_flags);
824 if (!buf) {
825 dprintk("%s: Not enough memory\n", __func__);
826 goto out_free_netid;
828 buf[rlen] = '\0';
829 memcpy(buf, p, rlen);
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;
838 *portstr = '-';
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;
847 *portstr = '\0';
849 da = kzalloc(sizeof(*da), gfp_flags);
850 if (unlikely(!da))
851 goto out_free_buf;
853 INIT_LIST_HEAD(&da->da_node);
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;
861 portstr++;
862 sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
863 port = htons((tmp[0] << 8) | (tmp[1]));
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;
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;
882 default:
883 dprintk("%s: unsupported address family: %u\n",
884 __func__, da->da_addr.ss_family);
885 goto out_free_da;
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;
894 /* save human readable address */
895 len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
896 da->da_remotestr = kzalloc(len, gfp_flags);
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));
903 dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
904 kfree(buf);
905 kfree(netid);
906 return da;
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;
918 EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
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)
926 struct list_head *list;
927 struct pnfs_commit_bucket *buckets;
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;
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
944 WARN_ON_ONCE(buckets[ds_commit_idx].wlseg != NULL);
945 buckets[ds_commit_idx].wlseg = pnfs_get_lseg(lseg);
947 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
948 cinfo->ds->nwritten++;
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);
954 EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
957 pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
959 int ret;
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);
970 EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);