blob: 1a8308b901088c02df3a4288939b656702763827 [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
Andy Adamson16b374c2010-10-20 00:18:04 -040068/* nfs4_ds_cache_lock is held */
69static struct nfs4_pnfs_ds *
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040070_data_server_lookup_locked(struct sockaddr *addr, size_t addrlen)
Andy Adamson16b374c2010-10-20 00:18:04 -040071{
72 struct nfs4_pnfs_ds *ds;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040073 struct sockaddr_in *a, *b;
74 struct sockaddr_in6 *a6, *b6;
Andy Adamson16b374c2010-10-20 00:18:04 -040075
76 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) {
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -040077 if (addr->sa_family != ds->ds_addr.ss_family)
78 continue;
79
80 switch (addr->sa_family) {
81 case AF_INET:
82 a = (struct sockaddr_in *)addr;
83 b = (struct sockaddr_in *)&ds->ds_addr;
84
85 if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
86 a->sin_port == b->sin_port)
87 return ds;
88 break;
89
90 case AF_INET6:
91 a6 = (struct sockaddr_in6 *)addr;
92 b6 = (struct sockaddr_in6 *)&ds->ds_addr;
93
94 /* LINKLOCAL addresses must have matching scope_id */
95 if (ipv6_addr_scope(&a6->sin6_addr) ==
96 IPV6_ADDR_SCOPE_LINKLOCAL &&
97 a6->sin6_scope_id != b6->sin6_scope_id)
98 continue;
99
100 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
101 a6->sin6_port == b6->sin6_port)
102 return ds;
103 break;
104
105 default:
106 dprintk("%s: unhandled address family: %u\n",
107 __func__, addr->sa_family);
108 return NULL;
Andy Adamson16b374c2010-10-20 00:18:04 -0400109 }
110 }
111 return NULL;
112}
113
Andy Adamsond83217c2011-03-01 01:34:17 +0000114/*
115 * Create an rpc connection to the nfs4_pnfs_ds data server
116 * Currently only support IPv4
117 */
118static int
119nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
120{
121 struct nfs_client *clp;
Andy Adamsond83217c2011-03-01 01:34:17 +0000122 int status = 0;
123
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400124 dprintk("--> %s addr %s au_flavor %d\n", __func__, ds->ds_remotestr,
Andy Adamsond83217c2011-03-01 01:34:17 +0000125 mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor);
126
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400127 clp = nfs4_set_ds_client(mds_srv->nfs_client,
128 (struct sockaddr *)&ds->ds_addr,
129 ds->ds_addrlen, IPPROTO_TCP);
Andy Adamsond83217c2011-03-01 01:34:17 +0000130 if (IS_ERR(clp)) {
131 status = PTR_ERR(clp);
132 goto out;
133 }
134
135 if ((clp->cl_exchange_flags & EXCHGID4_FLAG_MASK_PNFS) != 0) {
136 if (!is_ds_client(clp)) {
137 status = -ENODEV;
138 goto out_put;
139 }
140 ds->ds_clp = clp;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400141 dprintk("%s [existing] server=%s\n", __func__,
142 ds->ds_remotestr);
Andy Adamsond83217c2011-03-01 01:34:17 +0000143 goto out;
144 }
145
146 /*
147 * Do not set NFS_CS_CHECK_LEASE_TIME instead set the DS lease to
148 * be equal to the MDS lease. Renewal is scheduled in create_session.
149 */
150 spin_lock(&mds_srv->nfs_client->cl_lock);
151 clp->cl_lease_time = mds_srv->nfs_client->cl_lease_time;
152 spin_unlock(&mds_srv->nfs_client->cl_lock);
153 clp->cl_last_renewal = jiffies;
154
155 /* New nfs_client */
156 status = nfs4_init_ds_session(clp);
157 if (status)
158 goto out_put;
159
160 ds->ds_clp = clp;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400161 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
Andy Adamsond83217c2011-03-01 01:34:17 +0000162out:
163 return status;
164out_put:
165 nfs_put_client(clp);
166 goto out;
167}
168
Andy Adamson16b374c2010-10-20 00:18:04 -0400169static void
170destroy_ds(struct nfs4_pnfs_ds *ds)
171{
172 dprintk("--> %s\n", __func__);
173 ifdebug(FACILITY)
174 print_ds(ds);
175
176 if (ds->ds_clp)
177 nfs_put_client(ds->ds_clp);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400178 kfree(ds->ds_remotestr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400179 kfree(ds);
180}
181
Benny Halevy1775bc32011-05-20 13:47:33 +0200182void
Andy Adamson16b374c2010-10-20 00:18:04 -0400183nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
184{
185 struct nfs4_pnfs_ds *ds;
186 int i;
187
Benny Halevya1eaecb2011-05-19 22:14:47 -0400188 nfs4_print_deviceid(&dsaddr->id_node.deviceid);
Andy Adamson16b374c2010-10-20 00:18:04 -0400189
190 for (i = 0; i < dsaddr->ds_num; i++) {
191 ds = dsaddr->ds_list[i];
192 if (ds != NULL) {
193 if (atomic_dec_and_lock(&ds->ds_count,
194 &nfs4_ds_cache_lock)) {
195 list_del_init(&ds->ds_node);
196 spin_unlock(&nfs4_ds_cache_lock);
197 destroy_ds(ds);
198 }
199 }
200 }
201 kfree(dsaddr->stripe_indices);
202 kfree(dsaddr);
203}
204
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400205/*
206 * Create a string with a human readable address and port to avoid
207 * complicated setup around many dprinks.
208 */
209static char *
210nfs4_pnfs_remotestr(struct sockaddr *ds_addr, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400211{
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400212 char buf[INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN];
213 char *remotestr;
214 char *startsep = "";
215 char *endsep = "";
216 size_t len;
217 uint16_t port;
218
219 switch (ds_addr->sa_family) {
220 case AF_INET:
221 port = ((struct sockaddr_in *)ds_addr)->sin_port;
222 break;
223 case AF_INET6:
224 startsep = "[";
225 endsep = "]";
226 port = ((struct sockaddr_in6 *)ds_addr)->sin6_port;
227 break;
228 default:
229 dprintk("%s: Unknown address family %u\n",
230 __func__, ds_addr->sa_family);
231 return NULL;
232 }
233
234 if (!rpc_ntop((struct sockaddr *)ds_addr, buf, sizeof(buf))) {
235 dprintk("%s: error printing addr\n", __func__);
236 return NULL;
237 }
238
239 len = strlen(buf) + strlen(startsep) + strlen(endsep) + 1 + 5 + 1;
240 remotestr = kzalloc(len, gfp_flags);
241
242 if (unlikely(!remotestr)) {
243 dprintk("%s: couldn't alloc remotestr\n", __func__);
244 return NULL;
245 }
246
247 snprintf(remotestr, len, "%s%s%s:%u",
248 startsep, buf, endsep, ntohs(port));
249
250 return remotestr;
251}
252
253static struct nfs4_pnfs_ds *
254nfs4_pnfs_ds_add(struct sockaddr *addr, size_t addrlen, gfp_t gfp_flags)
255{
256 struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
257 char *remotestr;
Andy Adamson16b374c2010-10-20 00:18:04 -0400258
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400259 ds = kzalloc(sizeof(*tmp_ds), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400260 if (!ds)
261 goto out;
262
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400263 /* this is only used for debugging, so it's ok if its NULL */
264 remotestr = nfs4_pnfs_remotestr(addr, gfp_flags);
265
Andy Adamson16b374c2010-10-20 00:18:04 -0400266 spin_lock(&nfs4_ds_cache_lock);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400267 tmp_ds = _data_server_lookup_locked(addr, addrlen);
Andy Adamson16b374c2010-10-20 00:18:04 -0400268 if (tmp_ds == NULL) {
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400269 memcpy(&ds->ds_addr, addr, addrlen);
270 ds->ds_addrlen = addrlen;
271 ds->ds_remotestr = remotestr;
Andy Adamson16b374c2010-10-20 00:18:04 -0400272 atomic_set(&ds->ds_count, 1);
273 INIT_LIST_HEAD(&ds->ds_node);
274 ds->ds_clp = NULL;
275 list_add(&ds->ds_node, &nfs4_data_server_cache);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400276 dprintk("%s add new data server %s\n", __func__,
277 ds->ds_remotestr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400278 } else {
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400279 kfree(remotestr);
Andy Adamson16b374c2010-10-20 00:18:04 -0400280 kfree(ds);
281 atomic_inc(&tmp_ds->ds_count);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400282 dprintk("%s data server %s found, inc'ed ds_count to %d\n",
283 __func__, tmp_ds->ds_remotestr,
Andy Adamson16b374c2010-10-20 00:18:04 -0400284 atomic_read(&tmp_ds->ds_count));
285 ds = tmp_ds;
286 }
287 spin_unlock(&nfs4_ds_cache_lock);
288out:
289 return ds;
290}
291
292/*
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400293 * Currently only supports ipv4, ipv6 and one multi-path address.
Andy Adamson16b374c2010-10-20 00:18:04 -0400294 */
295static struct nfs4_pnfs_ds *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400296decode_and_add_ds(struct xdr_stream *streamp, struct inode *inode, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400297{
298 struct nfs4_pnfs_ds *ds = NULL;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400299 char *buf, *portstr;
300 struct sockaddr_storage ss;
301 size_t sslen;
302 u32 port;
303 int nlen, rlen;
Andy Adamson16b374c2010-10-20 00:18:04 -0400304 int tmp[2];
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400305 __be32 *p;
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400306 char *netid, *match_netid;
307 size_t match_netid_len;
Andy Adamson16b374c2010-10-20 00:18:04 -0400308
309 /* r_netid */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400310 p = xdr_inline_decode(streamp, 4);
311 if (unlikely(!p))
312 goto out_err;
Andy Adamson16b374c2010-10-20 00:18:04 -0400313 nlen = be32_to_cpup(p++);
Andy Adamson16b374c2010-10-20 00:18:04 -0400314
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400315 p = xdr_inline_decode(streamp, nlen);
316 if (unlikely(!p))
317 goto out_err;
Andy Adamson16b374c2010-10-20 00:18:04 -0400318
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400319 netid = kmalloc(nlen+1, gfp_flags);
320 if (unlikely(!netid))
Andy Adamson16b374c2010-10-20 00:18:04 -0400321 goto out_err;
Andy Adamson16b374c2010-10-20 00:18:04 -0400322
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400323 netid[nlen] = '\0';
324 memcpy(netid, p, nlen);
325
326 /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400327 p = xdr_inline_decode(streamp, 4);
328 if (unlikely(!p))
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400329 goto out_free_netid;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400330 rlen = be32_to_cpup(p);
331
332 p = xdr_inline_decode(streamp, rlen);
333 if (unlikely(!p))
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400334 goto out_free_netid;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400335
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400336 /* port is ".ABC.DEF", 8 chars max */
337 if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
Jesper Juhlad3d2ee2011-01-17 18:41:50 +0000338 dprintk("%s: Invalid address, length %d\n", __func__,
Andy Adamson16b374c2010-10-20 00:18:04 -0400339 rlen);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400340 goto out_free_netid;
Andy Adamson16b374c2010-10-20 00:18:04 -0400341 }
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400342 buf = kmalloc(rlen + 1, gfp_flags);
Stanislav Fomichevb9f81052011-02-05 23:13:01 +0000343 if (!buf) {
344 dprintk("%s: Not enough memory\n", __func__);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400345 goto out_free_netid;
Stanislav Fomichevb9f81052011-02-05 23:13:01 +0000346 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400347 buf[rlen] = '\0';
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400348 memcpy(buf, p, rlen);
Andy Adamson16b374c2010-10-20 00:18:04 -0400349
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400350 /* replace port '.' with '-' */
351 portstr = strrchr(buf, '.');
352 if (!portstr) {
353 dprintk("%s: Failed finding expected dot in port\n",
354 __func__);
355 goto out_free_buf;
356 }
357 *portstr = '-';
358
359 /* find '.' between address and port */
360 portstr = strrchr(buf, '.');
361 if (!portstr) {
362 dprintk("%s: Failed finding expected dot between address and "
363 "port\n", __func__);
364 goto out_free_buf;
365 }
366 *portstr = '\0';
367
368 if (!rpc_pton(buf, portstr-buf, (struct sockaddr *)&ss, sizeof(ss))) {
369 dprintk("%s: Error parsing address %s\n", __func__, buf);
370 goto out_free_buf;
Andy Adamson16b374c2010-10-20 00:18:04 -0400371 }
372
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400373 portstr++;
374 sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
Andy Adamson16b374c2010-10-20 00:18:04 -0400375 port = htons((tmp[0] << 8) | (tmp[1]));
376
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400377 switch (ss.ss_family) {
378 case AF_INET:
379 ((struct sockaddr_in *)&ss)->sin_port = port;
380 sslen = sizeof(struct sockaddr_in);
381 match_netid = "tcp";
382 match_netid_len = 3;
383 break;
384
385 case AF_INET6:
386 ((struct sockaddr_in6 *)&ss)->sin6_port = port;
387 sslen = sizeof(struct sockaddr_in6);
388 match_netid = "tcp6";
389 match_netid_len = 4;
390 break;
391
392 default:
393 dprintk("%s: unsupported address family: %u\n",
394 __func__, ss.ss_family);
395 goto out_free_buf;
396 }
397
398 if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
399 dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
400 __func__, netid, match_netid);
401 goto out_free_buf;
402 }
403
404 ds = nfs4_pnfs_ds_add((struct sockaddr *)&ss, sslen, gfp_flags);
405 dprintk("%s: Added DS %s\n", __func__, ds->ds_remotestr);
406out_free_buf:
Andy Adamson16b374c2010-10-20 00:18:04 -0400407 kfree(buf);
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400408out_free_netid:
409 kfree(netid);
Andy Adamson16b374c2010-10-20 00:18:04 -0400410out_err:
411 return ds;
412}
413
414/* Decode opaque device data and return the result */
415static struct nfs4_file_layout_dsaddr*
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400416decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400417{
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400418 int i;
Andy Adamson16b374c2010-10-20 00:18:04 -0400419 u32 cnt, num;
420 u8 *indexp;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400421 __be32 *p;
422 u8 *stripe_indices;
423 u8 max_stripe_index;
424 struct nfs4_file_layout_dsaddr *dsaddr = NULL;
425 struct xdr_stream stream;
Benny Halevyf7da7a12011-05-19 14:16:47 -0400426 struct xdr_buf buf;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400427 struct page *scratch;
428
429 /* set up xdr stream */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400430 scratch = alloc_page(gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400431 if (!scratch)
432 goto out_err;
433
Benny Halevyf7da7a12011-05-19 14:16:47 -0400434 xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400435 xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
Andy Adamson16b374c2010-10-20 00:18:04 -0400436
437 /* Get the stripe count (number of stripe index) */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400438 p = xdr_inline_decode(&stream, 4);
439 if (unlikely(!p))
440 goto out_err_free_scratch;
441
442 cnt = be32_to_cpup(p);
Andy Adamson16b374c2010-10-20 00:18:04 -0400443 dprintk("%s stripe count %d\n", __func__, cnt);
444 if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
445 printk(KERN_WARNING "%s: stripe count %d greater than "
446 "supported maximum %d\n", __func__,
447 cnt, NFS4_PNFS_MAX_STRIPE_CNT);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400448 goto out_err_free_scratch;
449 }
450
451 /* read stripe indices */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400452 stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400453 if (!stripe_indices)
454 goto out_err_free_scratch;
455
456 p = xdr_inline_decode(&stream, cnt << 2);
457 if (unlikely(!p))
458 goto out_err_free_stripe_indices;
459
460 indexp = &stripe_indices[0];
461 max_stripe_index = 0;
462 for (i = 0; i < cnt; i++) {
463 *indexp = be32_to_cpup(p++);
464 max_stripe_index = max(max_stripe_index, *indexp);
465 indexp++;
Andy Adamson16b374c2010-10-20 00:18:04 -0400466 }
467
468 /* Check the multipath list count */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400469 p = xdr_inline_decode(&stream, 4);
470 if (unlikely(!p))
471 goto out_err_free_stripe_indices;
472
473 num = be32_to_cpup(p);
Andy Adamson16b374c2010-10-20 00:18:04 -0400474 dprintk("%s ds_num %u\n", __func__, num);
475 if (num > NFS4_PNFS_MAX_MULTI_CNT) {
476 printk(KERN_WARNING "%s: multipath count %d greater than "
477 "supported maximum %d\n", __func__,
478 num, NFS4_PNFS_MAX_MULTI_CNT);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400479 goto out_err_free_stripe_indices;
Andy Adamson16b374c2010-10-20 00:18:04 -0400480 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400481
482 /* validate stripe indices are all < num */
483 if (max_stripe_index >= num) {
484 printk(KERN_WARNING "%s: stripe index %u >= num ds %u\n",
485 __func__, max_stripe_index, num);
486 goto out_err_free_stripe_indices;
487 }
488
Andy Adamson16b374c2010-10-20 00:18:04 -0400489 dsaddr = kzalloc(sizeof(*dsaddr) +
490 (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400491 gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400492 if (!dsaddr)
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400493 goto out_err_free_stripe_indices;
Andy Adamson16b374c2010-10-20 00:18:04 -0400494
495 dsaddr->stripe_count = cnt;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400496 dsaddr->stripe_indices = stripe_indices;
497 stripe_indices = NULL;
Andy Adamson16b374c2010-10-20 00:18:04 -0400498 dsaddr->ds_num = num;
Benny Halevy1775bc32011-05-20 13:47:33 +0200499 nfs4_init_deviceid_node(&dsaddr->id_node,
500 NFS_SERVER(ino)->pnfs_curr_ld,
501 NFS_SERVER(ino)->nfs_client,
Benny Halevya1eaecb2011-05-19 22:14:47 -0400502 &pdev->dev_id);
Andy Adamson16b374c2010-10-20 00:18:04 -0400503
Andy Adamson16b374c2010-10-20 00:18:04 -0400504 for (i = 0; i < dsaddr->ds_num; i++) {
505 int j;
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400506 u32 mp_count;
Andy Adamson16b374c2010-10-20 00:18:04 -0400507
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400508 p = xdr_inline_decode(&stream, 4);
509 if (unlikely(!p))
510 goto out_err_free_deviceid;
511
512 mp_count = be32_to_cpup(p); /* multipath count */
513 if (mp_count > 1) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400514 printk(KERN_WARNING
515 "%s: Multipath count %d not supported, "
516 "skipping all greater than 1\n", __func__,
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400517 mp_count);
Andy Adamson16b374c2010-10-20 00:18:04 -0400518 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400519 for (j = 0; j < mp_count; j++) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400520 if (j == 0) {
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400521 dsaddr->ds_list[i] = decode_and_add_ds(&stream,
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400522 ino, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400523 if (dsaddr->ds_list[i] == NULL)
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400524 goto out_err_free_deviceid;
Andy Adamson16b374c2010-10-20 00:18:04 -0400525 } else {
526 u32 len;
527 /* skip extra multipath */
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400528
529 /* read len, skip */
530 p = xdr_inline_decode(&stream, 4);
531 if (unlikely(!p))
532 goto out_err_free_deviceid;
533 len = be32_to_cpup(p);
534
535 p = xdr_inline_decode(&stream, len);
536 if (unlikely(!p))
537 goto out_err_free_deviceid;
538
539 /* read len, skip */
540 p = xdr_inline_decode(&stream, 4);
541 if (unlikely(!p))
542 goto out_err_free_deviceid;
543 len = be32_to_cpup(p);
544
545 p = xdr_inline_decode(&stream, len);
546 if (unlikely(!p))
547 goto out_err_free_deviceid;
Andy Adamson16b374c2010-10-20 00:18:04 -0400548 }
549 }
550 }
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400551
552 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400553 return dsaddr;
554
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400555out_err_free_deviceid:
Andy Adamson16b374c2010-10-20 00:18:04 -0400556 nfs4_fl_free_deviceid(dsaddr);
Weston Andros Adamson35124a02011-03-24 16:48:21 -0400557 /* stripe_indicies was part of dsaddr */
558 goto out_err_free_scratch;
559out_err_free_stripe_indices:
560 kfree(stripe_indices);
561out_err_free_scratch:
562 __free_page(scratch);
Andy Adamson16b374c2010-10-20 00:18:04 -0400563out_err:
564 dprintk("%s ERROR: returning NULL\n", __func__);
565 return NULL;
566}
567
568/*
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000569 * Decode the opaque device specified in 'dev' and add it to the cache of
570 * available devices.
Andy Adamson16b374c2010-10-20 00:18:04 -0400571 */
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000572static struct nfs4_file_layout_dsaddr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400573decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400574{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400575 struct nfs4_deviceid_node *d;
576 struct nfs4_file_layout_dsaddr *n, *new;
Andy Adamson16b374c2010-10-20 00:18:04 -0400577
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400578 new = decode_device(inode, dev, gfp_flags);
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000579 if (!new) {
Andy Adamson16b374c2010-10-20 00:18:04 -0400580 printk(KERN_WARNING "%s: Could not decode or add device\n",
581 __func__);
582 return NULL;
583 }
584
Benny Halevya1eaecb2011-05-19 22:14:47 -0400585 d = nfs4_insert_deviceid_node(&new->id_node);
586 n = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
587 if (n != new) {
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000588 nfs4_fl_free_deviceid(new);
Benny Halevya1eaecb2011-05-19 22:14:47 -0400589 return n;
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000590 }
Andy Adamson16b374c2010-10-20 00:18:04 -0400591
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000592 return new;
Andy Adamson16b374c2010-10-20 00:18:04 -0400593}
594
595/*
596 * Retrieve the information for dev_id, add it to the list
597 * of available devices, and return it.
598 */
599struct nfs4_file_layout_dsaddr *
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400600get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id, gfp_t gfp_flags)
Andy Adamson16b374c2010-10-20 00:18:04 -0400601{
602 struct pnfs_device *pdev = NULL;
603 u32 max_resp_sz;
604 int max_pages;
605 struct page **pages = NULL;
606 struct nfs4_file_layout_dsaddr *dsaddr = NULL;
607 int rc, i;
608 struct nfs_server *server = NFS_SERVER(inode);
609
610 /*
611 * Use the session max response size as the basis for setting
612 * GETDEVICEINFO's maxcount
613 */
614 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
615 max_pages = max_resp_sz >> PAGE_SHIFT;
616 dprintk("%s inode %p max_resp_sz %u max_pages %d\n",
617 __func__, inode, max_resp_sz, max_pages);
618
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400619 pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400620 if (pdev == NULL)
621 return NULL;
622
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400623 pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400624 if (pages == NULL) {
625 kfree(pdev);
626 return NULL;
627 }
628 for (i = 0; i < max_pages; i++) {
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400629 pages[i] = alloc_page(gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400630 if (!pages[i])
631 goto out_free;
632 }
633
Andy Adamson16b374c2010-10-20 00:18:04 -0400634 memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
635 pdev->layout_type = LAYOUT_NFSV4_1_FILES;
636 pdev->pages = pages;
637 pdev->pgbase = 0;
638 pdev->pglen = PAGE_SIZE * max_pages;
639 pdev->mincount = 0;
640
641 rc = nfs4_proc_getdeviceinfo(server, pdev);
642 dprintk("%s getdevice info returns %d\n", __func__, rc);
643 if (rc)
644 goto out_free;
645
646 /*
647 * Found new device, need to decode it and then add it to the
648 * list of known devices for this mountpoint.
649 */
Trond Myklebusta75b9df2011-05-11 18:00:51 -0400650 dsaddr = decode_and_add_device(inode, pdev, gfp_flags);
Andy Adamson16b374c2010-10-20 00:18:04 -0400651out_free:
Andy Adamson16b374c2010-10-20 00:18:04 -0400652 for (i = 0; i < max_pages; i++)
653 __free_page(pages[i]);
654 kfree(pages);
655 kfree(pdev);
656 dprintk("<-- %s dsaddr %p\n", __func__, dsaddr);
657 return dsaddr;
658}
659
Christoph Hellwigea8eecd2011-03-01 01:34:21 +0000660void
661nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
Andy Adamson16b374c2010-10-20 00:18:04 -0400662{
Benny Halevy1775bc32011-05-20 13:47:33 +0200663 nfs4_put_deviceid_node(&dsaddr->id_node);
Andy Adamson16b374c2010-10-20 00:18:04 -0400664}
Fred Isamancfe7f412011-03-01 01:34:18 +0000665
666/*
667 * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
668 * Then: ((res + fsi) % dsaddr->stripe_count)
669 */
670u32
671nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
672{
673 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
674 u64 tmp;
675
676 tmp = offset - flseg->pattern_offset;
677 do_div(tmp, flseg->stripe_unit);
678 tmp += flseg->first_stripe_index;
679 return do_div(tmp, flseg->dsaddr->stripe_count);
680}
681
682u32
683nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
684{
685 return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
686}
687
688struct nfs_fh *
689nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
690{
691 struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
692 u32 i;
693
694 if (flseg->stripe_type == STRIPE_SPARSE) {
695 if (flseg->num_fh == 1)
696 i = 0;
697 else if (flseg->num_fh == 0)
698 /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
699 return NULL;
700 else
701 i = nfs4_fl_calc_ds_index(lseg, j);
702 } else
703 i = j;
704 return flseg->fh_array[i];
705}
706
Andy Adamson568e8c42011-03-01 01:34:22 +0000707static void
708filelayout_mark_devid_negative(struct nfs4_file_layout_dsaddr *dsaddr,
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400709 int err, const char *ds_remotestr)
Andy Adamson568e8c42011-03-01 01:34:22 +0000710{
Benny Halevya1eaecb2011-05-19 22:14:47 -0400711 u32 *p = (u32 *)&dsaddr->id_node.deviceid;
Andy Adamson568e8c42011-03-01 01:34:22 +0000712
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400713 printk(KERN_ERR "NFS: data server %s connection error %d."
Andy Adamson568e8c42011-03-01 01:34:22 +0000714 " Deviceid [%x%x%x%x] marked out of use.\n",
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400715 ds_remotestr, err, p[0], p[1], p[2], p[3]);
Andy Adamson568e8c42011-03-01 01:34:22 +0000716
Benny Halevya1eaecb2011-05-19 22:14:47 -0400717 spin_lock(&nfs4_ds_cache_lock);
Andy Adamson568e8c42011-03-01 01:34:22 +0000718 dsaddr->flags |= NFS4_DEVICE_ID_NEG_ENTRY;
Benny Halevya1eaecb2011-05-19 22:14:47 -0400719 spin_unlock(&nfs4_ds_cache_lock);
Andy Adamson568e8c42011-03-01 01:34:22 +0000720}
721
Fred Isamancfe7f412011-03-01 01:34:18 +0000722struct nfs4_pnfs_ds *
723nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
724{
725 struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
726 struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
727
728 if (ds == NULL) {
729 printk(KERN_ERR "%s: No data server for offset index %d\n",
730 __func__, ds_idx);
731 return NULL;
732 }
733
734 if (!ds->ds_clp) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000735 struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
Fred Isamancfe7f412011-03-01 01:34:18 +0000736 int err;
737
Andy Adamson568e8c42011-03-01 01:34:22 +0000738 if (dsaddr->flags & NFS4_DEVICE_ID_NEG_ENTRY) {
739 /* Already tried to connect, don't try again */
740 dprintk("%s Deviceid marked out of use\n", __func__);
741 return NULL;
742 }
743 err = nfs4_ds_connect(s, ds);
Fred Isamancfe7f412011-03-01 01:34:18 +0000744 if (err) {
Andy Adamson568e8c42011-03-01 01:34:22 +0000745 filelayout_mark_devid_negative(dsaddr, err,
Weston Andros Adamsonc9895cb2011-05-31 18:48:56 -0400746 ds->ds_remotestr);
Fred Isamancfe7f412011-03-01 01:34:18 +0000747 return NULL;
748 }
749 }
750 return ds;
751}