xref: /OK3568_Linux_fs/kernel/fs/nfs/filelayout/filelayoutdev.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  Device operations for the pnfs nfs4 file layout driver.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *  Copyright (c) 2002
5*4882a593Smuzhiyun  *  The Regents of the University of Michigan
6*4882a593Smuzhiyun  *  All Rights Reserved
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *  Dean Hildebrand <dhildebz@umich.edu>
9*4882a593Smuzhiyun  *  Garth Goodson   <Garth.Goodson@netapp.com>
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *  Permission is granted to use, copy, create derivative works, and
12*4882a593Smuzhiyun  *  redistribute this software and such derivative works for any purpose,
13*4882a593Smuzhiyun  *  so long as the name of the University of Michigan is not used in
14*4882a593Smuzhiyun  *  any advertising or publicity pertaining to the use or distribution
15*4882a593Smuzhiyun  *  of this software without specific, written prior authorization. If
16*4882a593Smuzhiyun  *  the above copyright notice or any other identification of the
17*4882a593Smuzhiyun  *  University of Michigan is included in any copy of any portion of
18*4882a593Smuzhiyun  *  this software, then the disclaimer below must also be included.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  *  This software is provided as is, without representation or warranty
21*4882a593Smuzhiyun  *  of any kind either express or implied, including without limitation
22*4882a593Smuzhiyun  *  the implied warranties of merchantability, fitness for a particular
23*4882a593Smuzhiyun  *  purpose, or noninfringement.  The Regents of the University of
24*4882a593Smuzhiyun  *  Michigan shall not be liable for any damages, including special,
25*4882a593Smuzhiyun  *  indirect, incidental, or consequential damages, with respect to any
26*4882a593Smuzhiyun  *  claim arising out of or in connection with the use of the software,
27*4882a593Smuzhiyun  *  even if it has been or is hereafter advised of the possibility of
28*4882a593Smuzhiyun  *  such damages.
29*4882a593Smuzhiyun  */
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun #include <linux/nfs_fs.h>
32*4882a593Smuzhiyun #include <linux/vmalloc.h>
33*4882a593Smuzhiyun #include <linux/module.h>
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include "../internal.h"
36*4882a593Smuzhiyun #include "../nfs4session.h"
37*4882a593Smuzhiyun #include "filelayout.h"
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #define NFSDBG_FACILITY		NFSDBG_PNFS_LD
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun static unsigned int dataserver_timeo = NFS4_DEF_DS_TIMEO;
42*4882a593Smuzhiyun static unsigned int dataserver_retrans = NFS4_DEF_DS_RETRANS;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun void
nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr * dsaddr)45*4882a593Smuzhiyun nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	struct nfs4_pnfs_ds *ds;
48*4882a593Smuzhiyun 	int i;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	nfs4_print_deviceid(&dsaddr->id_node.deviceid);
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	for (i = 0; i < dsaddr->ds_num; i++) {
53*4882a593Smuzhiyun 		ds = dsaddr->ds_list[i];
54*4882a593Smuzhiyun 		if (ds != NULL)
55*4882a593Smuzhiyun 			nfs4_pnfs_ds_put(ds);
56*4882a593Smuzhiyun 	}
57*4882a593Smuzhiyun 	kfree(dsaddr->stripe_indices);
58*4882a593Smuzhiyun 	kfree_rcu(dsaddr, id_node.rcu);
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun /* Decode opaque device data and return the result */
62*4882a593Smuzhiyun struct nfs4_file_layout_dsaddr *
nfs4_fl_alloc_deviceid_node(struct nfs_server * server,struct pnfs_device * pdev,gfp_t gfp_flags)63*4882a593Smuzhiyun nfs4_fl_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
64*4882a593Smuzhiyun 		gfp_t gfp_flags)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	int i;
67*4882a593Smuzhiyun 	u32 cnt, num;
68*4882a593Smuzhiyun 	u8 *indexp;
69*4882a593Smuzhiyun 	__be32 *p;
70*4882a593Smuzhiyun 	u8 *stripe_indices;
71*4882a593Smuzhiyun 	u8 max_stripe_index;
72*4882a593Smuzhiyun 	struct nfs4_file_layout_dsaddr *dsaddr = NULL;
73*4882a593Smuzhiyun 	struct xdr_stream stream;
74*4882a593Smuzhiyun 	struct xdr_buf buf;
75*4882a593Smuzhiyun 	struct page *scratch;
76*4882a593Smuzhiyun 	struct list_head dsaddrs;
77*4882a593Smuzhiyun 	struct nfs4_pnfs_ds_addr *da;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	/* set up xdr stream */
80*4882a593Smuzhiyun 	scratch = alloc_page(gfp_flags);
81*4882a593Smuzhiyun 	if (!scratch)
82*4882a593Smuzhiyun 		goto out_err;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
85*4882a593Smuzhiyun 	xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	/* Get the stripe count (number of stripe index) */
88*4882a593Smuzhiyun 	p = xdr_inline_decode(&stream, 4);
89*4882a593Smuzhiyun 	if (unlikely(!p))
90*4882a593Smuzhiyun 		goto out_err_free_scratch;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	cnt = be32_to_cpup(p);
93*4882a593Smuzhiyun 	dprintk("%s stripe count  %d\n", __func__, cnt);
94*4882a593Smuzhiyun 	if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
95*4882a593Smuzhiyun 		printk(KERN_WARNING "NFS: %s: stripe count %d greater than "
96*4882a593Smuzhiyun 		       "supported maximum %d\n", __func__,
97*4882a593Smuzhiyun 			cnt, NFS4_PNFS_MAX_STRIPE_CNT);
98*4882a593Smuzhiyun 		goto out_err_free_scratch;
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	/* read stripe indices */
102*4882a593Smuzhiyun 	stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
103*4882a593Smuzhiyun 	if (!stripe_indices)
104*4882a593Smuzhiyun 		goto out_err_free_scratch;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	p = xdr_inline_decode(&stream, cnt << 2);
107*4882a593Smuzhiyun 	if (unlikely(!p))
108*4882a593Smuzhiyun 		goto out_err_free_stripe_indices;
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	indexp = &stripe_indices[0];
111*4882a593Smuzhiyun 	max_stripe_index = 0;
112*4882a593Smuzhiyun 	for (i = 0; i < cnt; i++) {
113*4882a593Smuzhiyun 		*indexp = be32_to_cpup(p++);
114*4882a593Smuzhiyun 		max_stripe_index = max(max_stripe_index, *indexp);
115*4882a593Smuzhiyun 		indexp++;
116*4882a593Smuzhiyun 	}
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	/* Check the multipath list count */
119*4882a593Smuzhiyun 	p = xdr_inline_decode(&stream, 4);
120*4882a593Smuzhiyun 	if (unlikely(!p))
121*4882a593Smuzhiyun 		goto out_err_free_stripe_indices;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	num = be32_to_cpup(p);
124*4882a593Smuzhiyun 	dprintk("%s ds_num %u\n", __func__, num);
125*4882a593Smuzhiyun 	if (num > NFS4_PNFS_MAX_MULTI_CNT) {
126*4882a593Smuzhiyun 		printk(KERN_WARNING "NFS: %s: multipath count %d greater than "
127*4882a593Smuzhiyun 			"supported maximum %d\n", __func__,
128*4882a593Smuzhiyun 			num, NFS4_PNFS_MAX_MULTI_CNT);
129*4882a593Smuzhiyun 		goto out_err_free_stripe_indices;
130*4882a593Smuzhiyun 	}
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	/* validate stripe indices are all < num */
133*4882a593Smuzhiyun 	if (max_stripe_index >= num) {
134*4882a593Smuzhiyun 		printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n",
135*4882a593Smuzhiyun 			__func__, max_stripe_index, num);
136*4882a593Smuzhiyun 		goto out_err_free_stripe_indices;
137*4882a593Smuzhiyun 	}
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	dsaddr = kzalloc(sizeof(*dsaddr) +
140*4882a593Smuzhiyun 			(sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
141*4882a593Smuzhiyun 			gfp_flags);
142*4882a593Smuzhiyun 	if (!dsaddr)
143*4882a593Smuzhiyun 		goto out_err_free_stripe_indices;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	dsaddr->stripe_count = cnt;
146*4882a593Smuzhiyun 	dsaddr->stripe_indices = stripe_indices;
147*4882a593Smuzhiyun 	stripe_indices = NULL;
148*4882a593Smuzhiyun 	dsaddr->ds_num = num;
149*4882a593Smuzhiyun 	nfs4_init_deviceid_node(&dsaddr->id_node, server, &pdev->dev_id);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dsaddrs);
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	for (i = 0; i < dsaddr->ds_num; i++) {
154*4882a593Smuzhiyun 		int j;
155*4882a593Smuzhiyun 		u32 mp_count;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 		p = xdr_inline_decode(&stream, 4);
158*4882a593Smuzhiyun 		if (unlikely(!p))
159*4882a593Smuzhiyun 			goto out_err_free_deviceid;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 		mp_count = be32_to_cpup(p); /* multipath count */
162*4882a593Smuzhiyun 		for (j = 0; j < mp_count; j++) {
163*4882a593Smuzhiyun 			da = nfs4_decode_mp_ds_addr(server->nfs_client->cl_net,
164*4882a593Smuzhiyun 						    &stream, gfp_flags);
165*4882a593Smuzhiyun 			if (da)
166*4882a593Smuzhiyun 				list_add_tail(&da->da_node, &dsaddrs);
167*4882a593Smuzhiyun 		}
168*4882a593Smuzhiyun 		if (list_empty(&dsaddrs)) {
169*4882a593Smuzhiyun 			dprintk("%s: no suitable DS addresses found\n",
170*4882a593Smuzhiyun 				__func__);
171*4882a593Smuzhiyun 			goto out_err_free_deviceid;
172*4882a593Smuzhiyun 		}
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 		dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
175*4882a593Smuzhiyun 		if (!dsaddr->ds_list[i])
176*4882a593Smuzhiyun 			goto out_err_drain_dsaddrs;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 		/* If DS was already in cache, free ds addrs */
179*4882a593Smuzhiyun 		while (!list_empty(&dsaddrs)) {
180*4882a593Smuzhiyun 			da = list_first_entry(&dsaddrs,
181*4882a593Smuzhiyun 					      struct nfs4_pnfs_ds_addr,
182*4882a593Smuzhiyun 					      da_node);
183*4882a593Smuzhiyun 			list_del_init(&da->da_node);
184*4882a593Smuzhiyun 			kfree(da->da_remotestr);
185*4882a593Smuzhiyun 			kfree(da);
186*4882a593Smuzhiyun 		}
187*4882a593Smuzhiyun 	}
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	__free_page(scratch);
190*4882a593Smuzhiyun 	return dsaddr;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun out_err_drain_dsaddrs:
193*4882a593Smuzhiyun 	while (!list_empty(&dsaddrs)) {
194*4882a593Smuzhiyun 		da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
195*4882a593Smuzhiyun 				      da_node);
196*4882a593Smuzhiyun 		list_del_init(&da->da_node);
197*4882a593Smuzhiyun 		kfree(da->da_remotestr);
198*4882a593Smuzhiyun 		kfree(da);
199*4882a593Smuzhiyun 	}
200*4882a593Smuzhiyun out_err_free_deviceid:
201*4882a593Smuzhiyun 	nfs4_fl_free_deviceid(dsaddr);
202*4882a593Smuzhiyun 	/* stripe_indicies was part of dsaddr */
203*4882a593Smuzhiyun 	goto out_err_free_scratch;
204*4882a593Smuzhiyun out_err_free_stripe_indices:
205*4882a593Smuzhiyun 	kfree(stripe_indices);
206*4882a593Smuzhiyun out_err_free_scratch:
207*4882a593Smuzhiyun 	__free_page(scratch);
208*4882a593Smuzhiyun out_err:
209*4882a593Smuzhiyun 	dprintk("%s ERROR: returning NULL\n", __func__);
210*4882a593Smuzhiyun 	return NULL;
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun void
nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr * dsaddr)214*4882a593Smuzhiyun nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun 	nfs4_put_deviceid_node(&dsaddr->id_node);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun /*
220*4882a593Smuzhiyun  * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
221*4882a593Smuzhiyun  * Then: ((res + fsi) % dsaddr->stripe_count)
222*4882a593Smuzhiyun  */
223*4882a593Smuzhiyun u32
nfs4_fl_calc_j_index(struct pnfs_layout_segment * lseg,loff_t offset)224*4882a593Smuzhiyun nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
225*4882a593Smuzhiyun {
226*4882a593Smuzhiyun 	struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
227*4882a593Smuzhiyun 	u64 tmp;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	tmp = offset - flseg->pattern_offset;
230*4882a593Smuzhiyun 	do_div(tmp, flseg->stripe_unit);
231*4882a593Smuzhiyun 	tmp += flseg->first_stripe_index;
232*4882a593Smuzhiyun 	return do_div(tmp, flseg->dsaddr->stripe_count);
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun u32
nfs4_fl_calc_ds_index(struct pnfs_layout_segment * lseg,u32 j)236*4882a593Smuzhiyun nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun 	return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun struct nfs_fh *
nfs4_fl_select_ds_fh(struct pnfs_layout_segment * lseg,u32 j)242*4882a593Smuzhiyun nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
245*4882a593Smuzhiyun 	u32 i;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	if (flseg->stripe_type == STRIPE_SPARSE) {
248*4882a593Smuzhiyun 		if (flseg->num_fh == 1)
249*4882a593Smuzhiyun 			i = 0;
250*4882a593Smuzhiyun 		else if (flseg->num_fh == 0)
251*4882a593Smuzhiyun 			/* Use the MDS OPEN fh set in nfs_read_rpcsetup */
252*4882a593Smuzhiyun 			return NULL;
253*4882a593Smuzhiyun 		else
254*4882a593Smuzhiyun 			i = nfs4_fl_calc_ds_index(lseg, j);
255*4882a593Smuzhiyun 	} else
256*4882a593Smuzhiyun 		i = j;
257*4882a593Smuzhiyun 	return flseg->fh_array[i];
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun /* Upon return, either ds is connected, or ds is NULL */
261*4882a593Smuzhiyun struct nfs4_pnfs_ds *
nfs4_fl_prepare_ds(struct pnfs_layout_segment * lseg,u32 ds_idx)262*4882a593Smuzhiyun nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
263*4882a593Smuzhiyun {
264*4882a593Smuzhiyun 	struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
265*4882a593Smuzhiyun 	struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
266*4882a593Smuzhiyun 	struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
267*4882a593Smuzhiyun 	struct nfs4_pnfs_ds *ret = ds;
268*4882a593Smuzhiyun 	struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
269*4882a593Smuzhiyun 	int status;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	if (ds == NULL) {
272*4882a593Smuzhiyun 		printk(KERN_ERR "NFS: %s: No data server for offset index %d\n",
273*4882a593Smuzhiyun 			__func__, ds_idx);
274*4882a593Smuzhiyun 		pnfs_generic_mark_devid_invalid(devid);
275*4882a593Smuzhiyun 		goto out;
276*4882a593Smuzhiyun 	}
277*4882a593Smuzhiyun 	smp_rmb();
278*4882a593Smuzhiyun 	if (ds->ds_clp)
279*4882a593Smuzhiyun 		goto out_test_devid;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	status = nfs4_pnfs_ds_connect(s, ds, devid, dataserver_timeo,
282*4882a593Smuzhiyun 			     dataserver_retrans, 4,
283*4882a593Smuzhiyun 			     s->nfs_client->cl_minorversion);
284*4882a593Smuzhiyun 	if (status) {
285*4882a593Smuzhiyun 		nfs4_mark_deviceid_unavailable(devid);
286*4882a593Smuzhiyun 		ret = NULL;
287*4882a593Smuzhiyun 		goto out;
288*4882a593Smuzhiyun 	}
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun out_test_devid:
291*4882a593Smuzhiyun 	if (ret->ds_clp == NULL ||
292*4882a593Smuzhiyun 	    filelayout_test_devid_unavailable(devid))
293*4882a593Smuzhiyun 		ret = NULL;
294*4882a593Smuzhiyun out:
295*4882a593Smuzhiyun 	return ret;
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun module_param(dataserver_retrans, uint, 0644);
299*4882a593Smuzhiyun MODULE_PARM_DESC(dataserver_retrans, "The  number of times the NFSv4.1 client "
300*4882a593Smuzhiyun 			"retries a request before it attempts further "
301*4882a593Smuzhiyun 			" recovery  action.");
302*4882a593Smuzhiyun module_param(dataserver_timeo, uint, 0644);
303*4882a593Smuzhiyun MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the "
304*4882a593Smuzhiyun 			"NFSv4.1  client  waits for a response from a "
305*4882a593Smuzhiyun 			" data server before it retries an NFS request.");
306