Commit | Line | Data |
---|---|---|
f54bcf2e TH |
1 | /* |
2 | * Common NFS I/O operations for the pnfs file based | |
3 | * layout drivers. | |
4 | * | |
5 | * Copyright (c) 2014, Primary Data, Inc. All rights reserved. | |
6 | * | |
7 | * Tom Haynes <loghyr@primarydata.com> | |
8 | */ | |
9 | ||
10 | #include <linux/nfs_fs.h> | |
11 | #include <linux/nfs_page.h> | |
6b7f3cf9 | 12 | #include <linux/sunrpc/addr.h> |
5f01d953 | 13 | #include <linux/module.h> |
f54bcf2e | 14 | |
7405f9e1 | 15 | #include "nfs4session.h" |
f54bcf2e TH |
16 | #include "internal.h" |
17 | #include "pnfs.h" | |
18 | ||
875ae069 PT |
19 | #define NFSDBG_FACILITY NFSDBG_PNFS |
20 | ||
f54bcf2e TH |
21 | static void pnfs_generic_fenceme(struct inode *inode, |
22 | struct pnfs_layout_hdr *lo) | |
23 | { | |
24 | if (!test_and_clear_bit(NFS_LAYOUT_RETURN, &lo->plh_flags)) | |
25 | return; | |
26 | pnfs_return_layout(inode); | |
27 | } | |
28 | ||
29 | void pnfs_generic_rw_release(void *data) | |
30 | { | |
31 | struct nfs_pgio_header *hdr = data; | |
32 | struct pnfs_layout_hdr *lo = hdr->lseg->pls_layout; | |
33 | ||
34 | pnfs_generic_fenceme(lo->plh_inode, lo); | |
35 | nfs_put_client(hdr->ds_clp); | |
36 | hdr->mds_ops->rpc_release(data); | |
37 | } | |
38 | EXPORT_SYMBOL_GPL(pnfs_generic_rw_release); | |
39 | ||
40 | /* Fake up some data that will cause nfs_commit_release to retry the writes. */ | |
41 | void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data) | |
42 | { | |
43 | struct nfs_page *first = nfs_list_entry(data->pages.next); | |
44 | ||
45 | data->task.tk_status = 0; | |
46 | memcpy(&data->verf.verifier, &first->wb_verf, | |
47 | sizeof(data->verf.verifier)); | |
48 | data->verf.verifier.data[0]++; /* ensure verifier mismatch */ | |
49 | } | |
50 | EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes); | |
51 | ||
52 | void pnfs_generic_write_commit_done(struct rpc_task *task, void *data) | |
53 | { | |
54 | struct nfs_commit_data *wdata = data; | |
55 | ||
56 | /* Note this may cause RPC to be resent */ | |
57 | wdata->mds_ops->rpc_call_done(task, data); | |
58 | } | |
59 | EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done); | |
60 | ||
61 | void pnfs_generic_commit_release(void *calldata) | |
62 | { | |
63 | struct nfs_commit_data *data = calldata; | |
64 | ||
65 | data->completion_ops->completion(data); | |
66 | pnfs_put_lseg(data->lseg); | |
67 | nfs_put_client(data->ds_clp); | |
68 | nfs_commitdata_release(data); | |
69 | } | |
70 | EXPORT_SYMBOL_GPL(pnfs_generic_commit_release); | |
71 | ||
72 | /* The generic layer is about to remove the req from the commit list. | |
73 | * If this will make the bucket empty, it will need to put the lseg reference. | |
085d1e33 | 74 | * Note this must be called holding the inode (/cinfo) lock |
f54bcf2e TH |
75 | */ |
76 | void | |
77 | pnfs_generic_clear_request_commit(struct nfs_page *req, | |
78 | struct nfs_commit_info *cinfo) | |
79 | { | |
80 | struct pnfs_layout_segment *freeme = NULL; | |
81 | ||
82 | if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags)) | |
83 | goto out; | |
84 | cinfo->ds->nwritten--; | |
85 | if (list_is_singular(&req->wb_list)) { | |
86 | struct pnfs_commit_bucket *bucket; | |
87 | ||
88 | bucket = list_first_entry(&req->wb_list, | |
89 | struct pnfs_commit_bucket, | |
90 | written); | |
91 | freeme = bucket->wlseg; | |
92 | bucket->wlseg = NULL; | |
93 | } | |
94 | out: | |
95 | nfs_request_remove_commit_list(req, cinfo); | |
96 | pnfs_put_lseg_locked(freeme); | |
97 | } | |
98 | EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit); | |
99 | ||
100 | static int | |
101 | pnfs_generic_transfer_commit_list(struct list_head *src, struct list_head *dst, | |
102 | struct nfs_commit_info *cinfo, int max) | |
103 | { | |
104 | struct nfs_page *req, *tmp; | |
105 | int ret = 0; | |
106 | ||
107 | list_for_each_entry_safe(req, tmp, src, wb_list) { | |
108 | if (!nfs_lock_request(req)) | |
109 | continue; | |
110 | kref_get(&req->wb_kref); | |
111 | if (cond_resched_lock(cinfo->lock)) | |
112 | list_safe_reset_next(req, tmp, wb_list); | |
113 | nfs_request_remove_commit_list(req, cinfo); | |
114 | clear_bit(PG_COMMIT_TO_DS, &req->wb_flags); | |
115 | nfs_list_add_request(req, dst); | |
116 | ret++; | |
117 | if ((ret == max) && !cinfo->dreq) | |
118 | break; | |
119 | } | |
120 | return ret; | |
121 | } | |
122 | ||
f54bcf2e TH |
123 | static int |
124 | pnfs_generic_scan_ds_commit_list(struct pnfs_commit_bucket *bucket, | |
125 | struct nfs_commit_info *cinfo, | |
126 | int max) | |
127 | { | |
128 | struct list_head *src = &bucket->written; | |
129 | struct list_head *dst = &bucket->committing; | |
130 | int ret; | |
131 | ||
085d1e33 | 132 | lockdep_assert_held(cinfo->lock); |
f54bcf2e TH |
133 | ret = pnfs_generic_transfer_commit_list(src, dst, cinfo, max); |
134 | if (ret) { | |
135 | cinfo->ds->nwritten -= ret; | |
136 | cinfo->ds->ncommitting += ret; | |
137 | bucket->clseg = bucket->wlseg; | |
138 | if (list_empty(src)) | |
139 | bucket->wlseg = NULL; | |
140 | else | |
141 | pnfs_get_lseg(bucket->clseg); | |
142 | } | |
143 | return ret; | |
144 | } | |
145 | ||
085d1e33 TH |
146 | /* Move reqs from written to committing lists, returning count |
147 | * of number moved. | |
f54bcf2e TH |
148 | */ |
149 | int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo, | |
150 | int max) | |
151 | { | |
152 | int i, rv = 0, cnt; | |
153 | ||
085d1e33 | 154 | lockdep_assert_held(cinfo->lock); |
f54bcf2e TH |
155 | for (i = 0; i < cinfo->ds->nbuckets && max != 0; i++) { |
156 | cnt = pnfs_generic_scan_ds_commit_list(&cinfo->ds->buckets[i], | |
157 | cinfo, max); | |
158 | max -= cnt; | |
159 | rv += cnt; | |
160 | } | |
161 | return rv; | |
162 | } | |
163 | EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists); | |
164 | ||
085d1e33 | 165 | /* Pull everything off the committing lists and dump into @dst. */ |
f54bcf2e TH |
166 | void pnfs_generic_recover_commit_reqs(struct list_head *dst, |
167 | struct nfs_commit_info *cinfo) | |
168 | { | |
169 | struct pnfs_commit_bucket *b; | |
170 | struct pnfs_layout_segment *freeme; | |
171 | int i; | |
172 | ||
085d1e33 | 173 | lockdep_assert_held(cinfo->lock); |
f54bcf2e | 174 | restart: |
f54bcf2e TH |
175 | for (i = 0, b = cinfo->ds->buckets; i < cinfo->ds->nbuckets; i++, b++) { |
176 | if (pnfs_generic_transfer_commit_list(&b->written, dst, | |
177 | cinfo, 0)) { | |
178 | freeme = b->wlseg; | |
179 | b->wlseg = NULL; | |
180 | spin_unlock(cinfo->lock); | |
181 | pnfs_put_lseg(freeme); | |
085d1e33 | 182 | spin_lock(cinfo->lock); |
f54bcf2e TH |
183 | goto restart; |
184 | } | |
185 | } | |
186 | cinfo->ds->nwritten = 0; | |
f54bcf2e TH |
187 | } |
188 | EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs); | |
189 | ||
190 | static void pnfs_generic_retry_commit(struct nfs_commit_info *cinfo, int idx) | |
191 | { | |
192 | struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds; | |
193 | struct pnfs_commit_bucket *bucket; | |
194 | struct pnfs_layout_segment *freeme; | |
195 | int i; | |
196 | ||
197 | for (i = idx; i < fl_cinfo->nbuckets; i++) { | |
198 | bucket = &fl_cinfo->buckets[i]; | |
199 | if (list_empty(&bucket->committing)) | |
200 | continue; | |
201 | nfs_retry_commit(&bucket->committing, bucket->clseg, cinfo); | |
202 | spin_lock(cinfo->lock); | |
203 | freeme = bucket->clseg; | |
204 | bucket->clseg = NULL; | |
205 | spin_unlock(cinfo->lock); | |
206 | pnfs_put_lseg(freeme); | |
207 | } | |
208 | } | |
209 | ||
210 | static unsigned int | |
211 | pnfs_generic_alloc_ds_commits(struct nfs_commit_info *cinfo, | |
212 | struct list_head *list) | |
213 | { | |
214 | struct pnfs_ds_commit_info *fl_cinfo; | |
215 | struct pnfs_commit_bucket *bucket; | |
216 | struct nfs_commit_data *data; | |
217 | int i; | |
218 | unsigned int nreq = 0; | |
219 | ||
220 | fl_cinfo = cinfo->ds; | |
221 | bucket = fl_cinfo->buckets; | |
222 | for (i = 0; i < fl_cinfo->nbuckets; i++, bucket++) { | |
223 | if (list_empty(&bucket->committing)) | |
224 | continue; | |
225 | data = nfs_commitdata_alloc(); | |
226 | if (!data) | |
227 | break; | |
228 | data->ds_commit_index = i; | |
229 | spin_lock(cinfo->lock); | |
230 | data->lseg = bucket->clseg; | |
231 | bucket->clseg = NULL; | |
232 | spin_unlock(cinfo->lock); | |
233 | list_add(&data->pages, list); | |
234 | nreq++; | |
235 | } | |
236 | ||
237 | /* Clean up on error */ | |
238 | pnfs_generic_retry_commit(cinfo, i); | |
239 | return nreq; | |
240 | } | |
241 | ||
242 | /* This follows nfs_commit_list pretty closely */ | |
243 | int | |
244 | pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages, | |
245 | int how, struct nfs_commit_info *cinfo, | |
246 | int (*initiate_commit)(struct nfs_commit_data *data, | |
247 | int how)) | |
248 | { | |
249 | struct nfs_commit_data *data, *tmp; | |
250 | LIST_HEAD(list); | |
251 | unsigned int nreq = 0; | |
252 | ||
253 | if (!list_empty(mds_pages)) { | |
254 | data = nfs_commitdata_alloc(); | |
255 | if (data != NULL) { | |
256 | data->lseg = NULL; | |
257 | list_add(&data->pages, &list); | |
258 | nreq++; | |
259 | } else { | |
260 | nfs_retry_commit(mds_pages, NULL, cinfo); | |
261 | pnfs_generic_retry_commit(cinfo, 0); | |
262 | cinfo->completion_ops->error_cleanup(NFS_I(inode)); | |
263 | return -ENOMEM; | |
264 | } | |
265 | } | |
266 | ||
267 | nreq += pnfs_generic_alloc_ds_commits(cinfo, &list); | |
268 | ||
269 | if (nreq == 0) { | |
270 | cinfo->completion_ops->error_cleanup(NFS_I(inode)); | |
271 | goto out; | |
272 | } | |
273 | ||
274 | atomic_add(nreq, &cinfo->mds->rpcs_out); | |
275 | ||
276 | list_for_each_entry_safe(data, tmp, &list, pages) { | |
277 | list_del_init(&data->pages); | |
278 | if (!data->lseg) { | |
279 | nfs_init_commit(data, mds_pages, NULL, cinfo); | |
280 | nfs_initiate_commit(NFS_CLIENT(inode), data, | |
281 | data->mds_ops, how, 0); | |
282 | } else { | |
283 | struct pnfs_commit_bucket *buckets; | |
284 | ||
285 | buckets = cinfo->ds->buckets; | |
286 | nfs_init_commit(data, | |
287 | &buckets[data->ds_commit_index].committing, | |
288 | data->lseg, | |
289 | cinfo); | |
290 | initiate_commit(data, how); | |
291 | } | |
292 | } | |
293 | out: | |
294 | cinfo->ds->ncommitting = 0; | |
295 | return PNFS_ATTEMPTED; | |
296 | } | |
297 | EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist); | |
875ae069 PT |
298 | |
299 | /* | |
300 | * Data server cache | |
301 | * | |
302 | * Data servers can be mapped to different device ids. | |
303 | * nfs4_pnfs_ds reference counting | |
304 | * - set to 1 on allocation | |
305 | * - incremented when a device id maps a data server already in the cache. | |
306 | * - decremented when deviceid is removed from the cache. | |
307 | */ | |
308 | static DEFINE_SPINLOCK(nfs4_ds_cache_lock); | |
309 | static LIST_HEAD(nfs4_data_server_cache); | |
310 | ||
311 | /* Debug routines */ | |
312 | static void | |
313 | print_ds(struct nfs4_pnfs_ds *ds) | |
314 | { | |
315 | if (ds == NULL) { | |
316 | printk(KERN_WARNING "%s NULL device\n", __func__); | |
317 | return; | |
318 | } | |
319 | printk(KERN_WARNING " ds %s\n" | |
320 | " ref count %d\n" | |
321 | " client %p\n" | |
322 | " cl_exchange_flags %x\n", | |
323 | ds->ds_remotestr, | |
324 | atomic_read(&ds->ds_count), ds->ds_clp, | |
325 | ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0); | |
326 | } | |
327 | ||
328 | static bool | |
329 | same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2) | |
330 | { | |
331 | struct sockaddr_in *a, *b; | |
332 | struct sockaddr_in6 *a6, *b6; | |
333 | ||
334 | if (addr1->sa_family != addr2->sa_family) | |
335 | return false; | |
336 | ||
337 | switch (addr1->sa_family) { | |
338 | case AF_INET: | |
339 | a = (struct sockaddr_in *)addr1; | |
340 | b = (struct sockaddr_in *)addr2; | |
341 | ||
342 | if (a->sin_addr.s_addr == b->sin_addr.s_addr && | |
343 | a->sin_port == b->sin_port) | |
344 | return true; | |
345 | break; | |
346 | ||
347 | case AF_INET6: | |
348 | a6 = (struct sockaddr_in6 *)addr1; | |
349 | b6 = (struct sockaddr_in6 *)addr2; | |
350 | ||
351 | /* LINKLOCAL addresses must have matching scope_id */ | |
352 | if (ipv6_addr_src_scope(&a6->sin6_addr) == | |
353 | IPV6_ADDR_SCOPE_LINKLOCAL && | |
354 | a6->sin6_scope_id != b6->sin6_scope_id) | |
355 | return false; | |
356 | ||
357 | if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) && | |
358 | a6->sin6_port == b6->sin6_port) | |
359 | return true; | |
360 | break; | |
361 | ||
362 | default: | |
363 | dprintk("%s: unhandled address family: %u\n", | |
364 | __func__, addr1->sa_family); | |
365 | return false; | |
366 | } | |
367 | ||
368 | return false; | |
369 | } | |
370 | ||
371 | static bool | |
372 | _same_data_server_addrs_locked(const struct list_head *dsaddrs1, | |
373 | const struct list_head *dsaddrs2) | |
374 | { | |
375 | struct nfs4_pnfs_ds_addr *da1, *da2; | |
376 | ||
377 | /* step through both lists, comparing as we go */ | |
378 | for (da1 = list_first_entry(dsaddrs1, typeof(*da1), da_node), | |
379 | da2 = list_first_entry(dsaddrs2, typeof(*da2), da_node); | |
380 | da1 != NULL && da2 != NULL; | |
381 | da1 = list_entry(da1->da_node.next, typeof(*da1), da_node), | |
382 | da2 = list_entry(da2->da_node.next, typeof(*da2), da_node)) { | |
383 | if (!same_sockaddr((struct sockaddr *)&da1->da_addr, | |
384 | (struct sockaddr *)&da2->da_addr)) | |
385 | return false; | |
386 | } | |
387 | if (da1 == NULL && da2 == NULL) | |
388 | return true; | |
389 | ||
390 | return false; | |
391 | } | |
392 | ||
393 | /* | |
394 | * Lookup DS by addresses. nfs4_ds_cache_lock is held | |
395 | */ | |
396 | static struct nfs4_pnfs_ds * | |
397 | _data_server_lookup_locked(const struct list_head *dsaddrs) | |
398 | { | |
399 | struct nfs4_pnfs_ds *ds; | |
400 | ||
401 | list_for_each_entry(ds, &nfs4_data_server_cache, ds_node) | |
402 | if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs)) | |
403 | return ds; | |
404 | return NULL; | |
405 | } | |
406 | ||
407 | static void destroy_ds(struct nfs4_pnfs_ds *ds) | |
408 | { | |
409 | struct nfs4_pnfs_ds_addr *da; | |
410 | ||
411 | dprintk("--> %s\n", __func__); | |
412 | ifdebug(FACILITY) | |
413 | print_ds(ds); | |
414 | ||
415 | nfs_put_client(ds->ds_clp); | |
416 | ||
417 | while (!list_empty(&ds->ds_addrs)) { | |
418 | da = list_first_entry(&ds->ds_addrs, | |
419 | struct nfs4_pnfs_ds_addr, | |
420 | da_node); | |
421 | list_del_init(&da->da_node); | |
422 | kfree(da->da_remotestr); | |
423 | kfree(da); | |
424 | } | |
425 | ||
426 | kfree(ds->ds_remotestr); | |
427 | kfree(ds); | |
428 | } | |
429 | ||
430 | void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds) | |
431 | { | |
432 | if (atomic_dec_and_lock(&ds->ds_count, | |
433 | &nfs4_ds_cache_lock)) { | |
434 | list_del_init(&ds->ds_node); | |
435 | spin_unlock(&nfs4_ds_cache_lock); | |
436 | destroy_ds(ds); | |
437 | } | |
438 | } | |
439 | EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put); | |
440 | ||
441 | /* | |
442 | * Create a string with a human readable address and port to avoid | |
443 | * complicated setup around many dprinks. | |
444 | */ | |
445 | static char * | |
446 | nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags) | |
447 | { | |
448 | struct nfs4_pnfs_ds_addr *da; | |
449 | char *remotestr; | |
450 | size_t len; | |
451 | char *p; | |
452 | ||
453 | len = 3; /* '{', '}' and eol */ | |
454 | list_for_each_entry(da, dsaddrs, da_node) { | |
455 | len += strlen(da->da_remotestr) + 1; /* string plus comma */ | |
456 | } | |
457 | ||
458 | remotestr = kzalloc(len, gfp_flags); | |
459 | if (!remotestr) | |
460 | return NULL; | |
461 | ||
462 | p = remotestr; | |
463 | *(p++) = '{'; | |
464 | len--; | |
465 | list_for_each_entry(da, dsaddrs, da_node) { | |
466 | size_t ll = strlen(da->da_remotestr); | |
467 | ||
468 | if (ll > len) | |
469 | goto out_err; | |
470 | ||
471 | memcpy(p, da->da_remotestr, ll); | |
472 | p += ll; | |
473 | len -= ll; | |
474 | ||
475 | if (len < 1) | |
476 | goto out_err; | |
477 | (*p++) = ','; | |
478 | len--; | |
479 | } | |
480 | if (len < 2) | |
481 | goto out_err; | |
482 | *(p++) = '}'; | |
483 | *p = '\0'; | |
484 | return remotestr; | |
485 | out_err: | |
486 | kfree(remotestr); | |
487 | return NULL; | |
488 | } | |
489 | ||
490 | /* | |
491 | * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if | |
492 | * uncached and return cached struct nfs4_pnfs_ds. | |
493 | */ | |
494 | struct nfs4_pnfs_ds * | |
495 | nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags) | |
496 | { | |
497 | struct nfs4_pnfs_ds *tmp_ds, *ds = NULL; | |
498 | char *remotestr; | |
499 | ||
500 | if (list_empty(dsaddrs)) { | |
501 | dprintk("%s: no addresses defined\n", __func__); | |
502 | goto out; | |
503 | } | |
504 | ||
505 | ds = kzalloc(sizeof(*ds), gfp_flags); | |
506 | if (!ds) | |
507 | goto out; | |
508 | ||
509 | /* this is only used for debugging, so it's ok if its NULL */ | |
510 | remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags); | |
511 | ||
512 | spin_lock(&nfs4_ds_cache_lock); | |
513 | tmp_ds = _data_server_lookup_locked(dsaddrs); | |
514 | if (tmp_ds == NULL) { | |
515 | INIT_LIST_HEAD(&ds->ds_addrs); | |
516 | list_splice_init(dsaddrs, &ds->ds_addrs); | |
517 | ds->ds_remotestr = remotestr; | |
518 | atomic_set(&ds->ds_count, 1); | |
519 | INIT_LIST_HEAD(&ds->ds_node); | |
520 | ds->ds_clp = NULL; | |
521 | list_add(&ds->ds_node, &nfs4_data_server_cache); | |
522 | dprintk("%s add new data server %s\n", __func__, | |
523 | ds->ds_remotestr); | |
524 | } else { | |
525 | kfree(remotestr); | |
526 | kfree(ds); | |
527 | atomic_inc(&tmp_ds->ds_count); | |
528 | dprintk("%s data server %s found, inc'ed ds_count to %d\n", | |
529 | __func__, tmp_ds->ds_remotestr, | |
530 | atomic_read(&tmp_ds->ds_count)); | |
531 | ds = tmp_ds; | |
532 | } | |
533 | spin_unlock(&nfs4_ds_cache_lock); | |
534 | out: | |
535 | return ds; | |
536 | } | |
537 | EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add); | |
6b7f3cf9 | 538 | |
7405f9e1 PT |
539 | static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds) |
540 | { | |
541 | might_sleep(); | |
542 | wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING, | |
543 | TASK_KILLABLE); | |
544 | } | |
545 | ||
546 | static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds) | |
547 | { | |
548 | smp_mb__before_atomic(); | |
549 | clear_bit(NFS4DS_CONNECTING, &ds->ds_state); | |
550 | smp_mb__after_atomic(); | |
551 | wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING); | |
552 | } | |
553 | ||
5f01d953 PT |
554 | static struct nfs_client *(*get_v3_ds_connect)( |
555 | struct nfs_client *mds_clp, | |
556 | const struct sockaddr *ds_addr, | |
557 | int ds_addrlen, | |
558 | int ds_proto, | |
559 | unsigned int ds_timeo, | |
560 | unsigned int ds_retrans, | |
561 | rpc_authflavor_t au_flavor); | |
562 | ||
563 | static bool load_v3_ds_connect(void) | |
564 | { | |
565 | if (!get_v3_ds_connect) { | |
566 | get_v3_ds_connect = symbol_request(nfs3_set_ds_client); | |
567 | WARN_ON_ONCE(!get_v3_ds_connect); | |
568 | } | |
569 | ||
570 | return(get_v3_ds_connect != NULL); | |
571 | } | |
572 | ||
573 | void __exit nfs4_pnfs_v3_ds_connect_unload(void) | |
574 | { | |
575 | if (get_v3_ds_connect) { | |
576 | symbol_put(nfs3_set_ds_client); | |
577 | get_v3_ds_connect = NULL; | |
578 | } | |
579 | } | |
580 | EXPORT_SYMBOL_GPL(nfs4_pnfs_v3_ds_connect_unload); | |
581 | ||
582 | static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv, | |
583 | struct nfs4_pnfs_ds *ds, | |
584 | unsigned int timeo, | |
585 | unsigned int retrans, | |
586 | rpc_authflavor_t au_flavor) | |
587 | { | |
588 | struct nfs_client *clp = ERR_PTR(-EIO); | |
589 | struct nfs4_pnfs_ds_addr *da; | |
590 | int status = 0; | |
591 | ||
592 | dprintk("--> %s DS %s au_flavor %d\n", __func__, | |
593 | ds->ds_remotestr, au_flavor); | |
594 | ||
595 | if (!load_v3_ds_connect()) | |
596 | goto out; | |
597 | ||
598 | list_for_each_entry(da, &ds->ds_addrs, da_node) { | |
599 | dprintk("%s: DS %s: trying address %s\n", | |
600 | __func__, ds->ds_remotestr, da->da_remotestr); | |
601 | ||
602 | clp = get_v3_ds_connect(mds_srv->nfs_client, | |
603 | (struct sockaddr *)&da->da_addr, | |
604 | da->da_addrlen, IPPROTO_TCP, | |
605 | timeo, retrans, au_flavor); | |
606 | if (!IS_ERR(clp)) | |
607 | break; | |
608 | } | |
609 | ||
610 | if (IS_ERR(clp)) { | |
611 | status = PTR_ERR(clp); | |
612 | goto out; | |
613 | } | |
614 | ||
615 | smp_wmb(); | |
616 | ds->ds_clp = clp; | |
617 | dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); | |
618 | out: | |
619 | return status; | |
620 | } | |
621 | ||
622 | static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv, | |
7405f9e1 PT |
623 | struct nfs4_pnfs_ds *ds, |
624 | unsigned int timeo, | |
064172f3 | 625 | unsigned int retrans, |
30626f9c | 626 | u32 minor_version, |
064172f3 | 627 | rpc_authflavor_t au_flavor) |
7405f9e1 PT |
628 | { |
629 | struct nfs_client *clp = ERR_PTR(-EIO); | |
630 | struct nfs4_pnfs_ds_addr *da; | |
631 | int status = 0; | |
632 | ||
633 | dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr, | |
5f01d953 | 634 | au_flavor); |
7405f9e1 PT |
635 | |
636 | list_for_each_entry(da, &ds->ds_addrs, da_node) { | |
637 | dprintk("%s: DS %s: trying address %s\n", | |
638 | __func__, ds->ds_remotestr, da->da_remotestr); | |
639 | ||
640 | clp = nfs4_set_ds_client(mds_srv->nfs_client, | |
641 | (struct sockaddr *)&da->da_addr, | |
642 | da->da_addrlen, IPPROTO_TCP, | |
30626f9c PT |
643 | timeo, retrans, minor_version, |
644 | au_flavor); | |
7405f9e1 PT |
645 | if (!IS_ERR(clp)) |
646 | break; | |
647 | } | |
648 | ||
649 | if (IS_ERR(clp)) { | |
650 | status = PTR_ERR(clp); | |
651 | goto out; | |
652 | } | |
653 | ||
654 | status = nfs4_init_ds_session(clp, mds_srv->nfs_client->cl_lease_time); | |
655 | if (status) | |
656 | goto out_put; | |
657 | ||
658 | smp_wmb(); | |
659 | ds->ds_clp = clp; | |
660 | dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr); | |
661 | out: | |
662 | return status; | |
663 | out_put: | |
664 | nfs_put_client(clp); | |
665 | goto out; | |
666 | } | |
667 | ||
668 | /* | |
669 | * Create an rpc connection to the nfs4_pnfs_ds data server. | |
670 | * Currently only supports IPv4 and IPv6 addresses. | |
671 | * If connection fails, make devid unavailable. | |
672 | */ | |
673 | void nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds, | |
674 | struct nfs4_deviceid_node *devid, unsigned int timeo, | |
30626f9c PT |
675 | unsigned int retrans, u32 version, |
676 | u32 minor_version, rpc_authflavor_t au_flavor) | |
7405f9e1 PT |
677 | { |
678 | if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) { | |
679 | int err = 0; | |
680 | ||
5f01d953 PT |
681 | if (version == 3) { |
682 | err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo, | |
683 | retrans, au_flavor); | |
684 | } else if (version == 4) { | |
685 | err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo, | |
686 | retrans, minor_version, | |
687 | au_flavor); | |
688 | } else { | |
689 | dprintk("%s: unsupported DS version %d\n", __func__, | |
690 | version); | |
691 | err = -EPROTONOSUPPORT; | |
692 | } | |
693 | ||
7405f9e1 PT |
694 | if (err) |
695 | nfs4_mark_deviceid_unavailable(devid); | |
696 | nfs4_clear_ds_conn_bit(ds); | |
697 | } else { | |
698 | nfs4_wait_ds_connect(ds); | |
699 | } | |
700 | } | |
701 | EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect); | |
702 | ||
6b7f3cf9 PT |
703 | /* |
704 | * Currently only supports ipv4, ipv6 and one multi-path address. | |
705 | */ | |
706 | struct nfs4_pnfs_ds_addr * | |
707 | nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags) | |
708 | { | |
709 | struct nfs4_pnfs_ds_addr *da = NULL; | |
710 | char *buf, *portstr; | |
711 | __be16 port; | |
712 | int nlen, rlen; | |
713 | int tmp[2]; | |
714 | __be32 *p; | |
715 | char *netid, *match_netid; | |
716 | size_t len, match_netid_len; | |
717 | char *startsep = ""; | |
718 | char *endsep = ""; | |
719 | ||
720 | ||
721 | /* r_netid */ | |
722 | p = xdr_inline_decode(xdr, 4); | |
723 | if (unlikely(!p)) | |
724 | goto out_err; | |
725 | nlen = be32_to_cpup(p++); | |
726 | ||
727 | p = xdr_inline_decode(xdr, nlen); | |
728 | if (unlikely(!p)) | |
729 | goto out_err; | |
730 | ||
731 | netid = kmalloc(nlen+1, gfp_flags); | |
732 | if (unlikely(!netid)) | |
733 | goto out_err; | |
734 | ||
735 | netid[nlen] = '\0'; | |
736 | memcpy(netid, p, nlen); | |
737 | ||
738 | /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */ | |
739 | p = xdr_inline_decode(xdr, 4); | |
740 | if (unlikely(!p)) | |
741 | goto out_free_netid; | |
742 | rlen = be32_to_cpup(p); | |
743 | ||
744 | p = xdr_inline_decode(xdr, rlen); | |
745 | if (unlikely(!p)) | |
746 | goto out_free_netid; | |
747 | ||
748 | /* port is ".ABC.DEF", 8 chars max */ | |
749 | if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) { | |
750 | dprintk("%s: Invalid address, length %d\n", __func__, | |
751 | rlen); | |
752 | goto out_free_netid; | |
753 | } | |
754 | buf = kmalloc(rlen + 1, gfp_flags); | |
755 | if (!buf) { | |
756 | dprintk("%s: Not enough memory\n", __func__); | |
757 | goto out_free_netid; | |
758 | } | |
759 | buf[rlen] = '\0'; | |
760 | memcpy(buf, p, rlen); | |
761 | ||
762 | /* replace port '.' with '-' */ | |
763 | portstr = strrchr(buf, '.'); | |
764 | if (!portstr) { | |
765 | dprintk("%s: Failed finding expected dot in port\n", | |
766 | __func__); | |
767 | goto out_free_buf; | |
768 | } | |
769 | *portstr = '-'; | |
770 | ||
771 | /* find '.' between address and port */ | |
772 | portstr = strrchr(buf, '.'); | |
773 | if (!portstr) { | |
774 | dprintk("%s: Failed finding expected dot between address and " | |
775 | "port\n", __func__); | |
776 | goto out_free_buf; | |
777 | } | |
778 | *portstr = '\0'; | |
779 | ||
780 | da = kzalloc(sizeof(*da), gfp_flags); | |
781 | if (unlikely(!da)) | |
782 | goto out_free_buf; | |
783 | ||
784 | INIT_LIST_HEAD(&da->da_node); | |
785 | ||
786 | if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr, | |
787 | sizeof(da->da_addr))) { | |
788 | dprintk("%s: error parsing address %s\n", __func__, buf); | |
789 | goto out_free_da; | |
790 | } | |
791 | ||
792 | portstr++; | |
793 | sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]); | |
794 | port = htons((tmp[0] << 8) | (tmp[1])); | |
795 | ||
796 | switch (da->da_addr.ss_family) { | |
797 | case AF_INET: | |
798 | ((struct sockaddr_in *)&da->da_addr)->sin_port = port; | |
799 | da->da_addrlen = sizeof(struct sockaddr_in); | |
800 | match_netid = "tcp"; | |
801 | match_netid_len = 3; | |
802 | break; | |
803 | ||
804 | case AF_INET6: | |
805 | ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port; | |
806 | da->da_addrlen = sizeof(struct sockaddr_in6); | |
807 | match_netid = "tcp6"; | |
808 | match_netid_len = 4; | |
809 | startsep = "["; | |
810 | endsep = "]"; | |
811 | break; | |
812 | ||
813 | default: | |
814 | dprintk("%s: unsupported address family: %u\n", | |
815 | __func__, da->da_addr.ss_family); | |
816 | goto out_free_da; | |
817 | } | |
818 | ||
819 | if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) { | |
820 | dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n", | |
821 | __func__, netid, match_netid); | |
822 | goto out_free_da; | |
823 | } | |
824 | ||
825 | /* save human readable address */ | |
826 | len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7; | |
827 | da->da_remotestr = kzalloc(len, gfp_flags); | |
828 | ||
829 | /* NULL is ok, only used for dprintk */ | |
830 | if (da->da_remotestr) | |
831 | snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep, | |
832 | buf, endsep, ntohs(port)); | |
833 | ||
834 | dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr); | |
835 | kfree(buf); | |
836 | kfree(netid); | |
837 | return da; | |
838 | ||
839 | out_free_da: | |
840 | kfree(da); | |
841 | out_free_buf: | |
842 | dprintk("%s: Error parsing DS addr: %s\n", __func__, buf); | |
843 | kfree(buf); | |
844 | out_free_netid: | |
845 | kfree(netid); | |
846 | out_err: | |
847 | return NULL; | |
848 | } | |
849 | EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr); |