]>
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 | */ | |
48 | DEFINE_SPINLOCK(nfs4_ds_cache_lock); | |
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 | ||
16b374ca AA |
68 | /* nfs4_ds_cache_lock is held */ |
69 | static struct nfs4_pnfs_ds * | |
c9895cb6 | 70 | _data_server_lookup_locked(struct sockaddr *addr, size_t addrlen) |
16b374ca AA |
71 | { |
72 | struct nfs4_pnfs_ds *ds; | |
c9895cb6 WAA |
73 | struct sockaddr_in *a, *b; |
74 | struct sockaddr_in6 *a6, *b6; | |
16b374ca AA |
75 | |
76 | list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) { | |
c9895cb6 WAA |
77 | 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; | |
16b374ca AA |
109 | } |
110 | } | |
111 | return NULL; | |
112 | } | |
113 | ||
d83217c1 AA |
114 | /* |
115 | * Create an rpc connection to the nfs4_pnfs_ds data server | |
116 | * Currently only support IPv4 | |
117 | */ | |
118 | static int | |
119 | nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds) | |
120 | { | |
121 | struct nfs_client *clp; | |
d83217c1 AA |
122 | int status = 0; |
123 | ||
c9895cb6 | 124 | dprintk("--> %s addr %s au_flavor %d\n", __func__, ds->ds_remotestr, |
d83217c1 AA |
125 | mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor); |
126 | ||
c9895cb6 WAA |
127 | clp = nfs4_set_ds_client(mds_srv->nfs_client, |
128 | (struct sockaddr *)&ds->ds_addr, | |
129 | ds->ds_addrlen, IPPROTO_TCP); | |
d83217c1 AA |
130 | 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; | |
c9895cb6 WAA |
141 | dprintk("%s [existing] server=%s\n", __func__, |
142 | ds->ds_remotestr); | |
d83217c1 AA |
143 | 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; | |
c9895cb6 | 161 | dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); |
d83217c1 AA |
162 | out: |
163 | return status; | |
164 | out_put: | |
165 | nfs_put_client(clp); | |
166 | goto out; | |
167 | } | |
168 | ||
16b374ca AA |
169 | static void |
170 | destroy_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); | |
c9895cb6 | 178 | kfree(ds->ds_remotestr); |
16b374ca AA |
179 | kfree(ds); |
180 | } | |
181 | ||
1775bc34 | 182 | void |
16b374ca AA |
183 | nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr) |
184 | { | |
185 | struct nfs4_pnfs_ds *ds; | |
186 | int i; | |
187 | ||
a1eaecbc | 188 | nfs4_print_deviceid(&dsaddr->id_node.deviceid); |
16b374ca AA |
189 | |
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 | ||
c9895cb6 WAA |
205 | /* |
206 | * Create a string with a human readable address and port to avoid | |
207 | * complicated setup around many dprinks. | |
208 | */ | |
209 | static char * | |
210 | nfs4_pnfs_remotestr(struct sockaddr *ds_addr, gfp_t gfp_flags) | |
211 | { | |
212 | 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 | ||
16b374ca | 253 | static struct nfs4_pnfs_ds * |
c9895cb6 | 254 | nfs4_pnfs_ds_add(struct sockaddr *addr, size_t addrlen, gfp_t gfp_flags) |
16b374ca | 255 | { |
c9895cb6 WAA |
256 | struct nfs4_pnfs_ds *tmp_ds, *ds = NULL; |
257 | char *remotestr; | |
16b374ca | 258 | |
a75b9df9 | 259 | ds = kzalloc(sizeof(*tmp_ds), gfp_flags); |
16b374ca AA |
260 | if (!ds) |
261 | goto out; | |
262 | ||
c9895cb6 WAA |
263 | /* this is only used for debugging, so it's ok if its NULL */ |
264 | remotestr = nfs4_pnfs_remotestr(addr, gfp_flags); | |
265 | ||
16b374ca | 266 | spin_lock(&nfs4_ds_cache_lock); |
c9895cb6 | 267 | tmp_ds = _data_server_lookup_locked(addr, addrlen); |
16b374ca | 268 | if (tmp_ds == NULL) { |
c9895cb6 WAA |
269 | memcpy(&ds->ds_addr, addr, addrlen); |
270 | ds->ds_addrlen = addrlen; | |
271 | ds->ds_remotestr = remotestr; | |
16b374ca AA |
272 | 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); | |
c9895cb6 WAA |
276 | dprintk("%s add new data server %s\n", __func__, |
277 | ds->ds_remotestr); | |
16b374ca | 278 | } else { |
c9895cb6 | 279 | kfree(remotestr); |
16b374ca AA |
280 | kfree(ds); |
281 | atomic_inc(&tmp_ds->ds_count); | |
c9895cb6 WAA |
282 | dprintk("%s data server %s found, inc'ed ds_count to %d\n", |
283 | __func__, tmp_ds->ds_remotestr, | |
16b374ca AA |
284 | atomic_read(&tmp_ds->ds_count)); |
285 | ds = tmp_ds; | |
286 | } | |
287 | spin_unlock(&nfs4_ds_cache_lock); | |
288 | out: | |
289 | return ds; | |
290 | } | |
291 | ||
292 | /* | |
c9895cb6 | 293 | * Currently only supports ipv4, ipv6 and one multi-path address. |
16b374ca AA |
294 | */ |
295 | static struct nfs4_pnfs_ds * | |
a75b9df9 | 296 | decode_and_add_ds(struct xdr_stream *streamp, struct inode *inode, gfp_t gfp_flags) |
16b374ca AA |
297 | { |
298 | struct nfs4_pnfs_ds *ds = NULL; | |
c9895cb6 WAA |
299 | char *buf, *portstr; |
300 | struct sockaddr_storage ss; | |
301 | size_t sslen; | |
302 | u32 port; | |
303 | int nlen, rlen; | |
16b374ca | 304 | int tmp[2]; |
35124a09 | 305 | __be32 *p; |
c9895cb6 WAA |
306 | char *netid, *match_netid; |
307 | size_t match_netid_len; | |
16b374ca AA |
308 | |
309 | /* r_netid */ | |
35124a09 WAA |
310 | p = xdr_inline_decode(streamp, 4); |
311 | if (unlikely(!p)) | |
312 | goto out_err; | |
16b374ca | 313 | nlen = be32_to_cpup(p++); |
16b374ca | 314 | |
35124a09 WAA |
315 | p = xdr_inline_decode(streamp, nlen); |
316 | if (unlikely(!p)) | |
317 | goto out_err; | |
16b374ca | 318 | |
c9895cb6 WAA |
319 | netid = kmalloc(nlen+1, gfp_flags); |
320 | if (unlikely(!netid)) | |
16b374ca | 321 | goto out_err; |
16b374ca | 322 | |
c9895cb6 WAA |
323 | netid[nlen] = '\0'; |
324 | memcpy(netid, p, nlen); | |
325 | ||
326 | /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */ | |
35124a09 WAA |
327 | p = xdr_inline_decode(streamp, 4); |
328 | if (unlikely(!p)) | |
c9895cb6 | 329 | goto out_free_netid; |
35124a09 WAA |
330 | rlen = be32_to_cpup(p); |
331 | ||
332 | p = xdr_inline_decode(streamp, rlen); | |
333 | if (unlikely(!p)) | |
c9895cb6 | 334 | goto out_free_netid; |
35124a09 | 335 | |
c9895cb6 WAA |
336 | /* port is ".ABC.DEF", 8 chars max */ |
337 | if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) { | |
ad3d2eed | 338 | dprintk("%s: Invalid address, length %d\n", __func__, |
16b374ca | 339 | rlen); |
c9895cb6 | 340 | goto out_free_netid; |
16b374ca | 341 | } |
a75b9df9 | 342 | buf = kmalloc(rlen + 1, gfp_flags); |
b9f81057 SF |
343 | if (!buf) { |
344 | dprintk("%s: Not enough memory\n", __func__); | |
c9895cb6 | 345 | goto out_free_netid; |
b9f81057 | 346 | } |
16b374ca | 347 | buf[rlen] = '\0'; |
35124a09 | 348 | memcpy(buf, p, rlen); |
16b374ca | 349 | |
c9895cb6 WAA |
350 | /* 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; | |
16b374ca | 365 | } |
c9895cb6 | 366 | *portstr = '\0'; |
16b374ca | 367 | |
c9895cb6 WAA |
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; | |
16b374ca AA |
371 | } |
372 | ||
c9895cb6 WAA |
373 | portstr++; |
374 | sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]); | |
16b374ca AA |
375 | port = htons((tmp[0] << 8) | (tmp[1])); |
376 | ||
c9895cb6 WAA |
377 | 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); | |
406 | out_free_buf: | |
16b374ca | 407 | kfree(buf); |
c9895cb6 WAA |
408 | out_free_netid: |
409 | kfree(netid); | |
16b374ca AA |
410 | out_err: |
411 | return ds; | |
412 | } | |
413 | ||
414 | /* Decode opaque device data and return the result */ | |
415 | static struct nfs4_file_layout_dsaddr* | |
a75b9df9 | 416 | decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags) |
16b374ca | 417 | { |
35124a09 | 418 | int i; |
16b374ca AA |
419 | u32 cnt, num; |
420 | u8 *indexp; | |
35124a09 WAA |
421 | __be32 *p; |
422 | u8 *stripe_indices; | |
423 | u8 max_stripe_index; | |
424 | struct nfs4_file_layout_dsaddr *dsaddr = NULL; | |
425 | struct xdr_stream stream; | |
f7da7a12 | 426 | struct xdr_buf buf; |
35124a09 WAA |
427 | struct page *scratch; |
428 | ||
429 | /* set up xdr stream */ | |
a75b9df9 | 430 | scratch = alloc_page(gfp_flags); |
35124a09 WAA |
431 | if (!scratch) |
432 | goto out_err; | |
433 | ||
f7da7a12 | 434 | xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen); |
35124a09 | 435 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
16b374ca AA |
436 | |
437 | /* Get the stripe count (number of stripe index) */ | |
35124a09 WAA |
438 | p = xdr_inline_decode(&stream, 4); |
439 | if (unlikely(!p)) | |
440 | goto out_err_free_scratch; | |
441 | ||
442 | cnt = be32_to_cpup(p); | |
16b374ca AA |
443 | 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); | |
35124a09 WAA |
448 | goto out_err_free_scratch; |
449 | } | |
450 | ||
451 | /* read stripe indices */ | |
a75b9df9 | 452 | stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags); |
35124a09 WAA |
453 | 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++; | |
16b374ca AA |
466 | } |
467 | ||
468 | /* Check the multipath list count */ | |
35124a09 WAA |
469 | p = xdr_inline_decode(&stream, 4); |
470 | if (unlikely(!p)) | |
471 | goto out_err_free_stripe_indices; | |
472 | ||
473 | num = be32_to_cpup(p); | |
16b374ca AA |
474 | 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); | |
35124a09 | 479 | goto out_err_free_stripe_indices; |
16b374ca | 480 | } |
35124a09 WAA |
481 | |
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 | ||
16b374ca AA |
489 | dsaddr = kzalloc(sizeof(*dsaddr) + |
490 | (sizeof(struct nfs4_pnfs_ds *) * (num - 1)), | |
a75b9df9 | 491 | gfp_flags); |
16b374ca | 492 | if (!dsaddr) |
35124a09 | 493 | goto out_err_free_stripe_indices; |
16b374ca AA |
494 | |
495 | dsaddr->stripe_count = cnt; | |
35124a09 WAA |
496 | dsaddr->stripe_indices = stripe_indices; |
497 | stripe_indices = NULL; | |
16b374ca | 498 | dsaddr->ds_num = num; |
1775bc34 BH |
499 | nfs4_init_deviceid_node(&dsaddr->id_node, |
500 | NFS_SERVER(ino)->pnfs_curr_ld, | |
501 | NFS_SERVER(ino)->nfs_client, | |
a1eaecbc | 502 | &pdev->dev_id); |
16b374ca | 503 | |
16b374ca AA |
504 | for (i = 0; i < dsaddr->ds_num; i++) { |
505 | int j; | |
35124a09 WAA |
506 | u32 mp_count; |
507 | ||
508 | p = xdr_inline_decode(&stream, 4); | |
509 | if (unlikely(!p)) | |
510 | goto out_err_free_deviceid; | |
16b374ca | 511 | |
35124a09 WAA |
512 | mp_count = be32_to_cpup(p); /* multipath count */ |
513 | if (mp_count > 1) { | |
16b374ca AA |
514 | printk(KERN_WARNING |
515 | "%s: Multipath count %d not supported, " | |
516 | "skipping all greater than 1\n", __func__, | |
35124a09 | 517 | mp_count); |
16b374ca | 518 | } |
35124a09 | 519 | for (j = 0; j < mp_count; j++) { |
16b374ca | 520 | if (j == 0) { |
35124a09 | 521 | dsaddr->ds_list[i] = decode_and_add_ds(&stream, |
a75b9df9 | 522 | ino, gfp_flags); |
16b374ca | 523 | if (dsaddr->ds_list[i] == NULL) |
35124a09 | 524 | goto out_err_free_deviceid; |
16b374ca AA |
525 | } else { |
526 | u32 len; | |
527 | /* skip extra multipath */ | |
35124a09 WAA |
528 | |
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; | |
16b374ca AA |
548 | } |
549 | } | |
550 | } | |
35124a09 WAA |
551 | |
552 | __free_page(scratch); | |
16b374ca AA |
553 | return dsaddr; |
554 | ||
35124a09 | 555 | out_err_free_deviceid: |
16b374ca | 556 | nfs4_fl_free_deviceid(dsaddr); |
35124a09 WAA |
557 | /* stripe_indicies was part of dsaddr */ |
558 | goto out_err_free_scratch; | |
559 | out_err_free_stripe_indices: | |
560 | kfree(stripe_indices); | |
561 | out_err_free_scratch: | |
562 | __free_page(scratch); | |
16b374ca AA |
563 | out_err: |
564 | dprintk("%s ERROR: returning NULL\n", __func__); | |
565 | return NULL; | |
566 | } | |
567 | ||
568 | /* | |
ea8eecdd CH |
569 | * Decode the opaque device specified in 'dev' and add it to the cache of |
570 | * available devices. | |
16b374ca | 571 | */ |
ea8eecdd | 572 | static struct nfs4_file_layout_dsaddr * |
a75b9df9 | 573 | decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags) |
16b374ca | 574 | { |
a1eaecbc BH |
575 | struct nfs4_deviceid_node *d; |
576 | struct nfs4_file_layout_dsaddr *n, *new; | |
16b374ca | 577 | |
a75b9df9 | 578 | new = decode_device(inode, dev, gfp_flags); |
ea8eecdd | 579 | if (!new) { |
16b374ca AA |
580 | printk(KERN_WARNING "%s: Could not decode or add device\n", |
581 | __func__); | |
582 | return NULL; | |
583 | } | |
584 | ||
a1eaecbc BH |
585 | d = nfs4_insert_deviceid_node(&new->id_node); |
586 | n = container_of(d, struct nfs4_file_layout_dsaddr, id_node); | |
587 | if (n != new) { | |
ea8eecdd | 588 | nfs4_fl_free_deviceid(new); |
a1eaecbc | 589 | return n; |
ea8eecdd CH |
590 | } |
591 | ||
ea8eecdd | 592 | return new; |
16b374ca AA |
593 | } |
594 | ||
595 | /* | |
596 | * Retrieve the information for dev_id, add it to the list | |
597 | * of available devices, and return it. | |
598 | */ | |
599 | struct nfs4_file_layout_dsaddr * | |
a75b9df9 | 600 | get_device_info(struct inode *inode, struct nfs4_deviceid *dev_id, gfp_t gfp_flags) |
16b374ca AA |
601 | { |
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 | ||
a75b9df9 | 619 | pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags); |
16b374ca AA |
620 | if (pdev == NULL) |
621 | return NULL; | |
622 | ||
a75b9df9 | 623 | pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags); |
16b374ca AA |
624 | if (pages == NULL) { |
625 | kfree(pdev); | |
626 | return NULL; | |
627 | } | |
628 | for (i = 0; i < max_pages; i++) { | |
a75b9df9 | 629 | pages[i] = alloc_page(gfp_flags); |
16b374ca AA |
630 | if (!pages[i]) |
631 | goto out_free; | |
632 | } | |
633 | ||
16b374ca AA |
634 | 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 | */ | |
a75b9df9 | 650 | dsaddr = decode_and_add_device(inode, pdev, gfp_flags); |
16b374ca | 651 | out_free: |
16b374ca AA |
652 | 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 | ||
ea8eecdd CH |
660 | void |
661 | nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr) | |
16b374ca | 662 | { |
1775bc34 | 663 | nfs4_put_deviceid_node(&dsaddr->id_node); |
16b374ca | 664 | } |
cfe7f412 FI |
665 | |
666 | /* | |
667 | * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit | |
668 | * Then: ((res + fsi) % dsaddr->stripe_count) | |
669 | */ | |
670 | u32 | |
671 | nfs4_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 | ||
682 | u32 | |
683 | nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j) | |
684 | { | |
685 | return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j]; | |
686 | } | |
687 | ||
688 | struct nfs_fh * | |
689 | nfs4_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 | ||
568e8c49 AA |
707 | static void |
708 | filelayout_mark_devid_negative(struct nfs4_file_layout_dsaddr *dsaddr, | |
c9895cb6 | 709 | int err, const char *ds_remotestr) |
568e8c49 | 710 | { |
a1eaecbc | 711 | u32 *p = (u32 *)&dsaddr->id_node.deviceid; |
568e8c49 | 712 | |
c9895cb6 | 713 | printk(KERN_ERR "NFS: data server %s connection error %d." |
568e8c49 | 714 | " Deviceid [%x%x%x%x] marked out of use.\n", |
c9895cb6 | 715 | ds_remotestr, err, p[0], p[1], p[2], p[3]); |
568e8c49 | 716 | |
a1eaecbc | 717 | spin_lock(&nfs4_ds_cache_lock); |
568e8c49 | 718 | dsaddr->flags |= NFS4_DEVICE_ID_NEG_ENTRY; |
a1eaecbc | 719 | spin_unlock(&nfs4_ds_cache_lock); |
568e8c49 AA |
720 | } |
721 | ||
cfe7f412 FI |
722 | struct nfs4_pnfs_ds * |
723 | nfs4_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) { | |
568e8c49 | 735 | struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode); |
cfe7f412 FI |
736 | int err; |
737 | ||
568e8c49 AA |
738 | 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); | |
cfe7f412 | 744 | if (err) { |
568e8c49 | 745 | filelayout_mark_devid_negative(dsaddr, err, |
c9895cb6 | 746 | ds->ds_remotestr); |
cfe7f412 FI |
747 | return NULL; |
748 | } | |
749 | } | |
750 | return ds; | |
751 | } |