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