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