Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.5.6.
  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
 21void 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}
 28EXPORT_SYMBOL_GPL(pnfs_generic_rw_release);
 29
 30/* Fake up some data that will cause nfs_commit_release to retry the writes. */
 31void 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}
 40EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes);
 41
 42void 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}
 49EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done);
 50
 51void 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}
 60EXPORT_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 the inode (/cinfo) lock
 65 */
 66void
 67pnfs_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	}
 84out:
 85	nfs_request_remove_commit_list(req, cinfo);
 86	pnfs_put_lseg_locked(freeme);
 87}
 88EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
 89
 90static int
 91pnfs_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->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
113static int
114pnfs_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->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 */
140int 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->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}
154EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
155
156/* Pull everything off the committing lists and dump into @dst.  */
157void 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->lock);
165restart:
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->lock);
172			pnfs_put_lseg(freeme);
173			spin_lock(cinfo->lock);
174			goto restart;
175		}
176	}
177	cinfo->ds->nwritten = 0;
178}
179EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
180
181static 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->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->lock);
198		nfs_retry_commit(&pages, freeme, cinfo, i);
199		pnfs_put_lseg(freeme);
200		spin_lock(cinfo->lock);
201	}
202	spin_unlock(cinfo->lock);
203}
204
205static unsigned int
206pnfs_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
233static inline
234void 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->lock);
242	list_splice_init(&bucket->committing, pages);
243	data->lseg = bucket->clseg;
244	bucket->clseg = NULL;
245	spin_unlock(cinfo->lock);
246
247}
248
249/* This follows nfs_commit_list pretty closely */
250int
251pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
252			     int how, struct nfs_commit_info *cinfo,
253			     int (*initiate_commit)(struct nfs_commit_data *data,
254						    int how))
255{
256	struct nfs_commit_data *data, *tmp;
257	LIST_HEAD(list);
258	unsigned int nreq = 0;
259
260	if (!list_empty(mds_pages)) {
261		data = nfs_commitdata_alloc();
262		if (data != NULL) {
263			data->ds_commit_index = -1;
264			list_add(&data->pages, &list);
265			nreq++;
266		} else {
267			nfs_retry_commit(mds_pages, NULL, cinfo, 0);
268			pnfs_generic_retry_commit(cinfo, 0);
269			return -ENOMEM;
270		}
271	}
272
273	nreq += pnfs_generic_alloc_ds_commits(cinfo, &list);
274
275	if (nreq == 0)
276		goto out;
277
278	atomic_add(nreq, &cinfo->mds->rpcs_out);
279
280	list_for_each_entry_safe(data, tmp, &list, pages) {
281		list_del_init(&data->pages);
282		if (data->ds_commit_index < 0) {
283			nfs_init_commit(data, mds_pages, NULL, cinfo);
284			nfs_initiate_commit(NFS_CLIENT(inode), data,
285					    NFS_PROTO(data->inode),
286					    data->mds_ops, how, 0);
287		} else {
288			LIST_HEAD(pages);
289
290			pnfs_fetch_commit_bucket_list(&pages, data, cinfo);
291			nfs_init_commit(data, &pages, data->lseg, cinfo);
292			initiate_commit(data, how);
293		}
294	}
295out:
296	cinfo->ds->ncommitting = 0;
297	return PNFS_ATTEMPTED;
298}
299EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
300
301/*
302 * Data server cache
303 *
304 * Data servers can be mapped to different device ids.
305 * nfs4_pnfs_ds reference counting
306 *   - set to 1 on allocation
307 *   - incremented when a device id maps a data server already in the cache.
308 *   - decremented when deviceid is removed from the cache.
309 */
310static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
311static LIST_HEAD(nfs4_data_server_cache);
312
313/* Debug routines */
314static void
315print_ds(struct nfs4_pnfs_ds *ds)
316{
317	if (ds == NULL) {
318		printk(KERN_WARNING "%s NULL device\n", __func__);
319		return;
320	}
321	printk(KERN_WARNING "        ds %s\n"
322		"        ref count %d\n"
323		"        client %p\n"
324		"        cl_exchange_flags %x\n",
325		ds->ds_remotestr,
326		atomic_read(&ds->ds_count), ds->ds_clp,
327		ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
328}
329
330static bool
331same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
332{
333	struct sockaddr_in *a, *b;
334	struct sockaddr_in6 *a6, *b6;
335
336	if (addr1->sa_family != addr2->sa_family)
337		return false;
338
339	switch (addr1->sa_family) {
340	case AF_INET:
341		a = (struct sockaddr_in *)addr1;
342		b = (struct sockaddr_in *)addr2;
343
344		if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
345		    a->sin_port == b->sin_port)
346			return true;
347		break;
348
349	case AF_INET6:
350		a6 = (struct sockaddr_in6 *)addr1;
351		b6 = (struct sockaddr_in6 *)addr2;
352
353		/* LINKLOCAL addresses must have matching scope_id */
354		if (ipv6_addr_src_scope(&a6->sin6_addr) ==
355		    IPV6_ADDR_SCOPE_LINKLOCAL &&
356		    a6->sin6_scope_id != b6->sin6_scope_id)
357			return false;
358
359		if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
360		    a6->sin6_port == b6->sin6_port)
361			return true;
362		break;
363
364	default:
365		dprintk("%s: unhandled address family: %u\n",
366			__func__, addr1->sa_family);
367		return false;
368	}
369
370	return false;
371}
372
373/*
374 * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
375 * declare a match.
376 */
377static bool
378_same_data_server_addrs_locked(const struct list_head *dsaddrs1,
379			       const struct list_head *dsaddrs2)
380{
381	struct nfs4_pnfs_ds_addr *da1, *da2;
382	struct sockaddr *sa1, *sa2;
383	bool match = false;
384
385	list_for_each_entry(da1, dsaddrs1, da_node) {
386		sa1 = (struct sockaddr *)&da1->da_addr;
387		match = false;
388		list_for_each_entry(da2, dsaddrs2, da_node) {
389			sa2 = (struct sockaddr *)&da2->da_addr;
390			match = same_sockaddr(sa1, sa2);
391			if (match)
392				break;
393		}
394		if (!match)
395			break;
396	}
397	return match;
398}
399
400/*
401 * Lookup DS by addresses.  nfs4_ds_cache_lock is held
402 */
403static struct nfs4_pnfs_ds *
404_data_server_lookup_locked(const struct list_head *dsaddrs)
405{
406	struct nfs4_pnfs_ds *ds;
407
408	list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
409		if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
410			return ds;
411	return NULL;
412}
413
414static void destroy_ds(struct nfs4_pnfs_ds *ds)
415{
416	struct nfs4_pnfs_ds_addr *da;
417
418	dprintk("--> %s\n", __func__);
419	ifdebug(FACILITY)
420		print_ds(ds);
421
422	nfs_put_client(ds->ds_clp);
423
424	while (!list_empty(&ds->ds_addrs)) {
425		da = list_first_entry(&ds->ds_addrs,
426				      struct nfs4_pnfs_ds_addr,
427				      da_node);
428		list_del_init(&da->da_node);
429		kfree(da->da_remotestr);
430		kfree(da);
431	}
432
433	kfree(ds->ds_remotestr);
434	kfree(ds);
435}
436
437void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
438{
439	if (atomic_dec_and_lock(&ds->ds_count,
440				&nfs4_ds_cache_lock)) {
441		list_del_init(&ds->ds_node);
442		spin_unlock(&nfs4_ds_cache_lock);
443		destroy_ds(ds);
444	}
445}
446EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
447
448/*
449 * Create a string with a human readable address and port to avoid
450 * complicated setup around many dprinks.
451 */
452static char *
453nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
454{
455	struct nfs4_pnfs_ds_addr *da;
456	char *remotestr;
457	size_t len;
458	char *p;
459
460	len = 3;        /* '{', '}' and eol */
461	list_for_each_entry(da, dsaddrs, da_node) {
462		len += strlen(da->da_remotestr) + 1;    /* string plus comma */
463	}
464
465	remotestr = kzalloc(len, gfp_flags);
466	if (!remotestr)
467		return NULL;
468
469	p = remotestr;
470	*(p++) = '{';
471	len--;
472	list_for_each_entry(da, dsaddrs, da_node) {
473		size_t ll = strlen(da->da_remotestr);
474
475		if (ll > len)
476			goto out_err;
477
478		memcpy(p, da->da_remotestr, ll);
479		p += ll;
480		len -= ll;
481
482		if (len < 1)
483			goto out_err;
484		(*p++) = ',';
485		len--;
486	}
487	if (len < 2)
488		goto out_err;
489	*(p++) = '}';
490	*p = '\0';
491	return remotestr;
492out_err:
493	kfree(remotestr);
494	return NULL;
495}
496
497/*
498 * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
499 * uncached and return cached struct nfs4_pnfs_ds.
500 */
501struct nfs4_pnfs_ds *
502nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
503{
504	struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
505	char *remotestr;
506
507	if (list_empty(dsaddrs)) {
508		dprintk("%s: no addresses defined\n", __func__);
509		goto out;
510	}
511
512	ds = kzalloc(sizeof(*ds), gfp_flags);
513	if (!ds)
514		goto out;
515
516	/* this is only used for debugging, so it's ok if its NULL */
517	remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
518
519	spin_lock(&nfs4_ds_cache_lock);
520	tmp_ds = _data_server_lookup_locked(dsaddrs);
521	if (tmp_ds == NULL) {
522		INIT_LIST_HEAD(&ds->ds_addrs);
523		list_splice_init(dsaddrs, &ds->ds_addrs);
524		ds->ds_remotestr = remotestr;
525		atomic_set(&ds->ds_count, 1);
526		INIT_LIST_HEAD(&ds->ds_node);
527		ds->ds_clp = NULL;
528		list_add(&ds->ds_node, &nfs4_data_server_cache);
529		dprintk("%s add new data server %s\n", __func__,
530			ds->ds_remotestr);
531	} else {
532		kfree(remotestr);
533		kfree(ds);
534		atomic_inc(&tmp_ds->ds_count);
535		dprintk("%s data server %s found, inc'ed ds_count to %d\n",
536			__func__, tmp_ds->ds_remotestr,
537			atomic_read(&tmp_ds->ds_count));
538		ds = tmp_ds;
539	}
540	spin_unlock(&nfs4_ds_cache_lock);
541out:
542	return ds;
543}
544EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
545
546static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
547{
548	might_sleep();
549	wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
550			TASK_KILLABLE);
551}
552
553static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
554{
555	smp_mb__before_atomic();
556	clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
557	smp_mb__after_atomic();
558	wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
559}
560
561static struct nfs_client *(*get_v3_ds_connect)(
562			struct nfs_client *mds_clp,
563			const struct sockaddr *ds_addr,
564			int ds_addrlen,
565			int ds_proto,
566			unsigned int ds_timeo,
567			unsigned int ds_retrans,
568			rpc_authflavor_t au_flavor);
569
570static bool load_v3_ds_connect(void)
571{
572	if (!get_v3_ds_connect) {
573		get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
574		WARN_ON_ONCE(!get_v3_ds_connect);
575	}
576
577	return(get_v3_ds_connect != NULL);
578}
579
580void nfs4_pnfs_v3_ds_connect_unload(void)
581{
582	if (get_v3_ds_connect) {
583		symbol_put(nfs3_set_ds_client);
584		get_v3_ds_connect = NULL;
585	}
586}
587EXPORT_SYMBOL_GPL(nfs4_pnfs_v3_ds_connect_unload);
588
589static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
590				 struct nfs4_pnfs_ds *ds,
591				 unsigned int timeo,
592				 unsigned int retrans,
593				 rpc_authflavor_t au_flavor)
594{
595	struct nfs_client *clp = ERR_PTR(-EIO);
596	struct nfs4_pnfs_ds_addr *da;
597	int status = 0;
598
599	dprintk("--> %s DS %s au_flavor %d\n", __func__,
600		ds->ds_remotestr, au_flavor);
601
602	if (!load_v3_ds_connect())
603		goto out;
604
605	list_for_each_entry(da, &ds->ds_addrs, da_node) {
606		dprintk("%s: DS %s: trying address %s\n",
607			__func__, ds->ds_remotestr, da->da_remotestr);
608
609		if (!IS_ERR(clp)) {
610			struct xprt_create xprt_args = {
611				.ident = XPRT_TRANSPORT_TCP,
612				.net = clp->cl_net,
613				.dstaddr = (struct sockaddr *)&da->da_addr,
614				.addrlen = da->da_addrlen,
615				.servername = clp->cl_hostname,
616			};
617			/* Add this address as an alias */
618			rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
619					rpc_clnt_test_and_add_xprt, NULL);
620		} else
621			clp = get_v3_ds_connect(mds_srv->nfs_client,
622					(struct sockaddr *)&da->da_addr,
623					da->da_addrlen, IPPROTO_TCP,
624					timeo, retrans, au_flavor);
625	}
626
627	if (IS_ERR(clp)) {
628		status = PTR_ERR(clp);
629		goto out;
630	}
631
632	smp_wmb();
633	ds->ds_clp = clp;
634	dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
635out:
636	return status;
637}
638
639static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
640				 struct nfs4_pnfs_ds *ds,
641				 unsigned int timeo,
642				 unsigned int retrans,
643				 u32 minor_version,
644				 rpc_authflavor_t au_flavor)
645{
646	struct nfs_client *clp = ERR_PTR(-EIO);
647	struct nfs4_pnfs_ds_addr *da;
648	int status = 0;
649
650	dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr,
651		au_flavor);
652
653	list_for_each_entry(da, &ds->ds_addrs, da_node) {
654		dprintk("%s: DS %s: trying address %s\n",
655			__func__, ds->ds_remotestr, da->da_remotestr);
656
657		clp = nfs4_set_ds_client(mds_srv->nfs_client,
658					(struct sockaddr *)&da->da_addr,
659					da->da_addrlen, IPPROTO_TCP,
660					timeo, retrans, minor_version,
661					au_flavor);
662		if (!IS_ERR(clp))
663			break;
664	}
665
666	if (IS_ERR(clp)) {
667		status = PTR_ERR(clp);
668		goto out;
669	}
670
671	status = nfs4_init_ds_session(clp, mds_srv->nfs_client->cl_lease_time);
672	if (status)
673		goto out_put;
674
675	smp_wmb();
676	ds->ds_clp = clp;
677	dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
678out:
679	return status;
680out_put:
681	nfs_put_client(clp);
682	goto out;
683}
684
685/*
686 * Create an rpc connection to the nfs4_pnfs_ds data server.
687 * Currently only supports IPv4 and IPv6 addresses.
688 * If connection fails, make devid unavailable.
689 */
690void nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
691			  struct nfs4_deviceid_node *devid, unsigned int timeo,
692			  unsigned int retrans, u32 version,
693			  u32 minor_version, rpc_authflavor_t au_flavor)
694{
695	if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
696		int err = 0;
697
698		if (version == 3) {
699			err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo,
700						       retrans, au_flavor);
701		} else if (version == 4) {
702			err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo,
703						       retrans, minor_version,
704						       au_flavor);
705		} else {
706			dprintk("%s: unsupported DS version %d\n", __func__,
707				version);
708			err = -EPROTONOSUPPORT;
709		}
710
711		if (err)
712			nfs4_mark_deviceid_unavailable(devid);
713		nfs4_clear_ds_conn_bit(ds);
714	} else {
715		nfs4_wait_ds_connect(ds);
716	}
717}
718EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
719
720/*
721 * Currently only supports ipv4, ipv6 and one multi-path address.
722 */
723struct nfs4_pnfs_ds_addr *
724nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
725{
726	struct nfs4_pnfs_ds_addr *da = NULL;
727	char *buf, *portstr;
728	__be16 port;
729	int nlen, rlen;
730	int tmp[2];
731	__be32 *p;
732	char *netid, *match_netid;
733	size_t len, match_netid_len;
734	char *startsep = "";
735	char *endsep = "";
736
737
738	/* r_netid */
739	p = xdr_inline_decode(xdr, 4);
740	if (unlikely(!p))
741		goto out_err;
742	nlen = be32_to_cpup(p++);
743
744	p = xdr_inline_decode(xdr, nlen);
745	if (unlikely(!p))
746		goto out_err;
747
748	netid = kmalloc(nlen+1, gfp_flags);
749	if (unlikely(!netid))
750		goto out_err;
751
752	netid[nlen] = '\0';
753	memcpy(netid, p, nlen);
754
755	/* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
756	p = xdr_inline_decode(xdr, 4);
757	if (unlikely(!p))
758		goto out_free_netid;
759	rlen = be32_to_cpup(p);
760
761	p = xdr_inline_decode(xdr, rlen);
762	if (unlikely(!p))
763		goto out_free_netid;
764
765	/* port is ".ABC.DEF", 8 chars max */
766	if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
767		dprintk("%s: Invalid address, length %d\n", __func__,
768			rlen);
769		goto out_free_netid;
770	}
771	buf = kmalloc(rlen + 1, gfp_flags);
772	if (!buf) {
773		dprintk("%s: Not enough memory\n", __func__);
774		goto out_free_netid;
775	}
776	buf[rlen] = '\0';
777	memcpy(buf, p, rlen);
778
779	/* replace port '.' with '-' */
780	portstr = strrchr(buf, '.');
781	if (!portstr) {
782		dprintk("%s: Failed finding expected dot in port\n",
783			__func__);
784		goto out_free_buf;
785	}
786	*portstr = '-';
787
788	/* find '.' between address and port */
789	portstr = strrchr(buf, '.');
790	if (!portstr) {
791		dprintk("%s: Failed finding expected dot between address and "
792			"port\n", __func__);
793		goto out_free_buf;
794	}
795	*portstr = '\0';
796
797	da = kzalloc(sizeof(*da), gfp_flags);
798	if (unlikely(!da))
799		goto out_free_buf;
800
801	INIT_LIST_HEAD(&da->da_node);
802
803	if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
804		      sizeof(da->da_addr))) {
805		dprintk("%s: error parsing address %s\n", __func__, buf);
806		goto out_free_da;
807	}
808
809	portstr++;
810	sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
811	port = htons((tmp[0] << 8) | (tmp[1]));
812
813	switch (da->da_addr.ss_family) {
814	case AF_INET:
815		((struct sockaddr_in *)&da->da_addr)->sin_port = port;
816		da->da_addrlen = sizeof(struct sockaddr_in);
817		match_netid = "tcp";
818		match_netid_len = 3;
819		break;
820
821	case AF_INET6:
822		((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
823		da->da_addrlen = sizeof(struct sockaddr_in6);
824		match_netid = "tcp6";
825		match_netid_len = 4;
826		startsep = "[";
827		endsep = "]";
828		break;
829
830	default:
831		dprintk("%s: unsupported address family: %u\n",
832			__func__, da->da_addr.ss_family);
833		goto out_free_da;
834	}
835
836	if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
837		dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
838			__func__, netid, match_netid);
839		goto out_free_da;
840	}
841
842	/* save human readable address */
843	len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
844	da->da_remotestr = kzalloc(len, gfp_flags);
845
846	/* NULL is ok, only used for dprintk */
847	if (da->da_remotestr)
848		snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
849			 buf, endsep, ntohs(port));
850
851	dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
852	kfree(buf);
853	kfree(netid);
854	return da;
855
856out_free_da:
857	kfree(da);
858out_free_buf:
859	dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
860	kfree(buf);
861out_free_netid:
862	kfree(netid);
863out_err:
864	return NULL;
865}
866EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
867
868void
869pnfs_layout_mark_request_commit(struct nfs_page *req,
870				struct pnfs_layout_segment *lseg,
871				struct nfs_commit_info *cinfo,
872				u32 ds_commit_idx)
873{
874	struct list_head *list;
875	struct pnfs_commit_bucket *buckets;
876
877	spin_lock(cinfo->lock);
878	buckets = cinfo->ds->buckets;
879	list = &buckets[ds_commit_idx].written;
880	if (list_empty(list)) {
881		if (!pnfs_is_valid_lseg(lseg)) {
882			spin_unlock(cinfo->lock);
883			cinfo->completion_ops->resched_write(cinfo, req);
884			return;
885		}
886		/* Non-empty buckets hold a reference on the lseg.  That ref
887		 * is normally transferred to the COMMIT call and released
888		 * there.  It could also be released if the last req is pulled
889		 * off due to a rewrite, in which case it will be done in
890		 * pnfs_common_clear_request_commit
891		 */
892		WARN_ON_ONCE(buckets[ds_commit_idx].wlseg != NULL);
893		buckets[ds_commit_idx].wlseg = pnfs_get_lseg(lseg);
894	}
895	set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
896	cinfo->ds->nwritten++;
897
898	nfs_request_add_commit_list_locked(req, list, cinfo);
899	spin_unlock(cinfo->lock);
900	nfs_mark_page_unstable(req->wb_page, cinfo);
901}
902EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
903
904int
905pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
906{
907	if (datasync)
908		return 0;
909	return pnfs_layoutcommit_inode(inode, true);
910}
911EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);
912