blob: a5589b791439af1c6f426cb95e2a0670edd2a30b [file] [log] [blame]
Andy Adamson16b374c2010-10-20 00:18:04 -04001/*
2 * Device operations for the pnfs nfs4 file layout driver.
3 *
4 * Copyright (c) 2002
5 * The Regents of the University of Michigan
6 * All Rights Reserved
7 *
8 * Dean Hildebrand <dhildebz@umich.edu>
9 * Garth Goodson <Garth.Goodson@netapp.com>
10 *
11 * Permission is granted to use, copy, create derivative works, and
12 * redistribute this software and such derivative works for any purpose,
13 * so long as the name of the University of Michigan is not used in
14 * any advertising or publicity pertaining to the use or distribution
15 * of this software without specific, written prior authorization. If
16 * the above copyright notice or any other identification of the
17 * University of Michigan is included in any copy of any portion of
18 * this software, then the disclaimer below must also be included.
19 *
20 * This software is provided as is, without representation or warranty
21 * of any kind either express or implied, including without limitation
22 * the implied warranties of merchantability, fitness for a particular
23 * purpose, or noninfringement. The Regents of the University of
24 * Michigan shall not be liable for any damages, including special,
25 * indirect, incidental, or consequential damages, with respect to any
26 * claim arising out of or in connection with the use of the software,
27 * even if it has been or is hereafter advised of the possibility of
28 * such damages.
29 */
30
31#include <linux/nfs_fs.h>
32#include <linux/vmalloc.h>
Andy Adamson98fc6852012-04-27 17:53:45 -040033#include <linux/module.h>
Andy Adamson16b374c2010-10-20 00:18:04 -040034
Tom Haynesb5968722014-05-12 14:35:52 -070035#include "../internal.h"
36#include "../nfs4session.h"
37#include "filelayout.h"
Andy Adamson16b374c2010-10-20 00:18:04 -040038
39#define NFSDBG_FACILITY NFSDBG_PNFS_LD
40
Andy Adamson98fc6852012-04-27 17:53:45 -040041static unsigned int dataserver_timeo = NFS4_DEF_DS_TIMEO;
42static unsigned int dataserver_retrans = NFS4_DEF_DS_RETRANS;
43
Benny Halevy1775bc32011-05-20 13:47:33 +020044void
Andy Adamson16b374c2010-10-20 00:18:04 -040045nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
46{
47 struct nfs4_pnfs_ds *ds;
48 int i;
49
Benny Halevya1eaecb2011-05-19 22:14:47 -040050 nfs4_print_deviceid(&dsaddr->id_node.deviceid);
Andy Adamson16b374c2010-10-20 00:18:04 -040051
52 for (i = 0; i < dsaddr->ds_num; i++) {
53 ds = dsaddr->ds_list[i];
Peng Tao875ae062014-05-29 21:06:57 +080054 if (ds != NULL)
55 nfs4_pnfs_ds_put(ds);
Andy Adamson16b374c2010-10-20 00:18:04 -040056 }
57 kfree(dsaddr->stripe_indices);
Trond Myklebust84a80f62015-03-09 15:23:35 -040058 kfree_rcu(dsaddr, id_node.rcu);
Andy Adamson16b374c2010-10-20 00:18:04 -040059}
60
Andy Adamson16b374c2010-10-20 00:18:04 -040061/* Decode opaque device data and return the result */
Christoph Hellwig661373b2014-09-02 21:27:57 -070062struct nfs4_file_layout_dsaddr *
63nfs4_fl_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
64 gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -040065{
Weston Andros Adamson35124a02011-03-24 16:48:21 -040066 int i;
Andy Adamson16b374c2010-10-20 00:18:04 -040067 u32 cnt, num;
68 u8 *indexp;
Weston Andros Adamson35124a02011-03-24 16:48:21 -040069 __be32 *p;
70 u8 *stripe_indices;
71 u8 max_stripe_index;
72 struct nfs4_file_layout_dsaddr *dsaddr = NULL;
73 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -040074 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -040075 struct page *scratch;
Weston Andros Adamson14f9a602011-05-31 18:48:57 -040076 struct list_head dsaddrs;
77 struct nfs4_pnfs_ds_addr *da;
Weston Andros Adamson35124a02011-03-24 16:48:21 -040078
79 /* set up xdr stream */
Trond Myklebusta75b9df2011-05-11 18:00:51 -040080 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -040081 if (!scratch)
82 goto out_err;
83
Benny Halevyf7da7a12011-05-19 14:16:47 -040084 xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
Weston Andros Adamson35124a02011-03-24 16:48:21 -040085 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
Andy Adamson16b374c2010-10-20 00:18:04 -040086
87 /* Get the stripe count (number of stripe index) */
Weston Andros Adamson35124a02011-03-24 16:48:21 -040088 p = xdr_inline_decode(&stream, 4);
89 if (unlikely(!p))
90 goto out_err_free_scratch;
91
92 cnt = be32_to_cpup(p);
Andy Adamson16b374c2010-10-20 00:18:04 -040093 dprintk("%s stripe count %d\n", __func__, cnt);
94 if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -050095 printk(KERN_WARNING "NFS: %s: stripe count %d greater than "
Andy Adamson16b374c2010-10-20 00:18:04 -040096 "supported maximum %d\n", __func__,
97 cnt, NFS4_PNFS_MAX_STRIPE_CNT);
Weston Andros Adamson35124a02011-03-24 16:48:21 -040098 goto out_err_free_scratch;
99 }
100
101 /* read stripe indices */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400102 stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400103 if (!stripe_indices)
104 goto out_err_free_scratch;
105
106 p = xdr_inline_decode(&stream, cnt << 2);
107 if (unlikely(!p))
108 goto out_err_free_stripe_indices;
109
110 indexp = &stripe_indices[0];
111 max_stripe_index = 0;
112 for (i = 0; i < cnt; i++) {
113 *indexp = be32_to_cpup(p++);
114 max_stripe_index = max(max_stripe_index, *indexp);
115 indexp++;
Andy Adamson16b374c2010-10-20 00:18:04 -0400116 }
117
118 /* Check the multipath list count */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400119 p = xdr_inline_decode(&stream, 4);
120 if (unlikely(!p))
121 goto out_err_free_stripe_indices;
122
123 num = be32_to_cpup(p);
Andy Adamson16b374c2010-10-20 00:18:04 -0400124 dprintk("%s ds_num %u\n", __func__, num);
125 if (num > NFS4_PNFS_MAX_MULTI_CNT) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500126 printk(KERN_WARNING "NFS: %s: multipath count %d greater than "
Andy Adamson16b374c2010-10-20 00:18:04 -0400127 "supported maximum %d\n", __func__,
128 num, NFS4_PNFS_MAX_MULTI_CNT);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400129 goto out_err_free_stripe_indices;
Andy Adamson16b374c2010-10-20 00:18:04 -0400130 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400131
132 /* validate stripe indices are all < num */
133 if (max_stripe_index >= num) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500134 printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n",
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400135 __func__, max_stripe_index, num);
136 goto out_err_free_stripe_indices;
137 }
138
Andy Adamson16b374c2010-10-20 00:18:04 -0400139 dsaddr = kzalloc(sizeof(*dsaddr) +
140 (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400141 gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400142 if (!dsaddr)
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400143 goto out_err_free_stripe_indices;
Andy Adamson16b374c2010-10-20 00:18:04 -0400144
145 dsaddr->stripe_count = cnt;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400146 dsaddr->stripe_indices = stripe_indices;
147 stripe_indices = NULL;
Andy Adamson16b374c2010-10-20 00:18:04 -0400148 dsaddr->ds_num = num;
Christoph Hellwig661373b2014-09-02 21:27:57 -0700149 nfs4_init_deviceid_node(&dsaddr->id_node, server, &pdev->dev_id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400150
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400151 INIT_LIST_HEAD(&dsaddrs);
152
Andy Adamson16b374c2010-10-20 00:18:04 -0400153 for (i = 0; i < dsaddr->ds_num; i++) {
154 int j;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400155 u32 mp_count;
Andy Adamson16b374c2010-10-20 00:18:04 -0400156
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400157 p = xdr_inline_decode(&stream, 4);
158 if (unlikely(!p))
159 goto out_err_free_deviceid;
160
161 mp_count = be32_to_cpup(p); /* multipath count */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400162 for (j = 0; j < mp_count; j++) {
Peng Tao6b7f3cf2014-05-29 21:06:59 +0800163 da = nfs4_decode_mp_ds_addr(server->nfs_client->cl_net,
164 &stream, gfp_flags);
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400165 if (da)
166 list_add_tail(&da->da_node, &dsaddrs);
167 }
168 if (list_empty(&dsaddrs)) {
169 dprintk("%s: no suitable DS addresses found\n",
170 __func__);
171 goto out_err_free_deviceid;
172 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400173
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400174 dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
175 if (!dsaddr->ds_list[i])
176 goto out_err_drain_dsaddrs;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400177
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400178 /* If DS was already in cache, free ds addrs */
179 while (!list_empty(&dsaddrs)) {
180 da = list_first_entry(&dsaddrs,
181 struct nfs4_pnfs_ds_addr,
182 da_node);
183 list_del_init(&da->da_node);
184 kfree(da->da_remotestr);
185 kfree(da);
Andy Adamson16b374c2010-10-20 00:18:04 -0400186 }
187 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400188
189 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400190 return dsaddr;
191
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400192out_err_drain_dsaddrs:
193 while (!list_empty(&dsaddrs)) {
194 da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
195 da_node);
196 list_del_init(&da->da_node);
197 kfree(da->da_remotestr);
198 kfree(da);
199 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400200out_err_free_deviceid:
Andy Adamson16b374c2010-10-20 00:18:04 -0400201 nfs4_fl_free_deviceid(dsaddr);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400202 /* stripe_indicies was part of dsaddr */
203 goto out_err_free_scratch;
204out_err_free_stripe_indices:
205 kfree(stripe_indices);
206out_err_free_scratch:
207 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400208out_err:
209 dprintk("%s ERROR: returning NULL\n", __func__);
210 return NULL;
211}
212
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000213void
214nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
Andy Adamson16b374c2010-10-20 00:18:04 -0400215{
Benny Halevy1775bc32011-05-20 13:47:33 +0200216 nfs4_put_deviceid_node(&dsaddr->id_node);
Andy Adamson16b374c2010-10-20 00:18:04 -0400217}
Fred Isamancfe7f412011-03-01 01:34:18 +0000218
219/*
220 * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
221 * Then: ((res + fsi) % dsaddr->stripe_count)
222 */
223u32
224nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
225{
226 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
227 u64 tmp;
228
229 tmp = offset - flseg->pattern_offset;
230 do_div(tmp, flseg->stripe_unit);
231 tmp += flseg->first_stripe_index;
232 return do_div(tmp, flseg->dsaddr->stripe_count);
233}
234
235u32
236nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
237{
238 return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
239}
240
241struct nfs_fh *
242nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
243{
244 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
245 u32 i;
246
247 if (flseg->stripe_type == STRIPE_SPARSE) {
248 if (flseg->num_fh == 1)
249 i = 0;
250 else if (flseg->num_fh == 0)
251 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
252 return NULL;
253 else
254 i = nfs4_fl_calc_ds_index(lseg, j);
255 } else
256 i = j;
257 return flseg->fh_array[i];
258}
259
Peng Tao7405f9e2014-05-29 21:06:58 +0800260/* Upon return, either ds is connected, or ds is NULL */
Fred Isamancfe7f412011-03-01 01:34:18 +0000261struct nfs4_pnfs_ds *
262nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
263{
264 struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
265 struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
Andy Adamson554d4582012-04-27 17:53:42 -0400266 struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
Trond Myklebust52b26a32013-09-26 14:08:36 -0400267 struct nfs4_pnfs_ds *ret = ds;
Peng Tao7405f9e2014-05-29 21:06:58 +0800268 struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
Fred Isamancfe7f412011-03-01 01:34:18 +0000269
270 if (ds == NULL) {
Weston Andros Adamsona0308892012-01-26 13:32:23 -0500271 printk(KERN_ERR "NFS: %s: No data server for offset index %d\n",
Fred Isamancfe7f412011-03-01 01:34:18 +0000272 __func__, ds_idx);
Tom Haynesf54bcf22014-12-11 15:34:59 -0500273 pnfs_generic_mark_devid_invalid(devid);
Trond Myklebust52b26a32013-09-26 14:08:36 -0400274 goto out;
Fred Isamancfe7f412011-03-01 01:34:18 +0000275 }
Trond Myklebustacd65e52013-09-26 14:32:56 -0400276 smp_rmb();
Andy Adamsonc23266d2013-05-08 16:21:18 -0400277 if (ds->ds_clp)
Trond Myklebust52b26a32013-09-26 14:08:36 -0400278 goto out_test_devid;
Fred Isamancfe7f412011-03-01 01:34:18 +0000279
Peng Tao7405f9e2014-05-29 21:06:58 +0800280 nfs4_pnfs_ds_connect(s, ds, devid, dataserver_timeo,
Peng Tao30626f92014-05-30 18:15:58 +0800281 dataserver_retrans, 4,
Anna Schumaker7d38de32016-11-17 15:15:55 -0500282 s->nfs_client->cl_minorversion);
Fred Isamancfe7f412011-03-01 01:34:18 +0000283
Trond Myklebust52b26a32013-09-26 14:08:36 -0400284out_test_devid:
285 if (filelayout_test_devid_unavailable(devid))
286 ret = NULL;
287out:
288 return ret;
Fred Isamancfe7f412011-03-01 01:34:18 +0000289}
Andy Adamson98fc6852012-04-27 17:53:45 -0400290
291module_param(dataserver_retrans, uint, 0644);
292MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client "
293 "retries a request before it attempts further "
294 " recovery action.");
295module_param(dataserver_timeo, uint, 0644);
296MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the "
297 "NFSv4.1 client waits for a response from a "
298 " data server before it retries an NFS request.");