Merge tag 'fixes_for_v5.13-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / fs / nfs / pnfs_nfs.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
f54bcf2e
TH
2/*
3 * Common NFS I/O operations for the pnfs file based
4 * layout drivers.
5 *
6 * Copyright (c) 2014, Primary Data, Inc. All rights reserved.
7 *
8 * Tom Haynes <loghyr@primarydata.com>
9 */
10
11#include <linux/nfs_fs.h>
12#include <linux/nfs_page.h>
6b7f3cf9 13#include <linux/sunrpc/addr.h>
5f01d953 14#include <linux/module.h>
f54bcf2e 15
7405f9e1 16#include "nfs4session.h"
f54bcf2e
TH
17#include "internal.h"
18#include "pnfs.h"
19
875ae069
PT
20#define NFSDBG_FACILITY NFSDBG_PNFS
21
f54bcf2e
TH
22void pnfs_generic_rw_release(void *data)
23{
24 struct nfs_pgio_header *hdr = data;
f54bcf2e 25
f54bcf2e
TH
26 nfs_put_client(hdr->ds_clp);
27 hdr->mds_ops->rpc_release(data);
28}
29EXPORT_SYMBOL_GPL(pnfs_generic_rw_release);
30
31/* Fake up some data that will cause nfs_commit_release to retry the writes. */
32void pnfs_generic_prepare_to_resend_writes(struct nfs_commit_data *data)
33{
221203ce 34 struct nfs_writeverf *verf = data->res.verf;
f54bcf2e
TH
35
36 data->task.tk_status = 0;
221203ce
TM
37 memset(&verf->verifier, 0, sizeof(verf->verifier));
38 verf->committed = NFS_UNSTABLE;
f54bcf2e
TH
39}
40EXPORT_SYMBOL_GPL(pnfs_generic_prepare_to_resend_writes);
41
42void pnfs_generic_write_commit_done(struct rpc_task *task, void *data)
43{
44 struct nfs_commit_data *wdata = data;
45
46 /* Note this may cause RPC to be resent */
47 wdata->mds_ops->rpc_call_done(task, data);
48}
49EXPORT_SYMBOL_GPL(pnfs_generic_write_commit_done);
50
51void pnfs_generic_commit_release(void *calldata)
52{
53 struct nfs_commit_data *data = calldata;
54
55 data->completion_ops->completion(data);
56 pnfs_put_lseg(data->lseg);
57 nfs_put_client(data->ds_clp);
58 nfs_commitdata_release(data);
59}
60EXPORT_SYMBOL_GPL(pnfs_generic_commit_release);
61
c84bea59
TM
62static struct pnfs_layout_segment *
63pnfs_free_bucket_lseg(struct pnfs_commit_bucket *bucket)
64{
65 if (list_empty(&bucket->committing) && list_empty(&bucket->written)) {
66 struct pnfs_layout_segment *freeme = bucket->lseg;
67 bucket->lseg = NULL;
68 return freeme;
69 }
70 return NULL;
71}
72
f54bcf2e
TH
73/* The generic layer is about to remove the req from the commit list.
74 * If this will make the bucket empty, it will need to put the lseg reference.
d0fbb1d8 75 * Note this must be called holding nfsi->commit_mutex
f54bcf2e
TH
76 */
77void
78pnfs_generic_clear_request_commit(struct nfs_page *req,
79 struct nfs_commit_info *cinfo)
80{
1757655d 81 struct pnfs_commit_bucket *bucket = NULL;
f54bcf2e
TH
82
83 if (!test_and_clear_bit(PG_COMMIT_TO_DS, &req->wb_flags))
84 goto out;
85 cinfo->ds->nwritten--;
1757655d 86 if (list_is_singular(&req->wb_list))
f54bcf2e 87 bucket = list_first_entry(&req->wb_list,
1757655d 88 struct pnfs_commit_bucket, written);
f54bcf2e
TH
89out:
90 nfs_request_remove_commit_list(req, cinfo);
1757655d
TM
91 if (bucket)
92 pnfs_put_lseg(pnfs_free_bucket_lseg(bucket));
f54bcf2e
TH
93}
94EXPORT_SYMBOL_GPL(pnfs_generic_clear_request_commit);
95
d7242c46
TM
96struct pnfs_commit_array *
97pnfs_alloc_commit_array(size_t n, gfp_t gfp_flags)
98{
99 struct pnfs_commit_array *p;
100 struct pnfs_commit_bucket *b;
101
102 p = kmalloc(struct_size(p, buckets, n), gfp_flags);
103 if (!p)
104 return NULL;
105 p->nbuckets = n;
106 INIT_LIST_HEAD(&p->cinfo_list);
107 INIT_LIST_HEAD(&p->lseg_list);
108 p->lseg = NULL;
109 for (b = &p->buckets[0]; n != 0; b++, n--) {
110 INIT_LIST_HEAD(&b->written);
111 INIT_LIST_HEAD(&b->committing);
c84bea59 112 b->lseg = NULL;
d7242c46
TM
113 b->direct_verf.committed = NFS_INVALID_STABLE_HOW;
114 }
115 return p;
116}
117EXPORT_SYMBOL_GPL(pnfs_alloc_commit_array);
118
119void
120pnfs_free_commit_array(struct pnfs_commit_array *p)
121{
122 kfree_rcu(p, rcu);
123}
124EXPORT_SYMBOL_GPL(pnfs_free_commit_array);
125
ba827c9a
TM
126static struct pnfs_commit_array *
127pnfs_find_commit_array_by_lseg(struct pnfs_ds_commit_info *fl_cinfo,
128 struct pnfs_layout_segment *lseg)
129{
130 struct pnfs_commit_array *array;
131
132 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
133 if (array->lseg == lseg)
134 return array;
135 }
136 return NULL;
137}
138
139struct pnfs_commit_array *
140pnfs_add_commit_array(struct pnfs_ds_commit_info *fl_cinfo,
141 struct pnfs_commit_array *new,
142 struct pnfs_layout_segment *lseg)
143{
144 struct pnfs_commit_array *array;
145
146 array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
147 if (array)
148 return array;
149 new->lseg = lseg;
150 refcount_set(&new->refcount, 1);
151 list_add_rcu(&new->cinfo_list, &fl_cinfo->commits);
152 list_add(&new->lseg_list, &lseg->pls_commits);
153 return new;
154}
155EXPORT_SYMBOL_GPL(pnfs_add_commit_array);
156
ba827c9a
TM
157static struct pnfs_commit_array *
158pnfs_lookup_commit_array(struct pnfs_ds_commit_info *fl_cinfo,
159 struct pnfs_layout_segment *lseg)
160{
161 struct pnfs_commit_array *array;
162
163 rcu_read_lock();
164 array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
165 if (!array) {
166 rcu_read_unlock();
9c455a8c 167 fl_cinfo->ops->setup_ds_info(fl_cinfo, lseg);
ba827c9a
TM
168 rcu_read_lock();
169 array = pnfs_find_commit_array_by_lseg(fl_cinfo, lseg);
170 }
171 rcu_read_unlock();
172 return array;
173}
174
a9901899
TM
175static void
176pnfs_release_commit_array_locked(struct pnfs_commit_array *array)
177{
178 list_del_rcu(&array->cinfo_list);
179 list_del(&array->lseg_list);
180 pnfs_free_commit_array(array);
181}
182
183static void
184pnfs_put_commit_array_locked(struct pnfs_commit_array *array)
185{
186 if (refcount_dec_and_test(&array->refcount))
187 pnfs_release_commit_array_locked(array);
188}
189
190static void
191pnfs_put_commit_array(struct pnfs_commit_array *array, struct inode *inode)
192{
193 if (refcount_dec_and_lock(&array->refcount, &inode->i_lock)) {
194 pnfs_release_commit_array_locked(array);
195 spin_unlock(&inode->i_lock);
196 }
197}
198
199static struct pnfs_commit_array *
200pnfs_get_commit_array(struct pnfs_commit_array *array)
201{
202 if (refcount_inc_not_zero(&array->refcount))
203 return array;
204 return NULL;
205}
206
207static void
208pnfs_remove_and_free_commit_array(struct pnfs_commit_array *array)
209{
210 array->lseg = NULL;
211 list_del_init(&array->lseg_list);
212 pnfs_put_commit_array_locked(array);
213}
214
215void
216pnfs_generic_ds_cinfo_release_lseg(struct pnfs_ds_commit_info *fl_cinfo,
217 struct pnfs_layout_segment *lseg)
218{
219 struct pnfs_commit_array *array, *tmp;
220
221 list_for_each_entry_safe(array, tmp, &lseg->pls_commits, lseg_list)
222 pnfs_remove_and_free_commit_array(array);
223}
224EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_release_lseg);
225
226void
227pnfs_generic_ds_cinfo_destroy(struct pnfs_ds_commit_info *fl_cinfo)
228{
229 struct pnfs_commit_array *array, *tmp;
230
231 list_for_each_entry_safe(array, tmp, &fl_cinfo->commits, cinfo_list)
232 pnfs_remove_and_free_commit_array(array);
233}
234EXPORT_SYMBOL_GPL(pnfs_generic_ds_cinfo_destroy);
235
a8e3765e
TM
236/*
237 * Locks the nfs_page requests for commit and moves them to
238 * @bucket->committing.
239 */
f54bcf2e 240static int
a8e3765e
TM
241pnfs_bucket_scan_ds_commit_list(struct pnfs_commit_bucket *bucket,
242 struct nfs_commit_info *cinfo,
243 int max)
f54bcf2e
TH
244{
245 struct list_head *src = &bucket->written;
246 struct list_head *dst = &bucket->committing;
247 int ret;
248
e824f99a 249 lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
5d2a9d9d 250 ret = nfs_scan_commit_list(src, dst, cinfo, max);
f54bcf2e
TH
251 if (ret) {
252 cinfo->ds->nwritten -= ret;
253 cinfo->ds->ncommitting += ret;
f54bcf2e
TH
254 }
255 return ret;
256}
257
a8e3765e
TM
258static int pnfs_bucket_scan_array(struct nfs_commit_info *cinfo,
259 struct pnfs_commit_bucket *buckets,
260 unsigned int nbuckets,
261 int max)
262{
263 unsigned int i;
264 int rv = 0, cnt;
265
266 for (i = 0; i < nbuckets && max != 0; i++) {
267 cnt = pnfs_bucket_scan_ds_commit_list(&buckets[i], cinfo, max);
268 rv += cnt;
269 max -= cnt;
270 }
271 return rv;
272}
273
085d1e33
TH
274/* Move reqs from written to committing lists, returning count
275 * of number moved.
f54bcf2e 276 */
a8e3765e 277int pnfs_generic_scan_commit_lists(struct nfs_commit_info *cinfo, int max)
f54bcf2e 278{
a8e3765e
TM
279 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
280 struct pnfs_commit_array *array;
281 int rv = 0, cnt;
282
a9901899
TM
283 rcu_read_lock();
284 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
285 if (!array->lseg || !pnfs_get_commit_array(array))
286 continue;
287 rcu_read_unlock();
a8e3765e
TM
288 cnt = pnfs_bucket_scan_array(cinfo, array->buckets,
289 array->nbuckets, max);
a9901899
TM
290 rcu_read_lock();
291 pnfs_put_commit_array(array, cinfo->inode);
f54bcf2e 292 rv += cnt;
a8e3765e
TM
293 max -= cnt;
294 if (!max)
295 break;
f54bcf2e 296 }
a9901899 297 rcu_read_unlock();
f54bcf2e
TH
298 return rv;
299}
300EXPORT_SYMBOL_GPL(pnfs_generic_scan_commit_lists);
301
fce9ed03
TM
302static unsigned int
303pnfs_bucket_recover_commit_reqs(struct list_head *dst,
304 struct pnfs_commit_bucket *buckets,
305 unsigned int nbuckets,
306 struct nfs_commit_info *cinfo)
f54bcf2e
TH
307{
308 struct pnfs_commit_bucket *b;
309 struct pnfs_layout_segment *freeme;
fce9ed03
TM
310 unsigned int nwritten, ret = 0;
311 unsigned int i;
f54bcf2e
TH
312
313restart:
fce9ed03 314 for (i = 0, b = buckets; i < nbuckets; i++, b++) {
5d2a9d9d 315 nwritten = nfs_scan_commit_list(&b->written, dst, cinfo, 0);
e39928f9
TM
316 if (!nwritten)
317 continue;
fce9ed03 318 ret += nwritten;
c84bea59
TM
319 freeme = pnfs_free_bucket_lseg(b);
320 if (freeme) {
f54bcf2e
TH
321 pnfs_put_lseg(freeme);
322 goto restart;
323 }
324 }
fce9ed03
TM
325 return ret;
326}
327
328/* Pull everything off the committing lists and dump into @dst. */
329void pnfs_generic_recover_commit_reqs(struct list_head *dst,
330 struct nfs_commit_info *cinfo)
331{
332 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
333 struct pnfs_commit_array *array;
334 unsigned int nwritten;
335
336 lockdep_assert_held(&NFS_I(cinfo->inode)->commit_mutex);
a9901899
TM
337 rcu_read_lock();
338 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
339 if (!array->lseg || !pnfs_get_commit_array(array))
340 continue;
341 rcu_read_unlock();
fce9ed03
TM
342 nwritten = pnfs_bucket_recover_commit_reqs(dst,
343 array->buckets,
344 array->nbuckets,
345 cinfo);
a9901899
TM
346 rcu_read_lock();
347 pnfs_put_commit_array(array, cinfo->inode);
fce9ed03
TM
348 fl_cinfo->nwritten -= nwritten;
349 }
a9901899 350 rcu_read_unlock();
f54bcf2e
TH
351}
352EXPORT_SYMBOL_GPL(pnfs_generic_recover_commit_reqs);
353
fb6b53ba
TM
354static struct nfs_page *
355pnfs_bucket_search_commit_reqs(struct pnfs_commit_bucket *buckets,
356 unsigned int nbuckets, struct page *page)
357{
358 struct nfs_page *req;
359 struct pnfs_commit_bucket *b;
360 unsigned int i;
361
362 /* Linearly search the commit lists for each bucket until a matching
363 * request is found */
364 for (i = 0, b = buckets; i < nbuckets; i++, b++) {
365 list_for_each_entry(req, &b->written, wb_list) {
366 if (req->wb_page == page)
367 return req->wb_head;
368 }
369 list_for_each_entry(req, &b->committing, wb_list) {
370 if (req->wb_page == page)
371 return req->wb_head;
372 }
373 }
374 return NULL;
375}
376
377/* pnfs_generic_search_commit_reqs - Search lists in @cinfo for the head reqest
378 * for @page
379 * @cinfo - commit info for current inode
380 * @page - page to search for matching head request
381 *
382 * Returns a the head request if one is found, otherwise returns NULL.
383 */
384struct nfs_page *
385pnfs_generic_search_commit_reqs(struct nfs_commit_info *cinfo, struct page *page)
386{
387 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
388 struct pnfs_commit_array *array;
389 struct nfs_page *req;
390
fb6b53ba
TM
391 list_for_each_entry(array, &fl_cinfo->commits, cinfo_list) {
392 req = pnfs_bucket_search_commit_reqs(array->buckets,
393 array->nbuckets, page);
394 if (req)
395 return req;
396 }
397 return NULL;
398}
399EXPORT_SYMBOL_GPL(pnfs_generic_search_commit_reqs);
400
19573c93
TM
401static struct pnfs_layout_segment *
402pnfs_bucket_get_committing(struct list_head *head,
403 struct pnfs_commit_bucket *bucket,
404 struct nfs_commit_info *cinfo)
f54bcf2e 405{
46c9ea1d 406 struct pnfs_layout_segment *lseg;
41181886 407 struct list_head *pos;
19573c93
TM
408
409 list_for_each(pos, &bucket->committing)
410 cinfo->ds->ncommitting--;
411 list_splice_init(&bucket->committing, head);
46c9ea1d
TM
412 lseg = pnfs_free_bucket_lseg(bucket);
413 if (!lseg)
414 lseg = pnfs_get_lseg(bucket->lseg);
415 return lseg;
19573c93
TM
416}
417
418static struct nfs_commit_data *
419pnfs_bucket_fetch_commitdata(struct pnfs_commit_bucket *bucket,
420 struct nfs_commit_info *cinfo)
421{
422 struct nfs_commit_data *data = nfs_commitdata_alloc(false);
423
424 if (!data)
425 return NULL;
426 data->lseg = pnfs_bucket_get_committing(&data->pages, bucket, cinfo);
427 return data;
428}
429
430static void pnfs_generic_retry_commit(struct pnfs_commit_bucket *buckets,
431 unsigned int nbuckets,
432 struct nfs_commit_info *cinfo,
433 unsigned int idx)
434{
435 struct pnfs_commit_bucket *bucket;
436 struct pnfs_layout_segment *freeme;
27571297 437 LIST_HEAD(pages);
f54bcf2e 438
19573c93 439 for (bucket = buckets; idx < nbuckets; bucket++, idx++) {
f54bcf2e
TH
440 if (list_empty(&bucket->committing))
441 continue;
19573c93
TM
442 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
443 freeme = pnfs_bucket_get_committing(&pages, bucket, cinfo);
d0fbb1d8 444 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
19573c93 445 nfs_retry_commit(&pages, freeme, cinfo, idx);
f54bcf2e
TH
446 pnfs_put_lseg(freeme);
447 }
448}
449
450static unsigned int
19573c93
TM
451pnfs_bucket_alloc_ds_commits(struct list_head *list,
452 struct pnfs_commit_bucket *buckets,
453 unsigned int nbuckets,
454 struct nfs_commit_info *cinfo)
f54bcf2e 455{
f54bcf2e
TH
456 struct pnfs_commit_bucket *bucket;
457 struct nfs_commit_data *data;
19573c93 458 unsigned int i;
f54bcf2e
TH
459 unsigned int nreq = 0;
460
19573c93 461 for (i = 0, bucket = buckets; i < nbuckets; i++, bucket++) {
f54bcf2e
TH
462 if (list_empty(&bucket->committing))
463 continue;
19573c93
TM
464 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
465 if (!list_empty(&bucket->committing)) {
466 data = pnfs_bucket_fetch_commitdata(bucket, cinfo);
467 if (!data)
468 goto out_error;
469 data->ds_commit_index = i;
470 list_add_tail(&data->list, list);
471 atomic_inc(&cinfo->mds->rpcs_out);
472 nreq++;
473 }
474 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
f54bcf2e 475 }
f54bcf2e 476 return nreq;
19573c93 477out_error:
d0fbb1d8 478 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
19573c93
TM
479 /* Clean up on error */
480 pnfs_generic_retry_commit(buckets, nbuckets, cinfo, i);
481 return nreq;
ade8febd
WAA
482}
483
0cb1f6df
TM
484static unsigned int
485pnfs_alloc_ds_commits_list(struct list_head *list,
486 struct pnfs_ds_commit_info *fl_cinfo,
487 struct nfs_commit_info *cinfo)
488{
489 struct pnfs_commit_array *array;
490 unsigned int ret = 0;
491
a9901899
TM
492 rcu_read_lock();
493 list_for_each_entry_rcu(array, &fl_cinfo->commits, cinfo_list) {
494 if (!array->lseg || !pnfs_get_commit_array(array))
495 continue;
496 rcu_read_unlock();
0cb1f6df
TM
497 ret += pnfs_bucket_alloc_ds_commits(list, array->buckets,
498 array->nbuckets, cinfo);
a9901899
TM
499 rcu_read_lock();
500 pnfs_put_commit_array(array, cinfo->inode);
501 }
27d231c0 502 rcu_read_unlock();
0cb1f6df
TM
503 return ret;
504}
505
f54bcf2e
TH
506/* This follows nfs_commit_list pretty closely */
507int
508pnfs_generic_commit_pagelist(struct inode *inode, struct list_head *mds_pages,
509 int how, struct nfs_commit_info *cinfo,
510 int (*initiate_commit)(struct nfs_commit_data *data,
511 int how))
512{
19573c93 513 struct pnfs_ds_commit_info *fl_cinfo = cinfo->ds;
f54bcf2e
TH
514 struct nfs_commit_data *data, *tmp;
515 LIST_HEAD(list);
516 unsigned int nreq = 0;
517
518 if (!list_empty(mds_pages)) {
518662e0
N
519 data = nfs_commitdata_alloc(true);
520 data->ds_commit_index = -1;
19573c93
TM
521 list_splice_init(mds_pages, &data->pages);
522 list_add_tail(&data->list, &list);
523 atomic_inc(&cinfo->mds->rpcs_out);
518662e0 524 nreq++;
f54bcf2e
TH
525 }
526
0cb1f6df 527 nreq += pnfs_alloc_ds_commits_list(&list, fl_cinfo, cinfo);
af7cf057 528 if (nreq == 0)
f54bcf2e 529 goto out;
f54bcf2e 530
19573c93
TM
531 list_for_each_entry_safe(data, tmp, &list, list) {
532 list_del(&data->list);
27571297 533 if (data->ds_commit_index < 0) {
19573c93 534 nfs_init_commit(data, NULL, NULL, cinfo);
f54bcf2e 535 nfs_initiate_commit(NFS_CLIENT(inode), data,
c36aae9a 536 NFS_PROTO(data->inode),
4fa7ef69
TM
537 data->mds_ops, how,
538 RPC_TASK_CRED_NOREF);
f54bcf2e 539 } else {
19573c93 540 nfs_init_commit(data, NULL, data->lseg, cinfo);
f54bcf2e
TH
541 initiate_commit(data, how);
542 }
543 }
544out:
f54bcf2e
TH
545 return PNFS_ATTEMPTED;
546}
547EXPORT_SYMBOL_GPL(pnfs_generic_commit_pagelist);
875ae069
PT
548
549/*
550 * Data server cache
551 *
552 * Data servers can be mapped to different device ids.
553 * nfs4_pnfs_ds reference counting
554 * - set to 1 on allocation
555 * - incremented when a device id maps a data server already in the cache.
556 * - decremented when deviceid is removed from the cache.
557 */
558static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
559static LIST_HEAD(nfs4_data_server_cache);
560
561/* Debug routines */
562static void
563print_ds(struct nfs4_pnfs_ds *ds)
564{
565 if (ds == NULL) {
566 printk(KERN_WARNING "%s NULL device\n", __func__);
567 return;
568 }
569 printk(KERN_WARNING " ds %s\n"
570 " ref count %d\n"
571 " client %p\n"
572 " cl_exchange_flags %x\n",
573 ds->ds_remotestr,
a2a5dea7 574 refcount_read(&ds->ds_count), ds->ds_clp,
875ae069
PT
575 ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
576}
577
578static bool
579same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
580{
581 struct sockaddr_in *a, *b;
582 struct sockaddr_in6 *a6, *b6;
583
584 if (addr1->sa_family != addr2->sa_family)
585 return false;
586
587 switch (addr1->sa_family) {
588 case AF_INET:
589 a = (struct sockaddr_in *)addr1;
590 b = (struct sockaddr_in *)addr2;
591
592 if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
593 a->sin_port == b->sin_port)
594 return true;
595 break;
596
597 case AF_INET6:
598 a6 = (struct sockaddr_in6 *)addr1;
599 b6 = (struct sockaddr_in6 *)addr2;
600
601 /* LINKLOCAL addresses must have matching scope_id */
602 if (ipv6_addr_src_scope(&a6->sin6_addr) ==
603 IPV6_ADDR_SCOPE_LINKLOCAL &&
604 a6->sin6_scope_id != b6->sin6_scope_id)
605 return false;
606
607 if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
608 a6->sin6_port == b6->sin6_port)
609 return true;
610 break;
611
612 default:
613 dprintk("%s: unhandled address family: %u\n",
614 __func__, addr1->sa_family);
615 return false;
616 }
617
618 return false;
619}
620
6f536936
TM
621/*
622 * Checks if 'dsaddrs1' contains a subset of 'dsaddrs2'. If it does,
623 * declare a match.
624 */
875ae069
PT
625static bool
626_same_data_server_addrs_locked(const struct list_head *dsaddrs1,
627 const struct list_head *dsaddrs2)
628{
629 struct nfs4_pnfs_ds_addr *da1, *da2;
6f536936
TM
630 struct sockaddr *sa1, *sa2;
631 bool match = false;
632
633 list_for_each_entry(da1, dsaddrs1, da_node) {
634 sa1 = (struct sockaddr *)&da1->da_addr;
635 match = false;
636 list_for_each_entry(da2, dsaddrs2, da_node) {
637 sa2 = (struct sockaddr *)&da2->da_addr;
638 match = same_sockaddr(sa1, sa2);
639 if (match)
640 break;
641 }
642 if (!match)
643 break;
875ae069 644 }
6f536936 645 return match;
875ae069
PT
646}
647
648/*
649 * Lookup DS by addresses. nfs4_ds_cache_lock is held
650 */
651static struct nfs4_pnfs_ds *
652_data_server_lookup_locked(const struct list_head *dsaddrs)
653{
654 struct nfs4_pnfs_ds *ds;
655
656 list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
657 if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
658 return ds;
659 return NULL;
660}
661
190c75a3
TM
662static struct nfs4_pnfs_ds_addr *nfs4_pnfs_ds_addr_alloc(gfp_t gfp_flags)
663{
664 struct nfs4_pnfs_ds_addr *da = kzalloc(sizeof(*da), gfp_flags);
665 if (da)
666 INIT_LIST_HEAD(&da->da_node);
667 return da;
668}
669
670static void nfs4_pnfs_ds_addr_free(struct nfs4_pnfs_ds_addr *da)
671{
672 kfree(da->da_remotestr);
4be78d26 673 kfree(da->da_netid);
190c75a3
TM
674 kfree(da);
675}
676
875ae069
PT
677static void destroy_ds(struct nfs4_pnfs_ds *ds)
678{
679 struct nfs4_pnfs_ds_addr *da;
680
681 dprintk("--> %s\n", __func__);
682 ifdebug(FACILITY)
683 print_ds(ds);
684
685 nfs_put_client(ds->ds_clp);
686
687 while (!list_empty(&ds->ds_addrs)) {
688 da = list_first_entry(&ds->ds_addrs,
689 struct nfs4_pnfs_ds_addr,
690 da_node);
691 list_del_init(&da->da_node);
190c75a3 692 nfs4_pnfs_ds_addr_free(da);
875ae069
PT
693 }
694
695 kfree(ds->ds_remotestr);
696 kfree(ds);
697}
698
699void nfs4_pnfs_ds_put(struct nfs4_pnfs_ds *ds)
700{
a2a5dea7 701 if (refcount_dec_and_lock(&ds->ds_count,
875ae069
PT
702 &nfs4_ds_cache_lock)) {
703 list_del_init(&ds->ds_node);
704 spin_unlock(&nfs4_ds_cache_lock);
705 destroy_ds(ds);
706 }
707}
708EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_put);
709
710/*
711 * Create a string with a human readable address and port to avoid
712 * complicated setup around many dprinks.
713 */
714static char *
715nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
716{
717 struct nfs4_pnfs_ds_addr *da;
718 char *remotestr;
719 size_t len;
720 char *p;
721
722 len = 3; /* '{', '}' and eol */
723 list_for_each_entry(da, dsaddrs, da_node) {
724 len += strlen(da->da_remotestr) + 1; /* string plus comma */
725 }
726
727 remotestr = kzalloc(len, gfp_flags);
728 if (!remotestr)
729 return NULL;
730
731 p = remotestr;
732 *(p++) = '{';
733 len--;
734 list_for_each_entry(da, dsaddrs, da_node) {
735 size_t ll = strlen(da->da_remotestr);
736
737 if (ll > len)
738 goto out_err;
739
740 memcpy(p, da->da_remotestr, ll);
741 p += ll;
742 len -= ll;
743
744 if (len < 1)
745 goto out_err;
746 (*p++) = ',';
747 len--;
748 }
749 if (len < 2)
750 goto out_err;
751 *(p++) = '}';
752 *p = '\0';
753 return remotestr;
754out_err:
755 kfree(remotestr);
756 return NULL;
757}
758
759/*
760 * Given a list of multipath struct nfs4_pnfs_ds_addr, add it to ds cache if
761 * uncached and return cached struct nfs4_pnfs_ds.
762 */
763struct nfs4_pnfs_ds *
764nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
765{
766 struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
767 char *remotestr;
768
769 if (list_empty(dsaddrs)) {
770 dprintk("%s: no addresses defined\n", __func__);
771 goto out;
772 }
773
774 ds = kzalloc(sizeof(*ds), gfp_flags);
775 if (!ds)
776 goto out;
777
778 /* this is only used for debugging, so it's ok if its NULL */
779 remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
780
781 spin_lock(&nfs4_ds_cache_lock);
782 tmp_ds = _data_server_lookup_locked(dsaddrs);
783 if (tmp_ds == NULL) {
784 INIT_LIST_HEAD(&ds->ds_addrs);
785 list_splice_init(dsaddrs, &ds->ds_addrs);
786 ds->ds_remotestr = remotestr;
a2a5dea7 787 refcount_set(&ds->ds_count, 1);
875ae069
PT
788 INIT_LIST_HEAD(&ds->ds_node);
789 ds->ds_clp = NULL;
790 list_add(&ds->ds_node, &nfs4_data_server_cache);
791 dprintk("%s add new data server %s\n", __func__,
792 ds->ds_remotestr);
793 } else {
794 kfree(remotestr);
795 kfree(ds);
a2a5dea7 796 refcount_inc(&tmp_ds->ds_count);
875ae069
PT
797 dprintk("%s data server %s found, inc'ed ds_count to %d\n",
798 __func__, tmp_ds->ds_remotestr,
a2a5dea7 799 refcount_read(&tmp_ds->ds_count));
875ae069
PT
800 ds = tmp_ds;
801 }
802 spin_unlock(&nfs4_ds_cache_lock);
803out:
804 return ds;
805}
806EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_add);
6b7f3cf9 807
7405f9e1
PT
808static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
809{
810 might_sleep();
811 wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
812 TASK_KILLABLE);
813}
814
815static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
816{
817 smp_mb__before_atomic();
818 clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
819 smp_mb__after_atomic();
820 wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
821}
822
5f01d953 823static struct nfs_client *(*get_v3_ds_connect)(
b224f7cb 824 struct nfs_server *mds_srv,
5f01d953
PT
825 const struct sockaddr *ds_addr,
826 int ds_addrlen,
827 int ds_proto,
828 unsigned int ds_timeo,
7d38de3f 829 unsigned int ds_retrans);
5f01d953
PT
830
831static bool load_v3_ds_connect(void)
832{
833 if (!get_v3_ds_connect) {
834 get_v3_ds_connect = symbol_request(nfs3_set_ds_client);
835 WARN_ON_ONCE(!get_v3_ds_connect);
836 }
837
838 return(get_v3_ds_connect != NULL);
839}
840
df137bc1 841void nfs4_pnfs_v3_ds_connect_unload(void)
5f01d953
PT
842{
843 if (get_v3_ds_connect) {
844 symbol_put(nfs3_set_ds_client);
845 get_v3_ds_connect = NULL;
846 }
847}
5f01d953
PT
848
849static int _nfs4_pnfs_v3_ds_connect(struct nfs_server *mds_srv,
850 struct nfs4_pnfs_ds *ds,
851 unsigned int timeo,
7d38de3f 852 unsigned int retrans)
5f01d953
PT
853{
854 struct nfs_client *clp = ERR_PTR(-EIO);
855 struct nfs4_pnfs_ds_addr *da;
856 int status = 0;
857
7d38de3f 858 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
5f01d953
PT
859
860 if (!load_v3_ds_connect())
861 goto out;
862
863 list_for_each_entry(da, &ds->ds_addrs, da_node) {
864 dprintk("%s: DS %s: trying address %s\n",
865 __func__, ds->ds_remotestr, da->da_remotestr);
866
fc821d59
TM
867 if (!IS_ERR(clp)) {
868 struct xprt_create xprt_args = {
4be78d26 869 .ident = da->da_transport,
fc821d59
TM
870 .net = clp->cl_net,
871 .dstaddr = (struct sockaddr *)&da->da_addr,
872 .addrlen = da->da_addrlen,
873 .servername = clp->cl_hostname,
874 };
a12f996d 875
4be78d26
TM
876 if (da->da_transport != clp->cl_proto)
877 continue;
a12f996d
TM
878 if (da->da_addr.ss_family != clp->cl_addr.ss_family)
879 continue;
fc821d59
TM
880 /* Add this address as an alias */
881 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
882 rpc_clnt_test_and_add_xprt, NULL);
bf2bf9b8
TM
883 continue;
884 }
885 clp = get_v3_ds_connect(mds_srv,
886 (struct sockaddr *)&da->da_addr,
4be78d26 887 da->da_addrlen, da->da_transport,
bf2bf9b8
TM
888 timeo, retrans);
889 if (IS_ERR(clp))
890 continue;
891 clp->cl_rpcclient->cl_softerr = 0;
892 clp->cl_rpcclient->cl_softrtry = 0;
5f01d953
PT
893 }
894
895 if (IS_ERR(clp)) {
896 status = PTR_ERR(clp);
897 goto out;
898 }
899
900 smp_wmb();
901 ds->ds_clp = clp;
902 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
903out:
904 return status;
905}
906
907static int _nfs4_pnfs_v4_ds_connect(struct nfs_server *mds_srv,
7405f9e1
PT
908 struct nfs4_pnfs_ds *ds,
909 unsigned int timeo,
064172f3 910 unsigned int retrans,
7d38de3f 911 u32 minor_version)
7405f9e1
PT
912{
913 struct nfs_client *clp = ERR_PTR(-EIO);
914 struct nfs4_pnfs_ds_addr *da;
915 int status = 0;
916
7d38de3f 917 dprintk("--> %s DS %s\n", __func__, ds->ds_remotestr);
7405f9e1
PT
918
919 list_for_each_entry(da, &ds->ds_addrs, da_node) {
920 dprintk("%s: DS %s: trying address %s\n",
921 __func__, ds->ds_remotestr, da->da_remotestr);
922
04fa2c6b
AA
923 if (!IS_ERR(clp) && clp->cl_mvops->session_trunk) {
924 struct xprt_create xprt_args = {
4be78d26 925 .ident = da->da_transport,
04fa2c6b
AA
926 .net = clp->cl_net,
927 .dstaddr = (struct sockaddr *)&da->da_addr,
928 .addrlen = da->da_addrlen,
929 .servername = clp->cl_hostname,
930 };
931 struct nfs4_add_xprt_data xprtdata = {
932 .clp = clp,
04fa2c6b
AA
933 };
934 struct rpc_add_xprt_test rpcdata = {
935 .add_xprt_test = clp->cl_mvops->session_trunk,
936 .data = &xprtdata,
937 };
938
4be78d26
TM
939 if (da->da_transport != clp->cl_proto)
940 continue;
a12f996d
TM
941 if (da->da_addr.ss_family != clp->cl_addr.ss_family)
942 continue;
04fa2c6b
AA
943 /**
944 * Test this address for session trunking and
945 * add as an alias
946 */
a12f996d 947 xprtdata.cred = nfs4_get_clid_cred(clp),
04fa2c6b
AA
948 rpc_clnt_add_xprt(clp->cl_rpcclient, &xprt_args,
949 rpc_clnt_setup_test_and_add_xprt,
950 &rpcdata);
951 if (xprtdata.cred)
a52458b4 952 put_cred(xprtdata.cred);
04fa2c6b
AA
953 } else {
954 clp = nfs4_set_ds_client(mds_srv,
955 (struct sockaddr *)&da->da_addr,
4be78d26
TM
956 da->da_addrlen,
957 da->da_transport, timeo,
958 retrans, minor_version);
04fa2c6b
AA
959 if (IS_ERR(clp))
960 continue;
961
962 status = nfs4_init_ds_session(clp,
963 mds_srv->nfs_client->cl_lease_time);
964 if (status) {
965 nfs_put_client(clp);
966 clp = ERR_PTR(-EIO);
967 continue;
968 }
969
970 }
7405f9e1
PT
971 }
972
973 if (IS_ERR(clp)) {
974 status = PTR_ERR(clp);
975 goto out;
976 }
977
7405f9e1
PT
978 smp_wmb();
979 ds->ds_clp = clp;
980 dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
981out:
982 return status;
7405f9e1
PT
983}
984
985/*
986 * Create an rpc connection to the nfs4_pnfs_ds data server.
987 * Currently only supports IPv4 and IPv6 addresses.
a33e4b03 988 * If connection fails, make devid unavailable and return a -errno.
7405f9e1 989 */
a33e4b03 990int nfs4_pnfs_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds,
7405f9e1 991 struct nfs4_deviceid_node *devid, unsigned int timeo,
7d38de3f 992 unsigned int retrans, u32 version, u32 minor_version)
7405f9e1 993{
da066f3f 994 int err;
7405f9e1 995
da066f3f
WAA
996again:
997 err = 0;
998 if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
5f01d953
PT
999 if (version == 3) {
1000 err = _nfs4_pnfs_v3_ds_connect(mds_srv, ds, timeo,
7d38de3f 1001 retrans);
5f01d953
PT
1002 } else if (version == 4) {
1003 err = _nfs4_pnfs_v4_ds_connect(mds_srv, ds, timeo,
7d38de3f 1004 retrans, minor_version);
5f01d953
PT
1005 } else {
1006 dprintk("%s: unsupported DS version %d\n", __func__,
1007 version);
1008 err = -EPROTONOSUPPORT;
1009 }
1010
7405f9e1
PT
1011 nfs4_clear_ds_conn_bit(ds);
1012 } else {
1013 nfs4_wait_ds_connect(ds);
da066f3f
WAA
1014
1015 /* what was waited on didn't connect AND didn't mark unavail */
1016 if (!ds->ds_clp && !nfs4_test_deviceid_unavailable(devid))
1017 goto again;
7405f9e1 1018 }
a33e4b03
WAA
1019
1020 /*
1021 * At this point the ds->ds_clp should be ready, but it might have
1022 * hit an error.
1023 */
da066f3f
WAA
1024 if (!err) {
1025 if (!ds->ds_clp || !nfs_client_init_is_complete(ds->ds_clp)) {
1026 WARN_ON_ONCE(ds->ds_clp ||
1027 !nfs4_test_deviceid_unavailable(devid));
1028 return -EINVAL;
1029 }
1030 err = nfs_client_init_status(ds->ds_clp);
a33e4b03
WAA
1031 }
1032
da066f3f 1033 return err;
7405f9e1
PT
1034}
1035EXPORT_SYMBOL_GPL(nfs4_pnfs_ds_connect);
1036
6b7f3cf9
PT
1037/*
1038 * Currently only supports ipv4, ipv6 and one multi-path address.
1039 */
1040struct nfs4_pnfs_ds_addr *
1041nfs4_decode_mp_ds_addr(struct net *net, struct xdr_stream *xdr, gfp_t gfp_flags)
1042{
1043 struct nfs4_pnfs_ds_addr *da = NULL;
1044 char *buf, *portstr;
1045 __be16 port;
98899813 1046 ssize_t nlen, rlen;
6b7f3cf9 1047 int tmp[2];
4be78d26
TM
1048 char *netid;
1049 size_t len;
6b7f3cf9
PT
1050 char *startsep = "";
1051 char *endsep = "";
1052
1053
1054 /* r_netid */
98899813
TM
1055 nlen = xdr_stream_decode_string_dup(xdr, &netid, XDR_MAX_NETOBJ,
1056 gfp_flags);
1057 if (unlikely(nlen < 0))
6b7f3cf9 1058 goto out_err;
6b7f3cf9
PT
1059
1060 /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
6b7f3cf9 1061 /* port is ".ABC.DEF", 8 chars max */
98899813
TM
1062 rlen = xdr_stream_decode_string_dup(xdr, &buf, INET6_ADDRSTRLEN +
1063 IPV6_SCOPE_ID_LEN + 8, gfp_flags);
1064 if (unlikely(rlen < 0))
6b7f3cf9 1065 goto out_free_netid;
6b7f3cf9
PT
1066
1067 /* replace port '.' with '-' */
1068 portstr = strrchr(buf, '.');
1069 if (!portstr) {
1070 dprintk("%s: Failed finding expected dot in port\n",
1071 __func__);
1072 goto out_free_buf;
1073 }
1074 *portstr = '-';
1075
1076 /* find '.' between address and port */
1077 portstr = strrchr(buf, '.');
1078 if (!portstr) {
1079 dprintk("%s: Failed finding expected dot between address and "
1080 "port\n", __func__);
1081 goto out_free_buf;
1082 }
1083 *portstr = '\0';
1084
190c75a3 1085 da = nfs4_pnfs_ds_addr_alloc(gfp_flags);
6b7f3cf9
PT
1086 if (unlikely(!da))
1087 goto out_free_buf;
1088
6b7f3cf9
PT
1089 if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
1090 sizeof(da->da_addr))) {
1091 dprintk("%s: error parsing address %s\n", __func__, buf);
1092 goto out_free_da;
1093 }
1094
1095 portstr++;
1096 sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
1097 port = htons((tmp[0] << 8) | (tmp[1]));
1098
1099 switch (da->da_addr.ss_family) {
1100 case AF_INET:
1101 ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
1102 da->da_addrlen = sizeof(struct sockaddr_in);
6b7f3cf9
PT
1103 break;
1104
1105 case AF_INET6:
1106 ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
1107 da->da_addrlen = sizeof(struct sockaddr_in6);
6b7f3cf9
PT
1108 startsep = "[";
1109 endsep = "]";
1110 break;
1111
1112 default:
1113 dprintk("%s: unsupported address family: %u\n",
1114 __func__, da->da_addr.ss_family);
1115 goto out_free_da;
1116 }
1117
4be78d26
TM
1118 da->da_transport = xprt_find_transport_ident(netid);
1119 if (da->da_transport < 0) {
1120 dprintk("%s: ERROR: unknown r_netid \"%s\"\n",
1121 __func__, netid);
6b7f3cf9
PT
1122 goto out_free_da;
1123 }
1124
4be78d26
TM
1125 da->da_netid = netid;
1126
6b7f3cf9
PT
1127 /* save human readable address */
1128 len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
1129 da->da_remotestr = kzalloc(len, gfp_flags);
1130
1131 /* NULL is ok, only used for dprintk */
1132 if (da->da_remotestr)
1133 snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
1134 buf, endsep, ntohs(port));
1135
1136 dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
1137 kfree(buf);
6b7f3cf9
PT
1138 return da;
1139
1140out_free_da:
1141 kfree(da);
1142out_free_buf:
1143 dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
1144 kfree(buf);
1145out_free_netid:
1146 kfree(netid);
1147out_err:
1148 return NULL;
1149}
1150EXPORT_SYMBOL_GPL(nfs4_decode_mp_ds_addr);
338d00cf
TH
1151
1152void
1153pnfs_layout_mark_request_commit(struct nfs_page *req,
1154 struct pnfs_layout_segment *lseg,
1155 struct nfs_commit_info *cinfo,
1156 u32 ds_commit_idx)
1157{
1158 struct list_head *list;
ba827c9a 1159 struct pnfs_commit_array *array;
e18c18eb 1160 struct pnfs_commit_bucket *bucket;
338d00cf 1161
e824f99a 1162 mutex_lock(&NFS_I(cinfo->inode)->commit_mutex);
ba827c9a 1163 array = pnfs_lookup_commit_array(cinfo->ds, lseg);
e18c18eb 1164 if (!array || !pnfs_is_valid_lseg(lseg))
ba827c9a 1165 goto out_resched;
e18c18eb
TM
1166 bucket = &array->buckets[ds_commit_idx];
1167 list = &bucket->written;
1168 /* Non-empty buckets hold a reference on the lseg. That ref
1169 * is normally transferred to the COMMIT call and released
1170 * there. It could also be released if the last req is pulled
1171 * off due to a rewrite, in which case it will be done in
1172 * pnfs_common_clear_request_commit
1173 */
1174 if (!bucket->lseg)
1175 bucket->lseg = pnfs_get_lseg(lseg);
338d00cf
TH
1176 set_bit(PG_COMMIT_TO_DS, &req->wb_flags);
1177 cinfo->ds->nwritten++;
338d00cf 1178
86d80f97 1179 nfs_request_add_commit_list_locked(req, list, cinfo);
e824f99a 1180 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
86d80f97 1181 nfs_mark_page_unstable(req->wb_page, cinfo);
ba827c9a
TM
1182 return;
1183out_resched:
1184 mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex);
1185 cinfo->completion_ops->resched_write(cinfo, req);
338d00cf
TH
1186}
1187EXPORT_SYMBOL_GPL(pnfs_layout_mark_request_commit);
5bb89b47
TM
1188
1189int
1190pnfs_nfs_generic_sync(struct inode *inode, bool datasync)
1191{
2e18d4d8
TM
1192 int ret;
1193
1194 if (!pnfs_layoutcommit_outstanding(inode))
1195 return 0;
1196 ret = nfs_commit_inode(inode, FLUSH_SYNC);
1197 if (ret < 0)
1198 return ret;
5bb89b47
TM
1199 if (datasync)
1200 return 0;
1201 return pnfs_layoutcommit_inode(inode, true);
1202}
1203EXPORT_SYMBOL_GPL(pnfs_nfs_generic_sync);
1204