xref: /openbmc/linux/fs/nfs/flexfilelayout/flexfilelayoutdev.c (revision cb067935175ca477380806dc80bf5f0bb51f6f71)
1d67ae825STom Haynes /*
2d67ae825STom Haynes  * Device operations for the pnfs nfs4 file layout driver.
3d67ae825STom Haynes  *
4d67ae825STom Haynes  * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
5d67ae825STom Haynes  *
6d67ae825STom Haynes  * Tao Peng <bergwolf@primarydata.com>
7d67ae825STom Haynes  */
8d67ae825STom Haynes 
9d67ae825STom Haynes #include <linux/nfs_fs.h>
10d67ae825STom Haynes #include <linux/vmalloc.h>
11d67ae825STom Haynes #include <linux/module.h>
12d67ae825STom Haynes #include <linux/sunrpc/addr.h>
13d67ae825STom Haynes 
14d67ae825STom Haynes #include "../internal.h"
15d67ae825STom Haynes #include "../nfs4session.h"
16d67ae825STom Haynes #include "flexfilelayout.h"
17d67ae825STom Haynes 
18d67ae825STom Haynes #define NFSDBG_FACILITY		NFSDBG_PNFS_LD
19d67ae825STom Haynes 
2015d03055STrond Myklebust static unsigned int dataserver_timeo = NFS_DEF_TCP_RETRANS;
2115d03055STrond Myklebust static unsigned int dataserver_retrans;
22d67ae825STom Haynes 
23d67ae825STom Haynes void nfs4_ff_layout_put_deviceid(struct nfs4_ff_layout_ds *mirror_ds)
24d67ae825STom Haynes {
25d67ae825STom Haynes 	if (mirror_ds)
26d67ae825STom Haynes 		nfs4_put_deviceid_node(&mirror_ds->id_node);
27d67ae825STom Haynes }
28d67ae825STom Haynes 
29d67ae825STom Haynes void nfs4_ff_layout_free_deviceid(struct nfs4_ff_layout_ds *mirror_ds)
30d67ae825STom Haynes {
31d67ae825STom Haynes 	nfs4_print_deviceid(&mirror_ds->id_node.deviceid);
32d67ae825STom Haynes 	nfs4_pnfs_ds_put(mirror_ds->ds);
3384a80f62STrond Myklebust 	kfree_rcu(mirror_ds, id_node.rcu);
34d67ae825STom Haynes }
35d67ae825STom Haynes 
36d67ae825STom Haynes /* Decode opaque device data and construct new_ds using it */
37d67ae825STom Haynes struct nfs4_ff_layout_ds *
38d67ae825STom Haynes nfs4_ff_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
39d67ae825STom Haynes 			    gfp_t gfp_flags)
40d67ae825STom Haynes {
41d67ae825STom Haynes 	struct xdr_stream stream;
42d67ae825STom Haynes 	struct xdr_buf buf;
43d67ae825STom Haynes 	struct page *scratch;
44d67ae825STom Haynes 	struct list_head dsaddrs;
45d67ae825STom Haynes 	struct nfs4_pnfs_ds_addr *da;
46d67ae825STom Haynes 	struct nfs4_ff_layout_ds *new_ds = NULL;
47d67ae825STom Haynes 	struct nfs4_ff_ds_version *ds_versions = NULL;
48d67ae825STom Haynes 	u32 mp_count;
49d67ae825STom Haynes 	u32 version_count;
50d67ae825STom Haynes 	__be32 *p;
51d67ae825STom Haynes 	int i, ret = -ENOMEM;
52d67ae825STom Haynes 
53d67ae825STom Haynes 	/* set up xdr stream */
54d67ae825STom Haynes 	scratch = alloc_page(gfp_flags);
55d67ae825STom Haynes 	if (!scratch)
56d67ae825STom Haynes 		goto out_err;
57d67ae825STom Haynes 
58d67ae825STom Haynes 	new_ds = kzalloc(sizeof(struct nfs4_ff_layout_ds), gfp_flags);
59d67ae825STom Haynes 	if (!new_ds)
60d67ae825STom Haynes 		goto out_scratch;
61d67ae825STom Haynes 
62d67ae825STom Haynes 	nfs4_init_deviceid_node(&new_ds->id_node,
63d67ae825STom Haynes 				server,
64d67ae825STom Haynes 				&pdev->dev_id);
65d67ae825STom Haynes 	INIT_LIST_HEAD(&dsaddrs);
66d67ae825STom Haynes 
67d67ae825STom Haynes 	xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
68d67ae825STom Haynes 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
69d67ae825STom Haynes 
70d67ae825STom Haynes 	/* multipath count */
71d67ae825STom Haynes 	p = xdr_inline_decode(&stream, 4);
72d67ae825STom Haynes 	if (unlikely(!p))
73d67ae825STom Haynes 		goto out_err_drain_dsaddrs;
74d67ae825STom Haynes 	mp_count = be32_to_cpup(p);
75d67ae825STom Haynes 	dprintk("%s: multipath ds count %d\n", __func__, mp_count);
76d67ae825STom Haynes 
77d67ae825STom Haynes 	for (i = 0; i < mp_count; i++) {
78d67ae825STom Haynes 		/* multipath ds */
79d67ae825STom Haynes 		da = nfs4_decode_mp_ds_addr(server->nfs_client->cl_net,
80d67ae825STom Haynes 					    &stream, gfp_flags);
81d67ae825STom Haynes 		if (da)
82d67ae825STom Haynes 			list_add_tail(&da->da_node, &dsaddrs);
83d67ae825STom Haynes 	}
84d67ae825STom Haynes 	if (list_empty(&dsaddrs)) {
85d67ae825STom Haynes 		dprintk("%s: no suitable DS addresses found\n",
86d67ae825STom Haynes 			__func__);
87d67ae825STom Haynes 		ret = -ENOMEDIUM;
88d67ae825STom Haynes 		goto out_err_drain_dsaddrs;
89d67ae825STom Haynes 	}
90d67ae825STom Haynes 
91d67ae825STom Haynes 	/* version count */
92d67ae825STom Haynes 	p = xdr_inline_decode(&stream, 4);
93d67ae825STom Haynes 	if (unlikely(!p))
94d67ae825STom Haynes 		goto out_err_drain_dsaddrs;
95d67ae825STom Haynes 	version_count = be32_to_cpup(p);
96d67ae825STom Haynes 	dprintk("%s: version count %d\n", __func__, version_count);
97d67ae825STom Haynes 
98d67ae825STom Haynes 	ds_versions = kzalloc(version_count * sizeof(struct nfs4_ff_ds_version),
99d67ae825STom Haynes 			      gfp_flags);
100d67ae825STom Haynes 	if (!ds_versions)
101d67ae825STom Haynes 		goto out_scratch;
102d67ae825STom Haynes 
103d67ae825STom Haynes 	for (i = 0; i < version_count; i++) {
104d67ae825STom Haynes 		/* 20 = version(4) + minor_version(4) + rsize(4) + wsize(4) +
105d67ae825STom Haynes 		 * tightly_coupled(4) */
106d67ae825STom Haynes 		p = xdr_inline_decode(&stream, 20);
107d67ae825STom Haynes 		if (unlikely(!p))
108d67ae825STom Haynes 			goto out_err_drain_dsaddrs;
109d67ae825STom Haynes 		ds_versions[i].version = be32_to_cpup(p++);
110d67ae825STom Haynes 		ds_versions[i].minor_version = be32_to_cpup(p++);
111d67ae825STom Haynes 		ds_versions[i].rsize = nfs_block_size(be32_to_cpup(p++), NULL);
112d67ae825STom Haynes 		ds_versions[i].wsize = nfs_block_size(be32_to_cpup(p++), NULL);
113d67ae825STom Haynes 		ds_versions[i].tightly_coupled = be32_to_cpup(p);
114d67ae825STom Haynes 
115d67ae825STom Haynes 		if (ds_versions[i].rsize > NFS_MAX_FILE_IO_SIZE)
116d67ae825STom Haynes 			ds_versions[i].rsize = NFS_MAX_FILE_IO_SIZE;
117d67ae825STom Haynes 		if (ds_versions[i].wsize > NFS_MAX_FILE_IO_SIZE)
118d67ae825STom Haynes 			ds_versions[i].wsize = NFS_MAX_FILE_IO_SIZE;
119d67ae825STom Haynes 
120d67ae825STom Haynes 		if (ds_versions[i].version != 3 || ds_versions[i].minor_version != 0) {
121d67ae825STom Haynes 			dprintk("%s: [%d] unsupported ds version %d-%d\n", __func__,
122d67ae825STom Haynes 				i, ds_versions[i].version,
123d67ae825STom Haynes 				ds_versions[i].minor_version);
124d67ae825STom Haynes 			ret = -EPROTONOSUPPORT;
125d67ae825STom Haynes 			goto out_err_drain_dsaddrs;
126d67ae825STom Haynes 		}
127d67ae825STom Haynes 
128d67ae825STom Haynes 		dprintk("%s: [%d] vers %u minor_ver %u rsize %u wsize %u coupled %d\n",
129d67ae825STom Haynes 			__func__, i, ds_versions[i].version,
130d67ae825STom Haynes 			ds_versions[i].minor_version,
131d67ae825STom Haynes 			ds_versions[i].rsize,
132d67ae825STom Haynes 			ds_versions[i].wsize,
133d67ae825STom Haynes 			ds_versions[i].tightly_coupled);
134d67ae825STom Haynes 	}
135d67ae825STom Haynes 
136d67ae825STom Haynes 	new_ds->ds_versions = ds_versions;
137d67ae825STom Haynes 	new_ds->ds_versions_cnt = version_count;
138d67ae825STom Haynes 
139d67ae825STom Haynes 	new_ds->ds = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
140d67ae825STom Haynes 	if (!new_ds->ds)
141d67ae825STom Haynes 		goto out_err_drain_dsaddrs;
142d67ae825STom Haynes 
143d67ae825STom Haynes 	/* If DS was already in cache, free ds addrs */
144d67ae825STom Haynes 	while (!list_empty(&dsaddrs)) {
145d67ae825STom Haynes 		da = list_first_entry(&dsaddrs,
146d67ae825STom Haynes 				      struct nfs4_pnfs_ds_addr,
147d67ae825STom Haynes 				      da_node);
148d67ae825STom Haynes 		list_del_init(&da->da_node);
149d67ae825STom Haynes 		kfree(da->da_remotestr);
150d67ae825STom Haynes 		kfree(da);
151d67ae825STom Haynes 	}
152d67ae825STom Haynes 
153d67ae825STom Haynes 	__free_page(scratch);
154d67ae825STom Haynes 	return new_ds;
155d67ae825STom Haynes 
156d67ae825STom Haynes out_err_drain_dsaddrs:
157d67ae825STom Haynes 	while (!list_empty(&dsaddrs)) {
158d67ae825STom Haynes 		da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
159d67ae825STom Haynes 				      da_node);
160d67ae825STom Haynes 		list_del_init(&da->da_node);
161d67ae825STom Haynes 		kfree(da->da_remotestr);
162d67ae825STom Haynes 		kfree(da);
163d67ae825STom Haynes 	}
164d67ae825STom Haynes 
165d67ae825STom Haynes 	kfree(ds_versions);
166d67ae825STom Haynes out_scratch:
167d67ae825STom Haynes 	__free_page(scratch);
168d67ae825STom Haynes out_err:
169d67ae825STom Haynes 	kfree(new_ds);
170d67ae825STom Haynes 
171d67ae825STom Haynes 	dprintk("%s ERROR: returning %d\n", __func__, ret);
172d67ae825STom Haynes 	return NULL;
173d67ae825STom Haynes }
174d67ae825STom Haynes 
175889d94d4STrond Myklebust static void ff_layout_mark_devid_invalid(struct pnfs_layout_segment *lseg,
176889d94d4STrond Myklebust 		struct nfs4_deviceid_node *devid)
177889d94d4STrond Myklebust {
178889d94d4STrond Myklebust 	nfs4_mark_deviceid_unavailable(devid);
179889d94d4STrond Myklebust 	if (!ff_layout_has_available_ds(lseg))
180889d94d4STrond Myklebust 		pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode,
181889d94d4STrond Myklebust 				lseg);
182889d94d4STrond Myklebust }
183889d94d4STrond Myklebust 
184889d94d4STrond Myklebust static bool ff_layout_mirror_valid(struct pnfs_layout_segment *lseg,
185889d94d4STrond Myklebust 		struct nfs4_ff_layout_mirror *mirror)
186889d94d4STrond Myklebust {
187889d94d4STrond Myklebust 	if (mirror == NULL || mirror->mirror_ds == NULL) {
188889d94d4STrond Myklebust 		pnfs_error_mark_layout_for_return(lseg->pls_layout->plh_inode,
189889d94d4STrond Myklebust 					lseg);
190889d94d4STrond Myklebust 		return false;
191889d94d4STrond Myklebust 	}
192889d94d4STrond Myklebust 	if (mirror->mirror_ds->ds == NULL) {
193889d94d4STrond Myklebust 		struct nfs4_deviceid_node *devid;
194889d94d4STrond Myklebust 		devid = &mirror->mirror_ds->id_node;
195889d94d4STrond Myklebust 		ff_layout_mark_devid_invalid(lseg, devid);
196889d94d4STrond Myklebust 		return false;
197889d94d4STrond Myklebust 	}
198889d94d4STrond Myklebust 	return true;
199889d94d4STrond Myklebust }
200889d94d4STrond Myklebust 
201d67ae825STom Haynes static void extend_ds_error(struct nfs4_ff_layout_ds_err *err,
202d67ae825STom Haynes 			    u64 offset, u64 length)
203d67ae825STom Haynes {
204d67ae825STom Haynes 	u64 end;
205d67ae825STom Haynes 
20617822b20STrond Myklebust 	end = max_t(u64, pnfs_end_offset(err->offset, err->length),
20717822b20STrond Myklebust 		    pnfs_end_offset(offset, length));
208d67ae825STom Haynes 	err->offset = min_t(u64, err->offset, offset);
209d67ae825STom Haynes 	err->length = end - err->offset;
210d67ae825STom Haynes }
211d67ae825STom Haynes 
212b819ed4bSTrond Myklebust static int
213b819ed4bSTrond Myklebust ff_ds_error_match(const struct nfs4_ff_layout_ds_err *e1,
214b819ed4bSTrond Myklebust 		const struct nfs4_ff_layout_ds_err *e2)
215d67ae825STom Haynes {
216b819ed4bSTrond Myklebust 	int ret;
217b819ed4bSTrond Myklebust 
218b819ed4bSTrond Myklebust 	if (e1->opnum != e2->opnum)
219b819ed4bSTrond Myklebust 		return e1->opnum < e2->opnum ? -1 : 1;
220b819ed4bSTrond Myklebust 	if (e1->status != e2->status)
221b819ed4bSTrond Myklebust 		return e1->status < e2->status ? -1 : 1;
22293b717fdSTrond Myklebust 	ret = memcmp(e1->stateid.data, e2->stateid.data,
22393b717fdSTrond Myklebust 			sizeof(e1->stateid.data));
224b819ed4bSTrond Myklebust 	if (ret != 0)
225b819ed4bSTrond Myklebust 		return ret;
226b819ed4bSTrond Myklebust 	ret = memcmp(&e1->deviceid, &e2->deviceid, sizeof(e1->deviceid));
227b819ed4bSTrond Myklebust 	if (ret != 0)
228b819ed4bSTrond Myklebust 		return ret;
22917822b20STrond Myklebust 	if (pnfs_end_offset(e1->offset, e1->length) < e2->offset)
230b819ed4bSTrond Myklebust 		return -1;
23117822b20STrond Myklebust 	if (e1->offset > pnfs_end_offset(e2->offset, e2->length))
232b819ed4bSTrond Myklebust 		return 1;
233b819ed4bSTrond Myklebust 	/* If ranges overlap or are contiguous, they are the same */
234b819ed4bSTrond Myklebust 	return 0;
235d67ae825STom Haynes }
236d67ae825STom Haynes 
237b819ed4bSTrond Myklebust static void
238d67ae825STom Haynes ff_layout_add_ds_error_locked(struct nfs4_flexfile_layout *flo,
239d67ae825STom Haynes 			      struct nfs4_ff_layout_ds_err *dserr)
240d67ae825STom Haynes {
241b819ed4bSTrond Myklebust 	struct nfs4_ff_layout_ds_err *err, *tmp;
242b819ed4bSTrond Myklebust 	struct list_head *head = &flo->error_list;
243b819ed4bSTrond Myklebust 	int match;
244d67ae825STom Haynes 
245b819ed4bSTrond Myklebust 	/* Do insertion sort w/ merges */
246b819ed4bSTrond Myklebust 	list_for_each_entry_safe(err, tmp, &flo->error_list, list) {
247b819ed4bSTrond Myklebust 		match = ff_ds_error_match(err, dserr);
248b819ed4bSTrond Myklebust 		if (match < 0)
249b819ed4bSTrond Myklebust 			continue;
250b819ed4bSTrond Myklebust 		if (match > 0) {
251b819ed4bSTrond Myklebust 			/* Add entry "dserr" _before_ entry "err" */
252b819ed4bSTrond Myklebust 			head = &err->list;
253d67ae825STom Haynes 			break;
254d67ae825STom Haynes 		}
255b819ed4bSTrond Myklebust 		/* Entries match, so merge "err" into "dserr" */
256b819ed4bSTrond Myklebust 		extend_ds_error(dserr, err->offset, err->length);
257*cb067935STrond Myklebust 		list_replace(&err->list, &dserr->list);
258b819ed4bSTrond Myklebust 		kfree(err);
259*cb067935STrond Myklebust 		return;
260d67ae825STom Haynes 	}
261d67ae825STom Haynes 
262b819ed4bSTrond Myklebust 	list_add_tail(&dserr->list, head);
263d67ae825STom Haynes }
264d67ae825STom Haynes 
265d67ae825STom Haynes int ff_layout_track_ds_error(struct nfs4_flexfile_layout *flo,
266d67ae825STom Haynes 			     struct nfs4_ff_layout_mirror *mirror, u64 offset,
267d67ae825STom Haynes 			     u64 length, int status, enum nfs_opnum4 opnum,
268d67ae825STom Haynes 			     gfp_t gfp_flags)
269d67ae825STom Haynes {
270d67ae825STom Haynes 	struct nfs4_ff_layout_ds_err *dserr;
271d67ae825STom Haynes 
272d67ae825STom Haynes 	if (status == 0)
273d67ae825STom Haynes 		return 0;
274d67ae825STom Haynes 
275d67ae825STom Haynes 	if (mirror->mirror_ds == NULL)
276d67ae825STom Haynes 		return -EINVAL;
277d67ae825STom Haynes 
278d67ae825STom Haynes 	dserr = kmalloc(sizeof(*dserr), gfp_flags);
279d67ae825STom Haynes 	if (!dserr)
280d67ae825STom Haynes 		return -ENOMEM;
281d67ae825STom Haynes 
282d67ae825STom Haynes 	INIT_LIST_HEAD(&dserr->list);
283d67ae825STom Haynes 	dserr->offset = offset;
284d67ae825STom Haynes 	dserr->length = length;
285d67ae825STom Haynes 	dserr->status = status;
286d67ae825STom Haynes 	dserr->opnum = opnum;
287d67ae825STom Haynes 	nfs4_stateid_copy(&dserr->stateid, &mirror->stateid);
288d67ae825STom Haynes 	memcpy(&dserr->deviceid, &mirror->mirror_ds->id_node.deviceid,
289d67ae825STom Haynes 	       NFS4_DEVICEID4_SIZE);
290d67ae825STom Haynes 
291d67ae825STom Haynes 	spin_lock(&flo->generic_hdr.plh_inode->i_lock);
292b819ed4bSTrond Myklebust 	ff_layout_add_ds_error_locked(flo, dserr);
293d67ae825STom Haynes 	spin_unlock(&flo->generic_hdr.plh_inode->i_lock);
294d67ae825STom Haynes 
295d67ae825STom Haynes 	return 0;
296d67ae825STom Haynes }
297d67ae825STom Haynes 
29857f3f4c0SJeff Layton static struct rpc_cred *
29957f3f4c0SJeff Layton ff_layout_get_mirror_cred(struct nfs4_ff_layout_mirror *mirror, u32 iomode)
30057f3f4c0SJeff Layton {
3013064b686SJeff Layton 	struct rpc_cred *cred, __rcu **pcred;
30257f3f4c0SJeff Layton 
3033064b686SJeff Layton 	if (iomode == IOMODE_READ)
3043064b686SJeff Layton 		pcred = &mirror->ro_cred;
3053064b686SJeff Layton 	else
3063064b686SJeff Layton 		pcred = &mirror->rw_cred;
30757f3f4c0SJeff Layton 
30857f3f4c0SJeff Layton 	rcu_read_lock();
30957f3f4c0SJeff Layton 	do {
31057f3f4c0SJeff Layton 		cred = rcu_dereference(*pcred);
31157f3f4c0SJeff Layton 		if (!cred)
31257f3f4c0SJeff Layton 			break;
31357f3f4c0SJeff Layton 
31457f3f4c0SJeff Layton 		cred = get_rpccred_rcu(cred);
31557f3f4c0SJeff Layton 	} while(!cred);
31657f3f4c0SJeff Layton 	rcu_read_unlock();
31757f3f4c0SJeff Layton 	return cred;
31857f3f4c0SJeff Layton }
31957f3f4c0SJeff Layton 
320d67ae825STom Haynes struct nfs_fh *
321d67ae825STom Haynes nfs4_ff_layout_select_ds_fh(struct pnfs_layout_segment *lseg, u32 mirror_idx)
322d67ae825STom Haynes {
323d67ae825STom Haynes 	struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, mirror_idx);
324d67ae825STom Haynes 	struct nfs_fh *fh = NULL;
325d67ae825STom Haynes 
326889d94d4STrond Myklebust 	if (!ff_layout_mirror_valid(lseg, mirror)) {
327889d94d4STrond Myklebust 		pr_err_ratelimited("NFS: %s: No data server for mirror offset index %d\n",
328d67ae825STom Haynes 			__func__, mirror_idx);
329d67ae825STom Haynes 		goto out;
330d67ae825STom Haynes 	}
331d67ae825STom Haynes 
332d67ae825STom Haynes 	/* FIXME: For now assume there is only 1 version available for the DS */
333d67ae825STom Haynes 	fh = &mirror->fh_versions[0];
334d67ae825STom Haynes out:
335d67ae825STom Haynes 	return fh;
336d67ae825STom Haynes }
337d67ae825STom Haynes 
33895e2b7e9SJeff Layton /**
33995e2b7e9SJeff Layton  * nfs4_ff_layout_prepare_ds - prepare a DS connection for an RPC call
34095e2b7e9SJeff Layton  * @lseg: the layout segment we're operating on
34195e2b7e9SJeff Layton  * @ds_idx: index of the DS to use
34295e2b7e9SJeff Layton  * @fail_return: return layout on connect failure?
34395e2b7e9SJeff Layton  *
34495e2b7e9SJeff Layton  * Try to prepare a DS connection to accept an RPC call. This involves
34595e2b7e9SJeff Layton  * selecting a mirror to use and connecting the client to it if it's not
34695e2b7e9SJeff Layton  * already connected.
34795e2b7e9SJeff Layton  *
34895e2b7e9SJeff Layton  * Since we only need a single functioning mirror to satisfy a read, we don't
34995e2b7e9SJeff Layton  * want to return the layout if there is one. For writes though, any down
35095e2b7e9SJeff Layton  * mirror should result in a LAYOUTRETURN. @fail_return is how we distinguish
35195e2b7e9SJeff Layton  * between the two cases.
35295e2b7e9SJeff Layton  *
35395e2b7e9SJeff Layton  * Returns a pointer to a connected DS object on success or NULL on failure.
35495e2b7e9SJeff Layton  */
355d67ae825STom Haynes struct nfs4_pnfs_ds *
356d67ae825STom Haynes nfs4_ff_layout_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx,
357d67ae825STom Haynes 			  bool fail_return)
358d67ae825STom Haynes {
359d67ae825STom Haynes 	struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx);
360d67ae825STom Haynes 	struct nfs4_pnfs_ds *ds = NULL;
361d67ae825STom Haynes 	struct nfs4_deviceid_node *devid;
362d67ae825STom Haynes 	struct inode *ino = lseg->pls_layout->plh_inode;
363d67ae825STom Haynes 	struct nfs_server *s = NFS_SERVER(ino);
364d67ae825STom Haynes 	unsigned int max_payload;
365d67ae825STom Haynes 
366889d94d4STrond Myklebust 	if (!ff_layout_mirror_valid(lseg, mirror)) {
367889d94d4STrond Myklebust 		pr_err_ratelimited("NFS: %s: No data server for offset index %d\n",
368d67ae825STom Haynes 			__func__, ds_idx);
369d67ae825STom Haynes 		goto out;
370d67ae825STom Haynes 	}
371d67ae825STom Haynes 
372d67ae825STom Haynes 	devid = &mirror->mirror_ds->id_node;
373d67ae825STom Haynes 	if (ff_layout_test_devid_unavailable(devid))
3743dc14735STrond Myklebust 		goto out_fail;
375d67ae825STom Haynes 
376d67ae825STom Haynes 	ds = mirror->mirror_ds->ds;
377d67ae825STom Haynes 	/* matching smp_wmb() in _nfs4_pnfs_v3/4_ds_connect */
378d67ae825STom Haynes 	smp_rmb();
379d67ae825STom Haynes 	if (ds->ds_clp)
38090a0be00SJeff Layton 		goto out;
381d67ae825STom Haynes 
382d67ae825STom Haynes 	/* FIXME: For now we assume the server sent only one version of NFS
383d67ae825STom Haynes 	 * to use for the DS.
384d67ae825STom Haynes 	 */
385d67ae825STom Haynes 	nfs4_pnfs_ds_connect(s, ds, devid, dataserver_timeo,
386d67ae825STom Haynes 			     dataserver_retrans,
387d67ae825STom Haynes 			     mirror->mirror_ds->ds_versions[0].version,
3887d38de3fSAnna Schumaker 			     mirror->mirror_ds->ds_versions[0].minor_version);
389d67ae825STom Haynes 
390d67ae825STom Haynes 	/* connect success, check rsize/wsize limit */
391d67ae825STom Haynes 	if (ds->ds_clp) {
392d67ae825STom Haynes 		max_payload =
393d67ae825STom Haynes 			nfs_block_size(rpc_max_payload(ds->ds_clp->cl_rpcclient),
394d67ae825STom Haynes 				       NULL);
395d67ae825STom Haynes 		if (mirror->mirror_ds->ds_versions[0].rsize > max_payload)
396d67ae825STom Haynes 			mirror->mirror_ds->ds_versions[0].rsize = max_payload;
397d67ae825STom Haynes 		if (mirror->mirror_ds->ds_versions[0].wsize > max_payload)
398d67ae825STom Haynes 			mirror->mirror_ds->ds_versions[0].wsize = max_payload;
3993dc14735STrond Myklebust 		goto out;
4003dc14735STrond Myklebust 	}
401d67ae825STom Haynes 	ff_layout_track_ds_error(FF_LAYOUT_FROM_HDR(lseg->pls_layout),
402d67ae825STom Haynes 				 mirror, lseg->pls_range.offset,
403d67ae825STom Haynes 				 lseg->pls_range.length, NFS4ERR_NXIO,
404d67ae825STom Haynes 				 OP_ILLEGAL, GFP_NOIO);
4053dc14735STrond Myklebust out_fail:
406094069f1SJeff Layton 	if (fail_return || !ff_layout_has_available_ds(lseg))
4072e5b29f0STrond Myklebust 		pnfs_error_mark_layout_for_return(ino, lseg);
408849dc324SJeff Layton 	ds = NULL;
409d67ae825STom Haynes out:
410d67ae825STom Haynes 	return ds;
411d67ae825STom Haynes }
412d67ae825STom Haynes 
413d67ae825STom Haynes struct rpc_cred *
414d67ae825STom Haynes ff_layout_get_ds_cred(struct pnfs_layout_segment *lseg, u32 ds_idx,
415d67ae825STom Haynes 		      struct rpc_cred *mdscred)
416d67ae825STom Haynes {
417d67ae825STom Haynes 	struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx);
418547a6376SJeff Layton 	struct rpc_cred *cred;
419d67ae825STom Haynes 
42057f3f4c0SJeff Layton 	if (mirror) {
42157f3f4c0SJeff Layton 		cred = ff_layout_get_mirror_cred(mirror, lseg->pls_range.iomode);
42257f3f4c0SJeff Layton 		if (!cred)
42357f3f4c0SJeff Layton 			cred = get_rpccred(mdscred);
42457f3f4c0SJeff Layton 	} else {
42557f3f4c0SJeff Layton 		cred = get_rpccred(mdscred);
42657f3f4c0SJeff Layton 	}
427d67ae825STom Haynes 	return cred;
428d67ae825STom Haynes }
429d67ae825STom Haynes 
430d67ae825STom Haynes /**
431d67ae825STom Haynes * Find or create a DS rpc client with th MDS server rpc client auth flavor
432d67ae825STom Haynes * in the nfs_client cl_ds_clients list.
433d67ae825STom Haynes */
434d67ae825STom Haynes struct rpc_clnt *
435d67ae825STom Haynes nfs4_ff_find_or_create_ds_client(struct pnfs_layout_segment *lseg, u32 ds_idx,
436d67ae825STom Haynes 				 struct nfs_client *ds_clp, struct inode *inode)
437d67ae825STom Haynes {
438d67ae825STom Haynes 	struct nfs4_ff_layout_mirror *mirror = FF_LAYOUT_COMP(lseg, ds_idx);
439d67ae825STom Haynes 
440d67ae825STom Haynes 	switch (mirror->mirror_ds->ds_versions[0].version) {
441d67ae825STom Haynes 	case 3:
442d67ae825STom Haynes 		/* For NFSv3 DS, flavor is set when creating DS connections */
443d67ae825STom Haynes 		return ds_clp->cl_rpcclient;
444d67ae825STom Haynes 	case 4:
445d67ae825STom Haynes 		return nfs4_find_or_create_ds_client(ds_clp, inode);
446d67ae825STom Haynes 	default:
447d67ae825STom Haynes 		BUG();
448d67ae825STom Haynes 	}
449d67ae825STom Haynes }
450d67ae825STom Haynes 
4515b9b3c85STrond Myklebust void ff_layout_free_ds_ioerr(struct list_head *head)
452d67ae825STom Haynes {
4535b9b3c85STrond Myklebust 	struct nfs4_ff_layout_ds_err *err;
4545b9b3c85STrond Myklebust 
4555b9b3c85STrond Myklebust 	while (!list_empty(head)) {
4565b9b3c85STrond Myklebust 		err = list_first_entry(head,
4575b9b3c85STrond Myklebust 				struct nfs4_ff_layout_ds_err,
4585b9b3c85STrond Myklebust 				list);
4595b9b3c85STrond Myklebust 		list_del(&err->list);
4605b9b3c85STrond Myklebust 		kfree(err);
4615b9b3c85STrond Myklebust 	}
4625b9b3c85STrond Myklebust }
4635b9b3c85STrond Myklebust 
4645b9b3c85STrond Myklebust /* called with inode i_lock held */
4655b9b3c85STrond Myklebust int ff_layout_encode_ds_ioerr(struct xdr_stream *xdr, const struct list_head *head)
4665b9b3c85STrond Myklebust {
4675b9b3c85STrond Myklebust 	struct nfs4_ff_layout_ds_err *err;
468d67ae825STom Haynes 	__be32 *p;
469d67ae825STom Haynes 
4705b9b3c85STrond Myklebust 	list_for_each_entry(err, head, list) {
471d67ae825STom Haynes 		/* offset(8) + length(8) + stateid(NFS4_STATEID_SIZE)
472d1354907STrond Myklebust 		 * + array length + deviceid(NFS4_DEVICEID4_SIZE)
473d1354907STrond Myklebust 		 * + status(4) + opnum(4)
474d67ae825STom Haynes 		 */
475d67ae825STom Haynes 		p = xdr_reserve_space(xdr,
476d1354907STrond Myklebust 				28 + NFS4_STATEID_SIZE + NFS4_DEVICEID4_SIZE);
477d67ae825STom Haynes 		if (unlikely(!p))
478d67ae825STom Haynes 			return -ENOBUFS;
479d67ae825STom Haynes 		p = xdr_encode_hyper(p, err->offset);
480d67ae825STom Haynes 		p = xdr_encode_hyper(p, err->length);
481d67ae825STom Haynes 		p = xdr_encode_opaque_fixed(p, &err->stateid,
482d67ae825STom Haynes 					    NFS4_STATEID_SIZE);
483d1354907STrond Myklebust 		/* Encode 1 error */
484d1354907STrond Myklebust 		*p++ = cpu_to_be32(1);
485d67ae825STom Haynes 		p = xdr_encode_opaque_fixed(p, &err->deviceid,
486d67ae825STom Haynes 					    NFS4_DEVICEID4_SIZE);
487d67ae825STom Haynes 		*p++ = cpu_to_be32(err->status);
488d67ae825STom Haynes 		*p++ = cpu_to_be32(err->opnum);
4895b9b3c85STrond Myklebust 		dprintk("%s: offset %llu length %llu status %d op %d\n",
490d67ae825STom Haynes 			__func__, err->offset, err->length, err->status,
4915b9b3c85STrond Myklebust 			err->opnum);
492d67ae825STom Haynes 	}
493d67ae825STom Haynes 
494d67ae825STom Haynes 	return 0;
495d67ae825STom Haynes }
496d67ae825STom Haynes 
4975b9b3c85STrond Myklebust static
4985b9b3c85STrond Myklebust unsigned int do_layout_fetch_ds_ioerr(struct pnfs_layout_hdr *lo,
4995b9b3c85STrond Myklebust 				      const struct pnfs_layout_range *range,
5005b9b3c85STrond Myklebust 				      struct list_head *head,
5015b9b3c85STrond Myklebust 				      unsigned int maxnum)
5025b9b3c85STrond Myklebust {
5035b9b3c85STrond Myklebust 	struct nfs4_flexfile_layout *flo = FF_LAYOUT_FROM_HDR(lo);
5045b9b3c85STrond Myklebust 	struct inode *inode = lo->plh_inode;
5055b9b3c85STrond Myklebust 	struct nfs4_ff_layout_ds_err *err, *n;
5065b9b3c85STrond Myklebust 	unsigned int ret = 0;
5075b9b3c85STrond Myklebust 
5085b9b3c85STrond Myklebust 	spin_lock(&inode->i_lock);
5095b9b3c85STrond Myklebust 	list_for_each_entry_safe(err, n, &flo->error_list, list) {
5105b9b3c85STrond Myklebust 		if (!pnfs_is_range_intersecting(err->offset,
5115b9b3c85STrond Myklebust 				pnfs_end_offset(err->offset, err->length),
5125b9b3c85STrond Myklebust 				range->offset,
5135b9b3c85STrond Myklebust 				pnfs_end_offset(range->offset, range->length)))
5145b9b3c85STrond Myklebust 			continue;
5155b9b3c85STrond Myklebust 		if (!maxnum)
5165b9b3c85STrond Myklebust 			break;
5175b9b3c85STrond Myklebust 		list_move(&err->list, head);
5185b9b3c85STrond Myklebust 		maxnum--;
5195b9b3c85STrond Myklebust 		ret++;
5205b9b3c85STrond Myklebust 	}
5215b9b3c85STrond Myklebust 	spin_unlock(&inode->i_lock);
5225b9b3c85STrond Myklebust 	return ret;
5235b9b3c85STrond Myklebust }
5245b9b3c85STrond Myklebust 
5255b9b3c85STrond Myklebust unsigned int ff_layout_fetch_ds_ioerr(struct pnfs_layout_hdr *lo,
5265b9b3c85STrond Myklebust 				      const struct pnfs_layout_range *range,
5275b9b3c85STrond Myklebust 				      struct list_head *head,
5285b9b3c85STrond Myklebust 				      unsigned int maxnum)
5295b9b3c85STrond Myklebust {
5305b9b3c85STrond Myklebust 	unsigned int ret;
5315b9b3c85STrond Myklebust 
5325b9b3c85STrond Myklebust 	ret = do_layout_fetch_ds_ioerr(lo, range, head, maxnum);
5335b9b3c85STrond Myklebust 	/* If we're over the max, discard all remaining entries */
5345b9b3c85STrond Myklebust 	if (ret == maxnum) {
5355b9b3c85STrond Myklebust 		LIST_HEAD(discard);
5365b9b3c85STrond Myklebust 		do_layout_fetch_ds_ioerr(lo, range, &discard, -1);
5375b9b3c85STrond Myklebust 		ff_layout_free_ds_ioerr(&discard);
5385b9b3c85STrond Myklebust 	}
5395b9b3c85STrond Myklebust 	return ret;
5405b9b3c85STrond Myklebust }
5415b9b3c85STrond Myklebust 
54281d6dc8bSTrond Myklebust static bool ff_read_layout_has_available_ds(struct pnfs_layout_segment *lseg)
543d67ae825STom Haynes {
544d67ae825STom Haynes 	struct nfs4_ff_layout_mirror *mirror;
545d67ae825STom Haynes 	struct nfs4_deviceid_node *devid;
54681d6dc8bSTrond Myklebust 	u32 idx;
547d67ae825STom Haynes 
548d67ae825STom Haynes 	for (idx = 0; idx < FF_LAYOUT_MIRROR_COUNT(lseg); idx++) {
549d67ae825STom Haynes 		mirror = FF_LAYOUT_COMP(lseg, idx);
550d67ae825STom Haynes 		if (mirror && mirror->mirror_ds) {
551d67ae825STom Haynes 			devid = &mirror->mirror_ds->id_node;
552d67ae825STom Haynes 			if (!ff_layout_test_devid_unavailable(devid))
553d67ae825STom Haynes 				return true;
554d67ae825STom Haynes 		}
555d67ae825STom Haynes 	}
556d67ae825STom Haynes 
557d67ae825STom Haynes 	return false;
558d67ae825STom Haynes }
559d67ae825STom Haynes 
56081d6dc8bSTrond Myklebust static bool ff_rw_layout_has_available_ds(struct pnfs_layout_segment *lseg)
56181d6dc8bSTrond Myklebust {
56281d6dc8bSTrond Myklebust 	struct nfs4_ff_layout_mirror *mirror;
56381d6dc8bSTrond Myklebust 	struct nfs4_deviceid_node *devid;
56481d6dc8bSTrond Myklebust 	u32 idx;
56581d6dc8bSTrond Myklebust 
56681d6dc8bSTrond Myklebust 	for (idx = 0; idx < FF_LAYOUT_MIRROR_COUNT(lseg); idx++) {
56781d6dc8bSTrond Myklebust 		mirror = FF_LAYOUT_COMP(lseg, idx);
56881d6dc8bSTrond Myklebust 		if (!mirror || !mirror->mirror_ds)
56981d6dc8bSTrond Myklebust 			return false;
57081d6dc8bSTrond Myklebust 		devid = &mirror->mirror_ds->id_node;
57181d6dc8bSTrond Myklebust 		if (ff_layout_test_devid_unavailable(devid))
57281d6dc8bSTrond Myklebust 			return false;
57381d6dc8bSTrond Myklebust 	}
57481d6dc8bSTrond Myklebust 
57581d6dc8bSTrond Myklebust 	return FF_LAYOUT_MIRROR_COUNT(lseg) != 0;
57681d6dc8bSTrond Myklebust }
57781d6dc8bSTrond Myklebust 
57881d6dc8bSTrond Myklebust bool ff_layout_has_available_ds(struct pnfs_layout_segment *lseg)
57981d6dc8bSTrond Myklebust {
58081d6dc8bSTrond Myklebust 	if (lseg->pls_range.iomode == IOMODE_READ)
58181d6dc8bSTrond Myklebust 		return  ff_read_layout_has_available_ds(lseg);
58281d6dc8bSTrond Myklebust 	/* Note: RW layout needs all mirrors available */
58381d6dc8bSTrond Myklebust 	return ff_rw_layout_has_available_ds(lseg);
58481d6dc8bSTrond Myklebust }
58581d6dc8bSTrond Myklebust 
5863b13b4b3STom Haynes bool ff_layout_avoid_mds_available_ds(struct pnfs_layout_segment *lseg)
5873b13b4b3STom Haynes {
5883b13b4b3STom Haynes 	return ff_layout_no_fallback_to_mds(lseg) ||
5893b13b4b3STom Haynes 	       ff_layout_has_available_ds(lseg);
5903b13b4b3STom Haynes }
5913b13b4b3STom Haynes 
592fb1084e3STom Haynes bool ff_layout_avoid_read_on_rw(struct pnfs_layout_segment *lseg)
593fb1084e3STom Haynes {
594fb1084e3STom Haynes 	return lseg->pls_range.iomode == IOMODE_RW &&
595fb1084e3STom Haynes 	       ff_layout_no_read_on_rw(lseg);
596fb1084e3STom Haynes }
597fb1084e3STom Haynes 
598d67ae825STom Haynes module_param(dataserver_retrans, uint, 0644);
599d67ae825STom Haynes MODULE_PARM_DESC(dataserver_retrans, "The  number of times the NFSv4.1 client "
600d67ae825STom Haynes 			"retries a request before it attempts further "
601d67ae825STom Haynes 			" recovery  action.");
602d67ae825STom Haynes module_param(dataserver_timeo, uint, 0644);
603d67ae825STom Haynes MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the "
604d67ae825STom Haynes 			"NFSv4.1  client  waits for a response from a "
605d67ae825STom Haynes 			" data server before it retries an NFS request.");
606