]>
Commit | Line | Data |
---|---|---|
16b374ca AA |
1 | /* |
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 | */ | |
17280175 | 48 | static DEFINE_SPINLOCK(nfs4_ds_cache_lock); |
16b374ca AA |
49 | static LIST_HEAD(nfs4_data_server_cache); |
50 | ||
51 | /* Debug routines */ | |
52 | void | |
53 | print_ds(struct nfs4_pnfs_ds *ds) | |
54 | { | |
55 | if (ds == NULL) { | |
56 | printk("%s NULL device\n", __func__); | |
57 | return; | |
58 | } | |
c9895cb6 | 59 | printk(" ds %s\n" |
16b374ca AA |
60 | " ref count %d\n" |
61 | " client %p\n" | |
62 | " cl_exchange_flags %x\n", | |
c9895cb6 | 63 | ds->ds_remotestr, |
16b374ca AA |
64 | atomic_read(&ds->ds_count), ds->ds_clp, |
65 | ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0); | |
66 | } | |
67 | ||
14f9a607 WAA |
68 | static bool |
69 | same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2) | |
16b374ca | 70 | { |
c9895cb6 WAA |
71 | struct sockaddr_in *a, *b; |
72 | struct sockaddr_in6 *a6, *b6; | |
16b374ca | 73 | |
14f9a607 WAA |
74 | if (addr1->sa_family != addr2->sa_family) |
75 | return false; | |
76 | ||
77 | switch (addr1->sa_family) { | |
78 | case AF_INET: | |
79 | a = (struct sockaddr_in *)addr1; | |
80 | b = (struct sockaddr_in *)addr2; | |
81 | ||
82 | if (a->sin_addr.s_addr == b->sin_addr.s_addr && | |
83 | a->sin_port == b->sin_port) | |
84 | return true; | |
85 | break; | |
86 | ||
87 | case AF_INET6: | |
88 | a6 = (struct sockaddr_in6 *)addr1; | |
89 | b6 = (struct sockaddr_in6 *)addr2; | |
90 | ||
91 | /* 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; | |
96 | ||
97 | if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) && | |
98 | a6->sin6_port == b6->sin6_port) | |
99 | return true; | |
100 | break; | |
101 | ||
102 | default: | |
103 | dprintk("%s: unhandled address family: %u\n", | |
104 | __func__, addr1->sa_family); | |
105 | return false; | |
106 | } | |
107 | ||
108 | return false; | |
109 | } | |
110 | ||
17280175 | 111 | static bool |
2d3fe01c WAA |
112 | _same_data_server_addrs_locked(const struct list_head *dsaddrs1, |
113 | const struct list_head *dsaddrs2) | |
14f9a607 | 114 | { |
14f9a607 WAA |
115 | struct nfs4_pnfs_ds_addr *da1, *da2; |
116 | ||
2d3fe01c WAA |
117 | /* step through both lists, comparing as we go */ |
118 | for (da1 = list_first_entry(dsaddrs1, typeof(*da1), da_node), | |
119 | da2 = list_first_entry(dsaddrs2, typeof(*da2), da_node); | |
120 | da1 != NULL && da2 != NULL; | |
121 | da1 = list_entry(da1->da_node.next, typeof(*da1), da_node), | |
122 | da2 = list_entry(da2->da_node.next, typeof(*da2), da_node)) { | |
123 | if (!same_sockaddr((struct sockaddr *)&da1->da_addr, | |
124 | (struct sockaddr *)&da2->da_addr)) | |
125 | return false; | |
16b374ca | 126 | } |
2d3fe01c WAA |
127 | if (da1 == NULL && da2 == NULL) |
128 | return true; | |
129 | ||
130 | return false; | |
16b374ca AA |
131 | } |
132 | ||
14f9a607 | 133 | /* |
2d3fe01c | 134 | * Lookup DS by addresses. nfs4_ds_cache_lock is held |
14f9a607 | 135 | */ |
2d3fe01c WAA |
136 | static struct nfs4_pnfs_ds * |
137 | _data_server_lookup_locked(const struct list_head *dsaddrs) | |
14f9a607 | 138 | { |
2d3fe01c | 139 | struct nfs4_pnfs_ds *ds; |
14f9a607 | 140 | |
2d3fe01c WAA |
141 | list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) |
142 | if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs)) | |
143 | return ds; | |
144 | return NULL; | |
14f9a607 WAA |
145 | } |
146 | ||
d83217c1 AA |
147 | /* |
148 | * Create an rpc connection to the nfs4_pnfs_ds data server | |
35dbbc99 | 149 | * Currently only supports IPv4 and IPv6 addresses |
d83217c1 AA |
150 | */ |
151 | static int | |
152 | nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds) | |
153 | { | |
7e574f0d | 154 | struct nfs_client *clp = ERR_PTR(-EIO); |
14f9a607 | 155 | struct nfs4_pnfs_ds_addr *da; |
d83217c1 AA |
156 | int status = 0; |
157 | ||
14f9a607 | 158 | dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr, |
d83217c1 AA |
159 | mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor); |
160 | ||
14f9a607 WAA |
161 | BUG_ON(list_empty(&ds->ds_addrs)); |
162 | ||
7e574f0d WAA |
163 | list_for_each_entry(da, &ds->ds_addrs, da_node) { |
164 | dprintk("%s: DS %s: trying address %s\n", | |
165 | __func__, ds->ds_remotestr, da->da_remotestr); | |
14f9a607 | 166 | |
7e574f0d | 167 | clp = nfs4_set_ds_client(mds_srv->nfs_client, |
14f9a607 WAA |
168 | (struct sockaddr *)&da->da_addr, |
169 | da->da_addrlen, IPPROTO_TCP); | |
7e574f0d WAA |
170 | if (!IS_ERR(clp)) |
171 | break; | |
172 | } | |
173 | ||
d83217c1 AA |
174 | if (IS_ERR(clp)) { |
175 | status = PTR_ERR(clp); | |
176 | goto out; | |
177 | } | |
178 | ||
179 | if ((clp->cl_exchange_flags & EXCHGID4_FLAG_MASK_PNFS) != 0) { | |
180 | if (!is_ds_client(clp)) { | |
181 | status = -ENODEV; | |
182 | goto out_put; | |
183 | } | |
184 | ds->ds_clp = clp; | |
c9895cb6 WAA |
185 | dprintk("%s [existing] server=%s\n", __func__, |
186 | ds->ds_remotestr); | |
d83217c1 AA |
187 | goto out; |
188 | } | |
189 | ||
190 | /* | |
191 | * Do not set NFS_CS_CHECK_LEASE_TIME instead set the DS lease to | |
192 | * be equal to the MDS lease. Renewal is scheduled in create_session. | |
193 | */ | |
194 | spin_lock(&mds_srv->nfs_client->cl_lock); | |
195 | clp->cl_lease_time = mds_srv->nfs_client->cl_lease_time; | |
196 | spin_unlock(&mds_srv->nfs_client->cl_lock); | |
197 | clp->cl_last_renewal = jiffies; | |
198 | ||
199 | /* New nfs_client */ | |
200 | status = nfs4_init_ds_session(clp); | |
201 | if (status) | |
202 | goto out_put; | |
203 | ||
204 | ds->ds_clp = clp; | |
c9895cb6 | 205 | dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); |
d83217c1 AA |
206 | out: |
207 | return status; | |
208 | out_put: | |
209 | nfs_put_client(clp); | |
210 | goto out; | |
211 | } | |
212 | ||
16b374ca AA |
213 | static void |
214 | destroy_ds(struct nfs4_pnfs_ds *ds) | |
215 | { | |
14f9a607 WAA |
216 | struct nfs4_pnfs_ds_addr *da; |
217 | ||
16b374ca AA |
218 | dprintk("--> %s\n", __func__); |
219 | ifdebug(FACILITY) | |
220 | print_ds(ds); | |
221 | ||
222 | if (ds->ds_clp) | |
223 | nfs_put_client(ds->ds_clp); | |
14f9a607 WAA |
224 | |
225 | while (!list_empty(&ds->ds_addrs)) { | |
226 | da = list_first_entry(&ds->ds_addrs, | |
227 | struct nfs4_pnfs_ds_addr, | |
228 | da_node); | |
229 | list_del_init(&da->da_node); | |
230 | kfree(da->da_remotestr); | |
231 | kfree(da); | |
232 | } | |
233 | ||
c9895cb6 | 234 | kfree(ds->ds_remotestr); |
16b374ca AA |
235 | kfree(ds); |
236 | } | |
237 | ||
1775bc34 | 238 | void |
16b374ca AA |
239 | nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr) |
240 | { | |
241 | struct nfs4_pnfs_ds *ds; | |
242 | int i; | |
243 | ||
a1eaecbc | 244 | nfs4_print_deviceid(&dsaddr->id_node.deviceid); |
16b374ca AA |
245 | |
246 | for (i = 0; i < dsaddr->ds_num; i++) { | |
247 | ds = dsaddr->ds_list[i]; | |
248 | if (ds != NULL) { | |
249 | if (atomic_dec_and_lock(&ds->ds_count, | |
250 | &nfs4_ds_cache_lock)) { | |
251 | list_del_init(&ds->ds_node); | |
252 | spin_unlock(&nfs4_ds_cache_lock); | |
253 | destroy_ds(ds); | |
254 | } | |
255 | } | |
256 | } | |
257 | kfree(dsaddr->stripe_indices); | |
258 | kfree(dsaddr); | |
259 | } | |
260 | ||
c9895cb6 WAA |
261 | /* |
262 | * Create a string with a human readable address and port to avoid | |
263 | * complicated setup around many dprinks. | |
264 | */ | |
265 | static char * | |
14f9a607 | 266 | nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags) |
c9895cb6 | 267 | { |
14f9a607 | 268 | struct nfs4_pnfs_ds_addr *da; |
c9895cb6 | 269 | char *remotestr; |
c9895cb6 | 270 | size_t len; |
14f9a607 | 271 | char *p; |
c9895cb6 | 272 | |
14f9a607 WAA |
273 | len = 3; /* '{', '}' and eol */ |
274 | list_for_each_entry(da, dsaddrs, da_node) { | |
275 | len += strlen(da->da_remotestr) + 1; /* string plus comma */ | |
c9895cb6 WAA |
276 | } |
277 | ||
14f9a607 WAA |
278 | remotestr = kzalloc(len, gfp_flags); |
279 | if (!remotestr) | |
c9895cb6 | 280 | return NULL; |
c9895cb6 | 281 | |
14f9a607 WAA |
282 | p = remotestr; |
283 | *(p++) = '{'; | |
284 | len--; | |
285 | list_for_each_entry(da, dsaddrs, da_node) { | |
286 | size_t ll = strlen(da->da_remotestr); | |
c9895cb6 | 287 | |
14f9a607 WAA |
288 | if (ll > len) |
289 | goto out_err; | |
c9895cb6 | 290 | |
14f9a607 WAA |
291 | memcpy(p, da->da_remotestr, ll); |
292 | p += ll; | |
293 | len -= ll; | |
c9895cb6 | 294 | |
14f9a607 WAA |
295 | if (len < 1) |
296 | goto out_err; | |
297 | (*p++) = ','; | |
298 | len--; | |
299 | } | |
300 | if (len < 2) | |
301 | goto out_err; | |
302 | *(p++) = '}'; | |
303 | *p = '\0'; | |
c9895cb6 | 304 | return remotestr; |
14f9a607 WAA |
305 | out_err: |
306 | kfree(remotestr); | |
307 | return NULL; | |
c9895cb6 WAA |
308 | } |
309 | ||
16b374ca | 310 | static struct nfs4_pnfs_ds * |
14f9a607 | 311 | nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags) |
16b374ca | 312 | { |
c9895cb6 WAA |
313 | struct nfs4_pnfs_ds *tmp_ds, *ds = NULL; |
314 | char *remotestr; | |
16b374ca | 315 | |
14f9a607 WAA |
316 | if (list_empty(dsaddrs)) { |
317 | dprintk("%s: no addresses defined\n", __func__); | |
318 | goto out; | |
319 | } | |
320 | ||
321 | ds = kzalloc(sizeof(*ds), gfp_flags); | |
16b374ca AA |
322 | if (!ds) |
323 | goto out; | |
324 | ||
c9895cb6 | 325 | /* this is only used for debugging, so it's ok if its NULL */ |
14f9a607 | 326 | remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags); |
c9895cb6 | 327 | |
16b374ca | 328 | spin_lock(&nfs4_ds_cache_lock); |
14f9a607 | 329 | tmp_ds = _data_server_lookup_locked(dsaddrs); |
16b374ca | 330 | if (tmp_ds == NULL) { |
14f9a607 WAA |
331 | INIT_LIST_HEAD(&ds->ds_addrs); |
332 | list_splice_init(dsaddrs, &ds->ds_addrs); | |
c9895cb6 | 333 | ds->ds_remotestr = remotestr; |
16b374ca AA |
334 | atomic_set(&ds->ds_count, 1); |
335 | INIT_LIST_HEAD(&ds->ds_node); | |
336 | ds->ds_clp = NULL; | |
337 | list_add(&ds->ds_node, &nfs4_data_server_cache); | |
c9895cb6 WAA |
338 | dprintk("%s add new data server %s\n", __func__, |
339 | ds->ds_remotestr); | |
16b374ca | 340 | } else { |
c9895cb6 | 341 | kfree(remotestr); |
16b374ca AA |
342 | kfree(ds); |
343 | atomic_inc(&tmp_ds->ds_count); | |
c9895cb6 WAA |
344 | dprintk("%s data server %s found, inc'ed ds_count to %d\n", |
345 | __func__, tmp_ds->ds_remotestr, | |
16b374ca AA |
346 | atomic_read(&tmp_ds->ds_count)); |
347 | ds = tmp_ds; | |
348 | } | |
349 | spin_unlock(&nfs4_ds_cache_lock); | |
350 | out: | |
351 | return ds; | |
352 | } | |
353 | ||
354 | /* | |
c9895cb6 | 355 | * Currently only supports ipv4, ipv6 and one multi-path address. |
16b374ca | 356 | */ |
14f9a607 | 357 | static struct nfs4_pnfs_ds_addr * |
17094272 | 358 | decode_ds_addr(struct net *net, struct xdr_stream *streamp, gfp_t gfp_flags) |
16b374ca | 359 | { |
14f9a607 | 360 | struct nfs4_pnfs_ds_addr *da = NULL; |
c9895cb6 | 361 | char *buf, *portstr; |
13fff2f3 | 362 | __be16 port; |
c9895cb6 | 363 | int nlen, rlen; |
16b374ca | 364 | int tmp[2]; |
35124a09 | 365 | __be32 *p; |
c9895cb6 | 366 | char *netid, *match_netid; |
14f9a607 WAA |
367 | size_t len, match_netid_len; |
368 | char *startsep = ""; | |
369 | char *endsep = ""; | |
370 | ||
16b374ca AA |
371 | |
372 | /* r_netid */ | |
35124a09 WAA |
373 | p = xdr_inline_decode(streamp, 4); |
374 | if (unlikely(!p)) | |
375 | goto out_err; | |
16b374ca | 376 | nlen = be32_to_cpup(p++); |
16b374ca | 377 | |
35124a09 WAA |
378 | p = xdr_inline_decode(streamp, nlen); |
379 | if (unlikely(!p)) | |
380 | goto out_err; | |
16b374ca | 381 | |
c9895cb6 WAA |
382 | netid = kmalloc(nlen+1, gfp_flags); |
383 | if (unlikely(!netid)) | |
16b374ca | 384 | goto out_err; |
16b374ca | 385 | |
c9895cb6 WAA |
386 | netid[nlen] = '\0'; |
387 | memcpy(netid, p, nlen); | |
388 | ||
389 | /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */ | |
35124a09 WAA |
390 | p = xdr_inline_decode(streamp, 4); |
391 | if (unlikely(!p)) | |
c9895cb6 | 392 | goto out_free_netid; |
35124a09 WAA |
393 | rlen = be32_to_cpup(p); |
394 | ||
395 | p = xdr_inline_decode(streamp, rlen); | |
396 | if (unlikely(!p)) | |
c9895cb6 | 397 | goto out_free_netid; |
35124a09 | 398 | |
c9895cb6 WAA |
399 | /* port is ".ABC.DEF", 8 chars max */ |
400 | if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) { | |
ad3d2eed | 401 | dprintk("%s: Invalid address, length %d\n", __func__, |
16b374ca | 402 | rlen); |
c9895cb6 | 403 | goto out_free_netid; |
16b374ca | 404 | } |
a75b9df9 | 405 | buf = kmalloc(rlen + 1, gfp_flags); |
b9f81057 SF |
406 | if (!buf) { |
407 | dprintk("%s: Not enough memory\n", __func__); | |
c9895cb6 | 408 | goto out_free_netid; |
b9f81057 | 409 | } |
16b374ca | 410 | buf[rlen] = '\0'; |
35124a09 | 411 | memcpy(buf, p, rlen); |
16b374ca | 412 | |
c9895cb6 WAA |
413 | /* replace port '.' with '-' */ |
414 | portstr = strrchr(buf, '.'); | |
415 | if (!portstr) { | |
416 | dprintk("%s: Failed finding expected dot in port\n", | |
417 | __func__); | |
418 | goto out_free_buf; | |
419 | } | |
420 | *portstr = '-'; | |
421 | ||
422 | /* find '.' between address and port */ | |
423 | portstr = strrchr(buf, '.'); | |
424 | if (!portstr) { | |
425 | dprintk("%s: Failed finding expected dot between address and " | |
426 | "port\n", __func__); | |
427 | goto out_free_buf; | |
16b374ca | 428 | } |
c9895cb6 | 429 | *portstr = '\0'; |
16b374ca | 430 | |
14f9a607 WAA |
431 | da = kzalloc(sizeof(*da), gfp_flags); |
432 | if (unlikely(!da)) | |
c9895cb6 | 433 | goto out_free_buf; |
14f9a607 WAA |
434 | |
435 | INIT_LIST_HEAD(&da->da_node); | |
436 | ||
17094272 | 437 | if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr, |
14f9a607 WAA |
438 | sizeof(da->da_addr))) { |
439 | dprintk("%s: error parsing address %s\n", __func__, buf); | |
440 | goto out_free_da; | |
16b374ca AA |
441 | } |
442 | ||
c9895cb6 WAA |
443 | portstr++; |
444 | sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]); | |
16b374ca AA |
445 | port = htons((tmp[0] << 8) | (tmp[1])); |
446 | ||
14f9a607 | 447 | switch (da->da_addr.ss_family) { |
c9895cb6 | 448 | case AF_INET: |
14f9a607 WAA |
449 | ((struct sockaddr_in *)&da->da_addr)->sin_port = port; |
450 | da->da_addrlen = sizeof(struct sockaddr_in); | |
c9895cb6 WAA |
451 | match_netid = "tcp"; |
452 | match_netid_len = 3; | |
453 | break; | |
454 | ||
455 | case AF_INET6: | |
14f9a607 WAA |
456 | ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port; |
457 | da->da_addrlen = sizeof(struct sockaddr_in6); | |
c9895cb6 WAA |
458 | match_netid = "tcp6"; |
459 | match_netid_len = 4; | |
14f9a607 WAA |
460 | startsep = "["; |
461 | endsep = "]"; | |
c9895cb6 WAA |
462 | break; |
463 | ||
464 | default: | |
465 | dprintk("%s: unsupported address family: %u\n", | |
14f9a607 WAA |
466 | __func__, da->da_addr.ss_family); |
467 | goto out_free_da; | |
c9895cb6 WAA |
468 | } |
469 | ||
470 | if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) { | |
471 | dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n", | |
472 | __func__, netid, match_netid); | |
14f9a607 | 473 | goto out_free_da; |
c9895cb6 WAA |
474 | } |
475 | ||
14f9a607 WAA |
476 | /* save human readable address */ |
477 | len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7; | |
478 | da->da_remotestr = kzalloc(len, gfp_flags); | |
479 | ||
480 | /* NULL is ok, only used for dprintk */ | |
481 | if (da->da_remotestr) | |
482 | snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep, | |
483 | buf, endsep, ntohs(port)); | |
484 | ||
485 | dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr); | |
486 | kfree(buf); | |
487 | kfree(netid); | |
488 | return da; | |
489 | ||
490 | out_free_da: | |
491 | kfree(da); | |
c9895cb6 | 492 | out_free_buf: |
14f9a607 | 493 | dprintk("%s: Error parsing DS addr: %s\n", __func__, buf); |
16b374ca | 494 | kfree(buf); |
c9895cb6 WAA |
495 | out_free_netid: |
496 | kfree(netid); | |
16b374ca | 497 | out_err: |
14f9a607 | 498 | return NULL; |
16b374ca AA |
499 | } |
500 | ||
501 | /* Decode opaque device data and return the result */ | |
502 | static struct nfs4_file_layout_dsaddr* | |
a75b9df9 | 503 | decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags) |
16b374ca | 504 | { |
35124a09 | 505 | int i; |
16b374ca AA |
506 | u32 cnt, num; |
507 | u8 *indexp; | |
35124a09 WAA |
508 | __be32 *p; |
509 | u8 *stripe_indices; | |
510 | u8 max_stripe_index; | |
511 | struct nfs4_file_layout_dsaddr *dsaddr = NULL; | |
512 | struct xdr_stream stream; | |
f7da7a12 | 513 | struct xdr_buf buf; |
35124a09 | 514 | struct page *scratch; |
14f9a607 WAA |
515 | struct list_head dsaddrs; |
516 | struct nfs4_pnfs_ds_addr *da; | |
35124a09 WAA |
517 | |
518 | /* set up xdr stream */ | |
a75b9df9 | 519 | scratch = alloc_page(gfp_flags); |
35124a09 WAA |
520 | if (!scratch) |
521 | goto out_err; | |
522 | ||
f7da7a12 | 523 | xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen); |
35124a09 | 524 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
16b374ca AA |
525 | |
526 | /* Get the stripe count (number of stripe index) */ | |
35124a09 WAA |
527 | p = xdr_inline_decode(&stream, 4); |
528 | if (unlikely(!p)) | |
529 | goto out_err_free_scratch; | |
530 | ||
531 | cnt = be32_to_cpup(p); | |
16b374ca AA |
532 | dprintk("%s stripe count %d\n", __func__, cnt); |
533 | if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) { | |
a030889a | 534 | printk(KERN_WARNING "NFS: %s: stripe count %d greater than " |
16b374ca AA |
535 | "supported maximum %d\n", __func__, |
536 | cnt, NFS4_PNFS_MAX_STRIPE_CNT); | |
35124a09 WAA |
537 | goto out_err_free_scratch; |
538 | } | |
539 | ||
540 | /* read stripe indices */ | |
a75b9df9 | 541 | stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags); |
35124a09 WAA |
542 | if (!stripe_indices) |
543 | goto out_err_free_scratch; | |
544 | ||
545 | p = xdr_inline_decode(&stream, cnt << 2); | |
546 | if (unlikely(!p)) | |
547 | goto out_err_free_stripe_indices; | |
548 | ||
549 | indexp = &stripe_indices[0]; | |
550 | max_stripe_index = 0; | |
551 | for (i = 0; i < cnt; i++) { | |
552 | *indexp = be32_to_cpup(p++); | |
553 | max_stripe_index = max(max_stripe_index, *indexp); | |
554 | indexp++; | |
16b374ca AA |
555 | } |
556 | ||
557 | /* Check the multipath list count */ | |
35124a09 WAA |
558 | p = xdr_inline_decode(&stream, 4); |
559 | if (unlikely(!p)) | |
560 | goto out_err_free_stripe_indices; | |
561 | ||
562 | num = be32_to_cpup(p); | |
16b374ca AA |
563 | dprintk("%s ds_num %u\n", __func__, num); |
564 | if (num > NFS4_PNFS_MAX_MULTI_CNT) { | |
a030889a | 565 | printk(KERN_WARNING "NFS: %s: multipath count %d greater than " |
16b374ca AA |
566 | "supported maximum %d\n", __func__, |
567 | num, NFS4_PNFS_MAX_MULTI_CNT); | |
35124a09 | 568 | goto out_err_free_stripe_indices; |
16b374ca | 569 | } |
35124a09 WAA |
570 | |
571 | /* validate stripe indices are all < num */ | |
572 | if (max_stripe_index >= num) { | |
a030889a | 573 | printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n", |
35124a09 WAA |
574 | __func__, max_stripe_index, num); |
575 | goto out_err_free_stripe_indices; | |
576 | } | |
577 | ||
16b374ca AA |
578 | dsaddr = kzalloc(sizeof(*dsaddr) + |
579 | (sizeof(struct nfs4_pnfs_ds *) * (num - 1)), | |
a75b9df9 | 580 | gfp_flags); |
16b374ca | 581 | if (!dsaddr) |
35124a09 | 582 | goto out_err_free_stripe_indices; |
16b374ca AA |
583 | |
584 | dsaddr->stripe_count = cnt; | |
35124a09 WAA |
585 | dsaddr->stripe_indices = stripe_indices; |
586 | stripe_indices = NULL; | |
16b374ca | 587 | dsaddr->ds_num = num; |
1775bc34 BH |
588 | nfs4_init_deviceid_node(&dsaddr->id_node, |
589 | NFS_SERVER(ino)->pnfs_curr_ld, | |
590 | NFS_SERVER(ino)->nfs_client, | |
a1eaecbc | 591 | &pdev->dev_id); |
16b374ca | 592 | |
14f9a607 WAA |
593 | INIT_LIST_HEAD(&dsaddrs); |
594 | ||
16b374ca AA |
595 | for (i = 0; i < dsaddr->ds_num; i++) { |
596 | int j; | |
35124a09 WAA |
597 | u32 mp_count; |
598 | ||
599 | p = xdr_inline_decode(&stream, 4); | |
600 | if (unlikely(!p)) | |
601 | goto out_err_free_deviceid; | |
16b374ca | 602 | |
35124a09 | 603 | mp_count = be32_to_cpup(p); /* multipath count */ |
35124a09 | 604 | for (j = 0; j < mp_count; j++) { |
17094272 SK |
605 | da = decode_ds_addr(NFS_SERVER(ino)->nfs_client->net, |
606 | &stream, gfp_flags); | |
14f9a607 WAA |
607 | if (da) |
608 | list_add_tail(&da->da_node, &dsaddrs); | |
609 | } | |
610 | if (list_empty(&dsaddrs)) { | |
611 | dprintk("%s: no suitable DS addresses found\n", | |
612 | __func__); | |
613 | goto out_err_free_deviceid; | |
614 | } | |
615 | ||
616 | dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags); | |
617 | if (!dsaddr->ds_list[i]) | |
618 | goto out_err_drain_dsaddrs; | |
619 | ||
620 | /* If DS was already in cache, free ds addrs */ | |
621 | while (!list_empty(&dsaddrs)) { | |
622 | da = list_first_entry(&dsaddrs, | |
623 | struct nfs4_pnfs_ds_addr, | |
624 | da_node); | |
625 | list_del_init(&da->da_node); | |
626 | kfree(da->da_remotestr); | |
627 | kfree(da); | |
16b374ca AA |
628 | } |
629 | } | |
35124a09 WAA |
630 | |
631 | __free_page(scratch); | |
16b374ca AA |
632 | return dsaddr; |
633 | ||
14f9a607 WAA |
634 | out_err_drain_dsaddrs: |
635 | while (!list_empty(&dsaddrs)) { | |
636 | da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr, | |
637 | da_node); | |
638 | list_del_init(&da->da_node); | |
639 | kfree(da->da_remotestr); | |
640 | kfree(da); | |
641 | } | |
35124a09 | 642 | out_err_free_deviceid: |
16b374ca | 643 | nfs4_fl_free_deviceid(dsaddr); |
35124a09 WAA |
644 | /* stripe_indicies was part of dsaddr */ |
645 | goto out_err_free_scratch; | |
646 | out_err_free_stripe_indices: | |
647 | kfree(stripe_indices); | |
648 | out_err_free_scratch: | |
649 | __free_page(scratch); | |
16b374ca AA |
650 | out_err: |
651 | dprintk("%s ERROR: returning NULL\n", __func__); | |
652 | return NULL; | |
653 | } | |
654 | ||
655 | /* | |
ea8eecdd CH |
656 | * Decode the opaque device specified in 'dev' and add it to the cache of |
657 | * available devices. | |
16b374ca | 658 | */ |
ea8eecdd | 659 | static struct nfs4_file_layout_dsaddr * |
a75b9df9 | 660 | decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags) |
16b374ca | 661 | { |
a1eaecbc BH |
662 | struct nfs4_deviceid_node *d; |
663 | struct nfs4_file_layout_dsaddr *n, *new; | |
16b374ca | 664 | |
a75b9df9 | 665 | new = decode_device(inode, dev, gfp_flags); |
ea8eecdd | 666 | if (!new) { |
a030889a | 667 | printk(KERN_WARNING "NFS: %s: Could not decode or add device\n", |
16b374ca AA |
668 | __func__); |
669 | return NULL; | |
670 | } | |
671 | ||
a1eaecbc BH |
672 | d = nfs4_insert_deviceid_node(&new->id_node); |
673 | n = container_of(d, struct nfs4_file_layout_dsaddr, id_node); | |
674 | if (n != new) { | |
ea8eecdd | 675 | nfs4_fl_free_deviceid(new); |
a1eaecbc | 676 | return n; |
ea8eecdd CH |
677 | } |
678 | ||
ea8eecdd | 679 | return new; |
16b374ca AA |
680 | } |
681 | ||
682 | /* | |
683 | * Retrieve the information for dev_id, add it to the list | |
684 | * of available devices, and return it. | |
685 | */ | |
686 | struct nfs4_file_layout_dsaddr * | |
a75b9df9 | 687 | get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id, gfp_t gfp_flags) |
16b374ca AA |
688 | { |
689 | struct pnfs_device *pdev = NULL; | |
690 | u32 max_resp_sz; | |
691 | int max_pages; | |
692 | struct page **pages = NULL; | |
693 | struct nfs4_file_layout_dsaddr *dsaddr = NULL; | |
694 | int rc, i; | |
695 | struct nfs_server *server = NFS_SERVER(inode); | |
696 | ||
697 | /* | |
698 | * Use the session max response size as the basis for setting | |
699 | * GETDEVICEINFO's maxcount | |
700 | */ | |
701 | max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz; | |
e5265a0c | 702 | max_pages = nfs_page_array_len(0, max_resp_sz); |
16b374ca AA |
703 | dprintk("%s inode %p max_resp_sz %u max_pages %d\n", |
704 | __func__, inode, max_resp_sz, max_pages); | |
705 | ||
a75b9df9 | 706 | pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags); |
16b374ca AA |
707 | if (pdev == NULL) |
708 | return NULL; | |
709 | ||
a75b9df9 | 710 | pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags); |
16b374ca AA |
711 | if (pages == NULL) { |
712 | kfree(pdev); | |
713 | return NULL; | |
714 | } | |
715 | for (i = 0; i < max_pages; i++) { | |
a75b9df9 | 716 | pages[i] = alloc_page(gfp_flags); |
16b374ca AA |
717 | if (!pages[i]) |
718 | goto out_free; | |
719 | } | |
720 | ||
16b374ca AA |
721 | memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id)); |
722 | pdev->layout_type = LAYOUT_NFSV4_1_FILES; | |
723 | pdev->pages = pages; | |
724 | pdev->pgbase = 0; | |
725 | pdev->pglen = PAGE_SIZE * max_pages; | |
726 | pdev->mincount = 0; | |
727 | ||
728 | rc = nfs4_proc_getdeviceinfo(server, pdev); | |
729 | dprintk("%s getdevice info returns %d\n", __func__, rc); | |
730 | if (rc) | |
731 | goto out_free; | |
732 | ||
733 | /* | |
734 | * Found new device, need to decode it and then add it to the | |
735 | * list of known devices for this mountpoint. | |
736 | */ | |
a75b9df9 | 737 | dsaddr = decode_and_add_device(inode, pdev, gfp_flags); |
16b374ca | 738 | out_free: |
16b374ca AA |
739 | for (i = 0; i < max_pages; i++) |
740 | __free_page(pages[i]); | |
741 | kfree(pages); | |
742 | kfree(pdev); | |
743 | dprintk("<-- %s dsaddr %p\n", __func__, dsaddr); | |
744 | return dsaddr; | |
745 | } | |
746 | ||
ea8eecdd CH |
747 | void |
748 | nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr) | |
16b374ca | 749 | { |
1775bc34 | 750 | nfs4_put_deviceid_node(&dsaddr->id_node); |
16b374ca | 751 | } |
cfe7f412 FI |
752 | |
753 | /* | |
754 | * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit | |
755 | * Then: ((res + fsi) % dsaddr->stripe_count) | |
756 | */ | |
757 | u32 | |
758 | nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset) | |
759 | { | |
760 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); | |
761 | u64 tmp; | |
762 | ||
763 | tmp = offset - flseg->pattern_offset; | |
764 | do_div(tmp, flseg->stripe_unit); | |
765 | tmp += flseg->first_stripe_index; | |
766 | return do_div(tmp, flseg->dsaddr->stripe_count); | |
767 | } | |
768 | ||
769 | u32 | |
770 | nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j) | |
771 | { | |
772 | return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j]; | |
773 | } | |
774 | ||
775 | struct nfs_fh * | |
776 | nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j) | |
777 | { | |
778 | struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg); | |
779 | u32 i; | |
780 | ||
781 | if (flseg->stripe_type == STRIPE_SPARSE) { | |
782 | if (flseg->num_fh == 1) | |
783 | i = 0; | |
784 | else if (flseg->num_fh == 0) | |
785 | /* Use the MDS OPEN fh set in nfs_read_rpcsetup */ | |
786 | return NULL; | |
787 | else | |
788 | i = nfs4_fl_calc_ds_index(lseg, j); | |
789 | } else | |
790 | i = j; | |
791 | return flseg->fh_array[i]; | |
792 | } | |
793 | ||
568e8c49 AA |
794 | static void |
795 | filelayout_mark_devid_negative(struct nfs4_file_layout_dsaddr *dsaddr, | |
c9895cb6 | 796 | int err, const char *ds_remotestr) |
568e8c49 | 797 | { |
a1eaecbc | 798 | u32 *p = (u32 *)&dsaddr->id_node.deviceid; |
568e8c49 | 799 | |
c9895cb6 | 800 | printk(KERN_ERR "NFS: data server %s connection error %d." |
568e8c49 | 801 | " Deviceid [%x%x%x%x] marked out of use.\n", |
c9895cb6 | 802 | ds_remotestr, err, p[0], p[1], p[2], p[3]); |
568e8c49 | 803 | |
a1eaecbc | 804 | spin_lock(&nfs4_ds_cache_lock); |
568e8c49 | 805 | dsaddr->flags |= NFS4_DEVICE_ID_NEG_ENTRY; |
a1eaecbc | 806 | spin_unlock(&nfs4_ds_cache_lock); |
568e8c49 AA |
807 | } |
808 | ||
cfe7f412 FI |
809 | struct nfs4_pnfs_ds * |
810 | nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx) | |
811 | { | |
812 | struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr; | |
813 | struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx]; | |
814 | ||
815 | if (ds == NULL) { | |
a030889a | 816 | printk(KERN_ERR "NFS: %s: No data server for offset index %d\n", |
cfe7f412 FI |
817 | __func__, ds_idx); |
818 | return NULL; | |
819 | } | |
820 | ||
821 | if (!ds->ds_clp) { | |
568e8c49 | 822 | struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode); |
cfe7f412 FI |
823 | int err; |
824 | ||
568e8c49 AA |
825 | if (dsaddr->flags & NFS4_DEVICE_ID_NEG_ENTRY) { |
826 | /* Already tried to connect, don't try again */ | |
827 | dprintk("%s Deviceid marked out of use\n", __func__); | |
828 | return NULL; | |
829 | } | |
830 | err = nfs4_ds_connect(s, ds); | |
cfe7f412 | 831 | if (err) { |
568e8c49 | 832 | filelayout_mark_devid_negative(dsaddr, err, |
c9895cb6 | 833 | ds->ds_remotestr); |
cfe7f412 FI |
834 | return NULL; |
835 | } | |
836 | } | |
837 | return ds; | |
838 | } |