blob: ed388aae96893628dfcecb25743acac320b8a1b4 [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>
33
34#include "internal.h"
35#include "nfs4filelayout.h"
36
37#define NFSDBG_FACILITY NFSDBG_PNFS_LD
38
39/*
40 * Data server cache
41 *
42 * Data servers can be mapped to different device ids.
43 * nfs4_pnfs_ds reference counting
44 * - set to 1 on allocation
45 * - incremented when a device id maps a data server already in the cache.
46 * - decremented when deviceid is removed from the cache.
47 */
48DEFINE_SPINLOCK(nfs4_ds_cache_lock);
49static LIST_HEAD(nfs4_data_server_cache);
50
51/* Debug routines */
52void
53print_ds(struct nfs4_pnfs_ds *ds)
54{
55 if (ds == NULL) {
56 printk("%s NULL device\n", __func__);
57 return;
58 }
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040059 printk(" ds %s\n"
Andy Adamson16b374c2010-10-20 00:18:04 -040060 " ref count %d\n"
61 " client %p\n"
62 " cl_exchange_flags %x\n",
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040063 ds->ds_remotestr,
Andy Adamson16b374c2010-10-20 00:18:04 -040064 atomic_read(&ds->ds_count), ds->ds_clp,
65 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
66}
67
Weston Andros Adamson14f9a602011-05-31 18:48:57 -040068static bool
69same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
Andy Adamson16b374c2010-10-20 00:18:04 -040070{
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040071 struct sockaddr_in *a, *b;
72 struct sockaddr_in6 *a6, *b6;
Andy Adamson16b374c2010-10-20 00:18:04 -040073
Weston Andros Adamson14f9a602011-05-31 18:48:57 -040074 if (addr1->sa_family != addr2->sa_family)
75 return false;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040076
Weston Andros Adamson14f9a602011-05-31 18:48:57 -040077 switch (addr1->sa_family) {
78 case AF_INET:
79 a = (struct sockaddr_in *)addr1;
80 b = (struct sockaddr_in *)addr2;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040081
Weston Andros Adamson14f9a602011-05-31 18:48:57 -040082 if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
83 a->sin_port == b->sin_port)
84 return true;
85 break;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040086
Weston Andros Adamson14f9a602011-05-31 18:48:57 -040087 case AF_INET6:
88 a6 = (struct sockaddr_in6 *)addr1;
89 b6 = (struct sockaddr_in6 *)addr2;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040090
Weston Andros Adamson14f9a602011-05-31 18:48:57 -040091 /* LINKLOCAL addresses must have matching scope_id */
92 if (ipv6_addr_scope(&a6->sin6_addr) ==
93 IPV6_ADDR_SCOPE_LINKLOCAL &&
94 a6->sin6_scope_id != b6->sin6_scope_id)
95 return false;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040096
Weston Andros Adamson14f9a602011-05-31 18:48:57 -040097 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
98 a6->sin6_port == b6->sin6_port)
99 return true;
100 break;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400101
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400102 default:
103 dprintk("%s: unhandled address family: %u\n",
104 __func__, addr1->sa_family);
105 return false;
106 }
107
108 return false;
109}
110
111/*
112 * Lookup DS by addresses. The first matching address returns true.
113 * nfs4_ds_cache_lock is held
114 */
115static struct nfs4_pnfs_ds *
116_data_server_lookup_locked(struct list_head *dsaddrs)
117{
118 struct nfs4_pnfs_ds *ds;
119 struct nfs4_pnfs_ds_addr *da1, *da2;
120
121 list_for_each_entry(da1, dsaddrs, da_node) {
122 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) {
123 list_for_each_entry(da2, &ds->ds_addrs, da_node) {
124 if (same_sockaddr(
125 (struct sockaddr *)&da1->da_addr,
126 (struct sockaddr *)&da2->da_addr))
127 return ds;
128 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400129 }
130 }
131 return NULL;
132}
133
Andy Adamsond83217c2011-03-01 01:34:17 +0000134/*
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400135 * Compare two lists of addresses.
136 */
137static bool
138_data_server_match_all_addrs_locked(struct list_head *dsaddrs1,
139 struct list_head *dsaddrs2)
140{
141 struct nfs4_pnfs_ds_addr *da1, *da2;
142 size_t count1 = 0,
143 count2 = 0;
144
145 list_for_each_entry(da1, dsaddrs1, da_node)
146 count1++;
147
148 list_for_each_entry(da2, dsaddrs2, da_node) {
149 bool found = false;
150 count2++;
151 list_for_each_entry(da1, dsaddrs1, da_node) {
152 if (same_sockaddr((struct sockaddr *)&da1->da_addr,
153 (struct sockaddr *)&da2->da_addr)) {
154 found = true;
155 break;
156 }
157 }
158 if (!found)
159 return false;
160 }
161
162 return (count1 == count2);
163}
164
165/*
Andy Adamsond83217c2011-03-01 01:34:17 +0000166 * Create an rpc connection to the nfs4_pnfs_ds data server
Weston Andros Adamson35dbbc92011-06-01 16:32:21 -0400167 * Currently only supports IPv4 and IPv6 addresses
Andy Adamsond83217c2011-03-01 01:34:17 +0000168 */
169static int
170nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
171{
Weston Andros Adamson7e574f02011-05-31 18:48:58 -0400172 struct nfs_client *clp = ERR_PTR(-EIO);
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400173 struct nfs4_pnfs_ds_addr *da;
Andy Adamsond83217c2011-03-01 01:34:17 +0000174 int status = 0;
175
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400176 dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr,
Andy Adamsond83217c2011-03-01 01:34:17 +0000177 mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor);
178
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400179 BUG_ON(list_empty(&ds->ds_addrs));
180
Weston Andros Adamson7e574f02011-05-31 18:48:58 -0400181 list_for_each_entry(da, &ds->ds_addrs, da_node) {
182 dprintk("%s: DS %s: trying address %s\n",
183 __func__, ds->ds_remotestr, da->da_remotestr);
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400184
Weston Andros Adamson7e574f02011-05-31 18:48:58 -0400185 clp = nfs4_set_ds_client(mds_srv->nfs_client,
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400186 (struct sockaddr *)&da->da_addr,
187 da->da_addrlen, IPPROTO_TCP);
Weston Andros Adamson7e574f02011-05-31 18:48:58 -0400188 if (!IS_ERR(clp))
189 break;
190 }
191
Andy Adamsond83217c2011-03-01 01:34:17 +0000192 if (IS_ERR(clp)) {
193 status = PTR_ERR(clp);
194 goto out;
195 }
196
197 if ((clp->cl_exchange_flags & EXCHGID4_FLAG_MASK_PNFS) != 0) {
198 if (!is_ds_client(clp)) {
199 status = -ENODEV;
200 goto out_put;
201 }
202 ds->ds_clp = clp;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400203 dprintk("%s [existing] server=%s\n", __func__,
204 ds->ds_remotestr);
Andy Adamsond83217c2011-03-01 01:34:17 +0000205 goto out;
206 }
207
208 /*
209 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the DS lease to
210 * be equal to the MDS lease. Renewal is scheduled in create_session.
211 */
212 spin_lock(&mds_srv->nfs_client->cl_lock);
213 clp->cl_lease_time = mds_srv->nfs_client->cl_lease_time;
214 spin_unlock(&mds_srv->nfs_client->cl_lock);
215 clp->cl_last_renewal = jiffies;
216
217 /* New nfs_client */
218 status = nfs4_init_ds_session(clp);
219 if (status)
220 goto out_put;
221
222 ds->ds_clp = clp;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400223 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
Andy Adamsond83217c2011-03-01 01:34:17 +0000224out:
225 return status;
226out_put:
227 nfs_put_client(clp);
228 goto out;
229}
230
Andy Adamson16b374c2010-10-20 00:18:04 -0400231static void
232destroy_ds(struct nfs4_pnfs_ds *ds)
233{
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400234 struct nfs4_pnfs_ds_addr *da;
235
Andy Adamson16b374c2010-10-20 00:18:04 -0400236 dprintk("--> %s\n", __func__);
237 ifdebug(FACILITY)
238 print_ds(ds);
239
240 if (ds->ds_clp)
241 nfs_put_client(ds->ds_clp);
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400242
243 while (!list_empty(&ds->ds_addrs)) {
244 da = list_first_entry(&ds->ds_addrs,
245 struct nfs4_pnfs_ds_addr,
246 da_node);
247 list_del_init(&da->da_node);
248 kfree(da->da_remotestr);
249 kfree(da);
250 }
251
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400252 kfree(ds->ds_remotestr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400253 kfree(ds);
254}
255
Benny Halevy1775bc32011-05-20 13:47:33 +0200256void
Andy Adamson16b374c2010-10-20 00:18:04 -0400257nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
258{
259 struct nfs4_pnfs_ds *ds;
260 int i;
261
Benny Halevya1eaecb2011-05-19 22:14:47 -0400262 nfs4_print_deviceid(&dsaddr->id_node.deviceid);
Andy Adamson16b374c2010-10-20 00:18:04 -0400263
264 for (i = 0; i < dsaddr->ds_num; i++) {
265 ds = dsaddr->ds_list[i];
266 if (ds != NULL) {
267 if (atomic_dec_and_lock(&ds->ds_count,
268 &nfs4_ds_cache_lock)) {
269 list_del_init(&ds->ds_node);
270 spin_unlock(&nfs4_ds_cache_lock);
271 destroy_ds(ds);
272 }
273 }
274 }
275 kfree(dsaddr->stripe_indices);
276 kfree(dsaddr);
277}
278
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400279/*
280 * Create a string with a human readable address and port to avoid
281 * complicated setup around many dprinks.
282 */
283static char *
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400284nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400285{
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400286 struct nfs4_pnfs_ds_addr *da;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400287 char *remotestr;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400288 size_t len;
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400289 char *p;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400290
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400291 len = 3; /* '{', '}' and eol */
292 list_for_each_entry(da, dsaddrs, da_node) {
293 len += strlen(da->da_remotestr) + 1; /* string plus comma */
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400294 }
295
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400296 remotestr = kzalloc(len, gfp_flags);
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400297 if (!remotestr)
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400298 return NULL;
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400299
300 p = remotestr;
301 *(p++) = '{';
302 len--;
303 list_for_each_entry(da, dsaddrs, da_node) {
304 size_t ll = strlen(da->da_remotestr);
305
306 if (ll > len)
307 goto out_err;
308
309 memcpy(p, da->da_remotestr, ll);
310 p += ll;
311 len -= ll;
312
313 if (len < 1)
314 goto out_err;
315 (*p++) = ',';
316 len--;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400317 }
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400318 if (len < 2)
319 goto out_err;
320 *(p++) = '}';
321 *p = '\0';
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400322 return remotestr;
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400323out_err:
324 kfree(remotestr);
325 return NULL;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400326}
327
328static struct nfs4_pnfs_ds *
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400329nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400330{
331 struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
332 char *remotestr;
Andy Adamson16b374c2010-10-20 00:18:04 -0400333
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400334 if (list_empty(dsaddrs)) {
335 dprintk("%s: no addresses defined\n", __func__);
336 goto out;
337 }
338
339 ds = kzalloc(sizeof(*ds), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400340 if (!ds)
341 goto out;
342
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400343 /* this is only used for debugging, so it's ok if its NULL */
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400344 remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400345
Andy Adamson16b374c2010-10-20 00:18:04 -0400346 spin_lock(&nfs4_ds_cache_lock);
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400347 tmp_ds = _data_server_lookup_locked(dsaddrs);
Andy Adamson16b374c2010-10-20 00:18:04 -0400348 if (tmp_ds == NULL) {
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400349 INIT_LIST_HEAD(&ds->ds_addrs);
350 list_splice_init(dsaddrs, &ds->ds_addrs);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400351 ds->ds_remotestr = remotestr;
Andy Adamson16b374c2010-10-20 00:18:04 -0400352 atomic_set(&ds->ds_count, 1);
353 INIT_LIST_HEAD(&ds->ds_node);
354 ds->ds_clp = NULL;
355 list_add(&ds->ds_node, &nfs4_data_server_cache);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400356 dprintk("%s add new data server %s\n", __func__,
357 ds->ds_remotestr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400358 } else {
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400359 if (!_data_server_match_all_addrs_locked(&tmp_ds->ds_addrs,
360 dsaddrs)) {
361 dprintk("%s: multipath address mismatch: %s != %s",
362 __func__, tmp_ds->ds_remotestr, remotestr);
363 }
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400364 kfree(remotestr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400365 kfree(ds);
366 atomic_inc(&tmp_ds->ds_count);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400367 dprintk("%s data server %s found, inc'ed ds_count to %d\n",
368 __func__, tmp_ds->ds_remotestr,
Andy Adamson16b374c2010-10-20 00:18:04 -0400369 atomic_read(&tmp_ds->ds_count));
370 ds = tmp_ds;
371 }
372 spin_unlock(&nfs4_ds_cache_lock);
373out:
374 return ds;
375}
376
377/*
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400378 * Currently only supports ipv4, ipv6 and one multi-path address.
Andy Adamson16b374c2010-10-20 00:18:04 -0400379 */
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400380static struct nfs4_pnfs_ds_addr *
381decode_ds_addr(struct xdr_stream *streamp, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400382{
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400383 struct nfs4_pnfs_ds_addr *da = NULL;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400384 char *buf, *portstr;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400385 u32 port;
386 int nlen, rlen;
Andy Adamson16b374c2010-10-20 00:18:04 -0400387 int tmp[2];
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400388 __be32 *p;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400389 char *netid, *match_netid;
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400390 size_t len, match_netid_len;
391 char *startsep = "";
392 char *endsep = "";
393
Andy Adamson16b374c2010-10-20 00:18:04 -0400394
395 /* r_netid */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400396 p = xdr_inline_decode(streamp, 4);
397 if (unlikely(!p))
398 goto out_err;
Andy Adamson16b374c2010-10-20 00:18:04 -0400399 nlen = be32_to_cpup(p++);
Andy Adamson16b374c2010-10-20 00:18:04 -0400400
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400401 p = xdr_inline_decode(streamp, nlen);
402 if (unlikely(!p))
403 goto out_err;
Andy Adamson16b374c2010-10-20 00:18:04 -0400404
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400405 netid = kmalloc(nlen+1, gfp_flags);
406 if (unlikely(!netid))
Andy Adamson16b374c2010-10-20 00:18:04 -0400407 goto out_err;
Andy Adamson16b374c2010-10-20 00:18:04 -0400408
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400409 netid[nlen] = '\0';
410 memcpy(netid, p, nlen);
411
412 /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400413 p = xdr_inline_decode(streamp, 4);
414 if (unlikely(!p))
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400415 goto out_free_netid;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400416 rlen = be32_to_cpup(p);
417
418 p = xdr_inline_decode(streamp, rlen);
419 if (unlikely(!p))
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400420 goto out_free_netid;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400421
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400422 /* port is ".ABC.DEF", 8 chars max */
423 if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
Jesper Juhlad3d2ee2011-01-17 18:41:50 +0000424 dprintk("%s: Invalid address, length %d\n", __func__,
Andy Adamson16b374c2010-10-20 00:18:04 -0400425 rlen);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400426 goto out_free_netid;
Andy Adamson16b374c2010-10-20 00:18:04 -0400427 }
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400428 buf = kmalloc(rlen + 1, gfp_flags);
Stanislav Fomichevb9f81052011-02-05 23:13:01 +0000429 if (!buf) {
430 dprintk("%s: Not enough memory\n", __func__);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400431 goto out_free_netid;
Stanislav Fomichevb9f81052011-02-05 23:13:01 +0000432 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400433 buf[rlen] = '\0';
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400434 memcpy(buf, p, rlen);
Andy Adamson16b374c2010-10-20 00:18:04 -0400435
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400436 /* replace port '.' with '-' */
437 portstr = strrchr(buf, '.');
438 if (!portstr) {
439 dprintk("%s: Failed finding expected dot in port\n",
440 __func__);
441 goto out_free_buf;
442 }
443 *portstr = '-';
444
445 /* find '.' between address and port */
446 portstr = strrchr(buf, '.');
447 if (!portstr) {
448 dprintk("%s: Failed finding expected dot between address and "
449 "port\n", __func__);
450 goto out_free_buf;
451 }
452 *portstr = '\0';
453
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400454 da = kzalloc(sizeof(*da), gfp_flags);
455 if (unlikely(!da))
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400456 goto out_free_buf;
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400457
458 INIT_LIST_HEAD(&da->da_node);
459
460 if (!rpc_pton(buf, portstr-buf, (struct sockaddr *)&da->da_addr,
461 sizeof(da->da_addr))) {
462 dprintk("%s: error parsing address %s\n", __func__, buf);
463 goto out_free_da;
Andy Adamson16b374c2010-10-20 00:18:04 -0400464 }
465
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400466 portstr++;
467 sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
Andy Adamson16b374c2010-10-20 00:18:04 -0400468 port = htons((tmp[0] << 8) | (tmp[1]));
469
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400470 switch (da->da_addr.ss_family) {
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400471 case AF_INET:
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400472 ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
473 da->da_addrlen = sizeof(struct sockaddr_in);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400474 match_netid = "tcp";
475 match_netid_len = 3;
476 break;
477
478 case AF_INET6:
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400479 ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
480 da->da_addrlen = sizeof(struct sockaddr_in6);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400481 match_netid = "tcp6";
482 match_netid_len = 4;
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400483 startsep = "[";
484 endsep = "]";
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400485 break;
486
487 default:
488 dprintk("%s: unsupported address family: %u\n",
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400489 __func__, da->da_addr.ss_family);
490 goto out_free_da;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400491 }
492
493 if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
494 dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
495 __func__, netid, match_netid);
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400496 goto out_free_da;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400497 }
498
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400499 /* save human readable address */
500 len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
501 da->da_remotestr = kzalloc(len, gfp_flags);
502
503 /* NULL is ok, only used for dprintk */
504 if (da->da_remotestr)
505 snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
506 buf, endsep, ntohs(port));
507
508 dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
509 kfree(buf);
510 kfree(netid);
511 return da;
512
513out_free_da:
514 kfree(da);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400515out_free_buf:
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400516 dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
Andy Adamson16b374c2010-10-20 00:18:04 -0400517 kfree(buf);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400518out_free_netid:
519 kfree(netid);
Andy Adamson16b374c2010-10-20 00:18:04 -0400520out_err:
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400521 return NULL;
Andy Adamson16b374c2010-10-20 00:18:04 -0400522}
523
524/* Decode opaque device data and return the result */
525static struct nfs4_file_layout_dsaddr*
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400526decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400527{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400528 int i;
Andy Adamson16b374c2010-10-20 00:18:04 -0400529 u32 cnt, num;
530 u8 *indexp;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400531 __be32 *p;
532 u8 *stripe_indices;
533 u8 max_stripe_index;
534 struct nfs4_file_layout_dsaddr *dsaddr = NULL;
535 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400536 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400537 struct page *scratch;
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400538 struct list_head dsaddrs;
539 struct nfs4_pnfs_ds_addr *da;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400540
541 /* set up xdr stream */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400542 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400543 if (!scratch)
544 goto out_err;
545
Benny Halevyf7da7a12011-05-19 14:16:47 -0400546 xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400547 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
Andy Adamson16b374c2010-10-20 00:18:04 -0400548
549 /* Get the stripe count (number of stripe index) */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400550 p = xdr_inline_decode(&stream, 4);
551 if (unlikely(!p))
552 goto out_err_free_scratch;
553
554 cnt = be32_to_cpup(p);
Andy Adamson16b374c2010-10-20 00:18:04 -0400555 dprintk("%s stripe count %d\n", __func__, cnt);
556 if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
557 printk(KERN_WARNING "%s: stripe count %d greater than "
558 "supported maximum %d\n", __func__,
559 cnt, NFS4_PNFS_MAX_STRIPE_CNT);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400560 goto out_err_free_scratch;
561 }
562
563 /* read stripe indices */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400564 stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400565 if (!stripe_indices)
566 goto out_err_free_scratch;
567
568 p = xdr_inline_decode(&stream, cnt << 2);
569 if (unlikely(!p))
570 goto out_err_free_stripe_indices;
571
572 indexp = &stripe_indices[0];
573 max_stripe_index = 0;
574 for (i = 0; i < cnt; i++) {
575 *indexp = be32_to_cpup(p++);
576 max_stripe_index = max(max_stripe_index, *indexp);
577 indexp++;
Andy Adamson16b374c2010-10-20 00:18:04 -0400578 }
579
580 /* Check the multipath list count */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400581 p = xdr_inline_decode(&stream, 4);
582 if (unlikely(!p))
583 goto out_err_free_stripe_indices;
584
585 num = be32_to_cpup(p);
Andy Adamson16b374c2010-10-20 00:18:04 -0400586 dprintk("%s ds_num %u\n", __func__, num);
587 if (num > NFS4_PNFS_MAX_MULTI_CNT) {
588 printk(KERN_WARNING "%s: multipath count %d greater than "
589 "supported maximum %d\n", __func__,
590 num, NFS4_PNFS_MAX_MULTI_CNT);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400591 goto out_err_free_stripe_indices;
Andy Adamson16b374c2010-10-20 00:18:04 -0400592 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400593
594 /* validate stripe indices are all < num */
595 if (max_stripe_index >= num) {
596 printk(KERN_WARNING "%s: stripe index %u >= num ds %u\n",
597 __func__, max_stripe_index, num);
598 goto out_err_free_stripe_indices;
599 }
600
Andy Adamson16b374c2010-10-20 00:18:04 -0400601 dsaddr = kzalloc(sizeof(*dsaddr) +
602 (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400603 gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400604 if (!dsaddr)
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400605 goto out_err_free_stripe_indices;
Andy Adamson16b374c2010-10-20 00:18:04 -0400606
607 dsaddr->stripe_count = cnt;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400608 dsaddr->stripe_indices = stripe_indices;
609 stripe_indices = NULL;
Andy Adamson16b374c2010-10-20 00:18:04 -0400610 dsaddr->ds_num = num;
Benny Halevy1775bc32011-05-20 13:47:33 +0200611 nfs4_init_deviceid_node(&dsaddr->id_node,
612 NFS_SERVER(ino)->pnfs_curr_ld,
613 NFS_SERVER(ino)->nfs_client,
Benny Halevya1eaecb2011-05-19 22:14:47 -0400614 &pdev->dev_id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400615
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400616 INIT_LIST_HEAD(&dsaddrs);
617
Andy Adamson16b374c2010-10-20 00:18:04 -0400618 for (i = 0; i < dsaddr->ds_num; i++) {
619 int j;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400620 u32 mp_count;
Andy Adamson16b374c2010-10-20 00:18:04 -0400621
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400622 p = xdr_inline_decode(&stream, 4);
623 if (unlikely(!p))
624 goto out_err_free_deviceid;
625
626 mp_count = be32_to_cpup(p); /* multipath count */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400627 for (j = 0; j < mp_count; j++) {
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400628 da = decode_ds_addr(&stream, gfp_flags);
629 if (da)
630 list_add_tail(&da->da_node, &dsaddrs);
631 }
632 if (list_empty(&dsaddrs)) {
633 dprintk("%s: no suitable DS addresses found\n",
634 __func__);
635 goto out_err_free_deviceid;
636 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400637
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400638 dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
639 if (!dsaddr->ds_list[i])
640 goto out_err_drain_dsaddrs;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400641
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400642 /* If DS was already in cache, free ds addrs */
643 while (!list_empty(&dsaddrs)) {
644 da = list_first_entry(&dsaddrs,
645 struct nfs4_pnfs_ds_addr,
646 da_node);
647 list_del_init(&da->da_node);
648 kfree(da->da_remotestr);
649 kfree(da);
Andy Adamson16b374c2010-10-20 00:18:04 -0400650 }
651 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400652
653 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400654 return dsaddr;
655
Weston Andros Adamson14f9a602011-05-31 18:48:57 -0400656out_err_drain_dsaddrs:
657 while (!list_empty(&dsaddrs)) {
658 da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
659 da_node);
660 list_del_init(&da->da_node);
661 kfree(da->da_remotestr);
662 kfree(da);
663 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400664out_err_free_deviceid:
Andy Adamson16b374c2010-10-20 00:18:04 -0400665 nfs4_fl_free_deviceid(dsaddr);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400666 /* stripe_indicies was part of dsaddr */
667 goto out_err_free_scratch;
668out_err_free_stripe_indices:
669 kfree(stripe_indices);
670out_err_free_scratch:
671 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400672out_err:
673 dprintk("%s ERROR: returning NULL\n", __func__);
674 return NULL;
675}
676
677/*
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000678 * Decode the opaque device specified in 'dev' and add it to the cache of
679 * available devices.
Andy Adamson16b374c2010-10-20 00:18:04 -0400680 */
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000681static struct nfs4_file_layout_dsaddr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400682decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400683{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400684 struct nfs4_deviceid_node *d;
685 struct nfs4_file_layout_dsaddr *n, *new;
Andy Adamson16b374c2010-10-20 00:18:04 -0400686
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400687 new = decode_device(inode, dev, gfp_flags);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000688 if (!new) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400689 printk(KERN_WARNING "%s: Could not decode or add device\n",
690 __func__);
691 return NULL;
692 }
693
Benny Halevya1eaecb2011-05-19 22:14:47 -0400694 d = nfs4_insert_deviceid_node(&new->id_node);
695 n = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
696 if (n != new) {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000697 nfs4_fl_free_deviceid(new);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400698 return n;
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000699 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400700
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000701 return new;
Andy Adamson16b374c2010-10-20 00:18:04 -0400702}
703
704/*
705 * Retrieve the information for dev_id, add it to the list
706 * of available devices, and return it.
707 */
708struct nfs4_file_layout_dsaddr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400709get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400710{
711 struct pnfs_device *pdev = NULL;
712 u32 max_resp_sz;
713 int max_pages;
714 struct page **pages = NULL;
715 struct nfs4_file_layout_dsaddr *dsaddr = NULL;
716 int rc, i;
717 struct nfs_server *server = NFS_SERVER(inode);
718
719 /*
720 * Use the session max response size as the basis for setting
721 * GETDEVICEINFO's maxcount
722 */
723 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
724 max_pages = max_resp_sz >> PAGE_SHIFT;
725 dprintk("%s inode %p max_resp_sz %u max_pages %d\n",
726 __func__, inode, max_resp_sz, max_pages);
727
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400728 pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400729 if (pdev == NULL)
730 return NULL;
731
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400732 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400733 if (pages == NULL) {
734 kfree(pdev);
735 return NULL;
736 }
737 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400738 pages[i] = alloc_page(gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400739 if (!pages[i])
740 goto out_free;
741 }
742
Andy Adamson16b374c2010-10-20 00:18:04 -0400743 memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
744 pdev->layout_type = LAYOUT_NFSV4_1_FILES;
745 pdev->pages = pages;
746 pdev->pgbase = 0;
747 pdev->pglen = PAGE_SIZE * max_pages;
748 pdev->mincount = 0;
749
750 rc = nfs4_proc_getdeviceinfo(server, pdev);
751 dprintk("%s getdevice info returns %d\n", __func__, rc);
752 if (rc)
753 goto out_free;
754
755 /*
756 * Found new device, need to decode it and then add it to the
757 * list of known devices for this mountpoint.
758 */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400759 dsaddr = decode_and_add_device(inode, pdev, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400760out_free:
Andy Adamson16b374c2010-10-20 00:18:04 -0400761 for (i = 0; i < max_pages; i++)
762 __free_page(pages[i]);
763 kfree(pages);
764 kfree(pdev);
765 dprintk("<-- %s dsaddr %p\n", __func__, dsaddr);
766 return dsaddr;
767}
768
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000769void
770nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
Andy Adamson16b374c2010-10-20 00:18:04 -0400771{
Benny Halevy1775bc32011-05-20 13:47:33 +0200772 nfs4_put_deviceid_node(&dsaddr->id_node);
Andy Adamson16b374c2010-10-20 00:18:04 -0400773}
Fred Isamancfe7f412011-03-01 01:34:18 +0000774
775/*
776 * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
777 * Then: ((res + fsi) % dsaddr->stripe_count)
778 */
779u32
780nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
781{
782 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
783 u64 tmp;
784
785 tmp = offset - flseg->pattern_offset;
786 do_div(tmp, flseg->stripe_unit);
787 tmp += flseg->first_stripe_index;
788 return do_div(tmp, flseg->dsaddr->stripe_count);
789}
790
791u32
792nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
793{
794 return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
795}
796
797struct nfs_fh *
798nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
799{
800 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
801 u32 i;
802
803 if (flseg->stripe_type == STRIPE_SPARSE) {
804 if (flseg->num_fh == 1)
805 i = 0;
806 else if (flseg->num_fh == 0)
807 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
808 return NULL;
809 else
810 i = nfs4_fl_calc_ds_index(lseg, j);
811 } else
812 i = j;
813 return flseg->fh_array[i];
814}
815
Andy Adamson568e8c42011-03-01 01:34:22 +0000816static void
817filelayout_mark_devid_negative(struct nfs4_file_layout_dsaddr *dsaddr,
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400818 int err, const char *ds_remotestr)
Andy Adamson568e8c42011-03-01 01:34:22 +0000819{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400820 u32 *p = (u32 *)&dsaddr->id_node.deviceid;
Andy Adamson568e8c42011-03-01 01:34:22 +0000821
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400822 printk(KERN_ERR "NFS: data server %s connection error %d."
Andy Adamson568e8c42011-03-01 01:34:22 +0000823 " Deviceid [%x%x%x%x] marked out of use.\n",
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400824 ds_remotestr, err, p[0], p[1], p[2], p[3]);
Andy Adamson568e8c42011-03-01 01:34:22 +0000825
Benny Halevya1eaecb2011-05-19 22:14:47 -0400826 spin_lock(&nfs4_ds_cache_lock);
Andy Adamson568e8c42011-03-01 01:34:22 +0000827 dsaddr->flags |= NFS4_DEVICE_ID_NEG_ENTRY;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400828 spin_unlock(&nfs4_ds_cache_lock);
Andy Adamson568e8c42011-03-01 01:34:22 +0000829}
830
Fred Isamancfe7f412011-03-01 01:34:18 +0000831struct nfs4_pnfs_ds *
832nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
833{
834 struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
835 struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
836
837 if (ds == NULL) {
838 printk(KERN_ERR "%s: No data server for offset index %d\n",
839 __func__, ds_idx);
840 return NULL;
841 }
842
843 if (!ds->ds_clp) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000844 struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
Fred Isamancfe7f412011-03-01 01:34:18 +0000845 int err;
846
Andy Adamson568e8c42011-03-01 01:34:22 +0000847 if (dsaddr->flags & NFS4_DEVICE_ID_NEG_ENTRY) {
848 /* Already tried to connect, don't try again */
849 dprintk("%s Deviceid marked out of use\n", __func__);
850 return NULL;
851 }
852 err = nfs4_ds_connect(s, ds);
Fred Isamancfe7f412011-03-01 01:34:18 +0000853 if (err) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000854 filelayout_mark_devid_negative(dsaddr, err,
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400855 ds->ds_remotestr);
Fred Isamancfe7f412011-03-01 01:34:18 +0000856 return NULL;
857 }
858 }
859 return ds;
860}