i2c: designware-master: use core to detect 'no zero length' quirk
[linux/fpc-iii.git] / net / sunrpc / xprtmultipath.c
blobe2d64c7138c3aedb40febb75a6f2936345e3cb11
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Multipath support for RPC
5 * Copyright (c) 2015, 2016, Primary Data, Inc. All rights reserved.
7 * Trond Myklebust <trond.myklebust@primarydata.com>
9 */
10 #include <linux/types.h>
11 #include <linux/kref.h>
12 #include <linux/list.h>
13 #include <linux/rcupdate.h>
14 #include <linux/rculist.h>
15 #include <linux/slab.h>
16 #include <asm/cmpxchg.h>
17 #include <linux/spinlock.h>
18 #include <linux/sunrpc/xprt.h>
19 #include <linux/sunrpc/addr.h>
20 #include <linux/sunrpc/xprtmultipath.h>
22 typedef struct rpc_xprt *(*xprt_switch_find_xprt_t)(struct list_head *head,
23 const struct rpc_xprt *cur);
25 static const struct rpc_xprt_iter_ops rpc_xprt_iter_singular;
26 static const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin;
27 static const struct rpc_xprt_iter_ops rpc_xprt_iter_listall;
29 static void xprt_switch_add_xprt_locked(struct rpc_xprt_switch *xps,
30 struct rpc_xprt *xprt)
32 if (unlikely(xprt_get(xprt) == NULL))
33 return;
34 list_add_tail_rcu(&xprt->xprt_switch, &xps->xps_xprt_list);
35 smp_wmb();
36 if (xps->xps_nxprts == 0)
37 xps->xps_net = xprt->xprt_net;
38 xps->xps_nxprts++;
41 /**
42 * rpc_xprt_switch_add_xprt - Add a new rpc_xprt to an rpc_xprt_switch
43 * @xps: pointer to struct rpc_xprt_switch
44 * @xprt: pointer to struct rpc_xprt
46 * Adds xprt to the end of the list of struct rpc_xprt in xps.
48 void rpc_xprt_switch_add_xprt(struct rpc_xprt_switch *xps,
49 struct rpc_xprt *xprt)
51 if (xprt == NULL)
52 return;
53 spin_lock(&xps->xps_lock);
54 if ((xps->xps_net == xprt->xprt_net || xps->xps_net == NULL) &&
55 !rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
56 xprt_switch_add_xprt_locked(xps, xprt);
57 spin_unlock(&xps->xps_lock);
60 static void xprt_switch_remove_xprt_locked(struct rpc_xprt_switch *xps,
61 struct rpc_xprt *xprt)
63 if (unlikely(xprt == NULL))
64 return;
65 xps->xps_nxprts--;
66 if (xps->xps_nxprts == 0)
67 xps->xps_net = NULL;
68 smp_wmb();
69 list_del_rcu(&xprt->xprt_switch);
72 /**
73 * rpc_xprt_switch_remove_xprt - Removes an rpc_xprt from a rpc_xprt_switch
74 * @xps: pointer to struct rpc_xprt_switch
75 * @xprt: pointer to struct rpc_xprt
77 * Removes xprt from the list of struct rpc_xprt in xps.
79 void rpc_xprt_switch_remove_xprt(struct rpc_xprt_switch *xps,
80 struct rpc_xprt *xprt)
82 spin_lock(&xps->xps_lock);
83 xprt_switch_remove_xprt_locked(xps, xprt);
84 spin_unlock(&xps->xps_lock);
85 xprt_put(xprt);
88 /**
89 * xprt_switch_alloc - Allocate a new struct rpc_xprt_switch
90 * @xprt: pointer to struct rpc_xprt
91 * @gfp_flags: allocation flags
93 * On success, returns an initialised struct rpc_xprt_switch, containing
94 * the entry xprt. Returns NULL on failure.
96 struct rpc_xprt_switch *xprt_switch_alloc(struct rpc_xprt *xprt,
97 gfp_t gfp_flags)
99 struct rpc_xprt_switch *xps;
101 xps = kmalloc(sizeof(*xps), gfp_flags);
102 if (xps != NULL) {
103 spin_lock_init(&xps->xps_lock);
104 kref_init(&xps->xps_kref);
105 xps->xps_nxprts = 0;
106 INIT_LIST_HEAD(&xps->xps_xprt_list);
107 xps->xps_iter_ops = &rpc_xprt_iter_singular;
108 xprt_switch_add_xprt_locked(xps, xprt);
111 return xps;
114 static void xprt_switch_free_entries(struct rpc_xprt_switch *xps)
116 spin_lock(&xps->xps_lock);
117 while (!list_empty(&xps->xps_xprt_list)) {
118 struct rpc_xprt *xprt;
120 xprt = list_first_entry(&xps->xps_xprt_list,
121 struct rpc_xprt, xprt_switch);
122 xprt_switch_remove_xprt_locked(xps, xprt);
123 spin_unlock(&xps->xps_lock);
124 xprt_put(xprt);
125 spin_lock(&xps->xps_lock);
127 spin_unlock(&xps->xps_lock);
130 static void xprt_switch_free(struct kref *kref)
132 struct rpc_xprt_switch *xps = container_of(kref,
133 struct rpc_xprt_switch, xps_kref);
135 xprt_switch_free_entries(xps);
136 kfree_rcu(xps, xps_rcu);
140 * xprt_switch_get - Return a reference to a rpc_xprt_switch
141 * @xps: pointer to struct rpc_xprt_switch
143 * Returns a reference to xps unless the refcount is already zero.
145 struct rpc_xprt_switch *xprt_switch_get(struct rpc_xprt_switch *xps)
147 if (xps != NULL && kref_get_unless_zero(&xps->xps_kref))
148 return xps;
149 return NULL;
153 * xprt_switch_put - Release a reference to a rpc_xprt_switch
154 * @xps: pointer to struct rpc_xprt_switch
156 * Release the reference to xps, and free it once the refcount is zero.
158 void xprt_switch_put(struct rpc_xprt_switch *xps)
160 if (xps != NULL)
161 kref_put(&xps->xps_kref, xprt_switch_free);
165 * rpc_xprt_switch_set_roundrobin - Set a round-robin policy on rpc_xprt_switch
166 * @xps: pointer to struct rpc_xprt_switch
168 * Sets a round-robin default policy for iterators acting on xps.
170 void rpc_xprt_switch_set_roundrobin(struct rpc_xprt_switch *xps)
172 if (READ_ONCE(xps->xps_iter_ops) != &rpc_xprt_iter_roundrobin)
173 WRITE_ONCE(xps->xps_iter_ops, &rpc_xprt_iter_roundrobin);
176 static
177 const struct rpc_xprt_iter_ops *xprt_iter_ops(const struct rpc_xprt_iter *xpi)
179 if (xpi->xpi_ops != NULL)
180 return xpi->xpi_ops;
181 return rcu_dereference(xpi->xpi_xpswitch)->xps_iter_ops;
184 static
185 void xprt_iter_no_rewind(struct rpc_xprt_iter *xpi)
189 static
190 void xprt_iter_default_rewind(struct rpc_xprt_iter *xpi)
192 WRITE_ONCE(xpi->xpi_cursor, NULL);
195 static
196 struct rpc_xprt *xprt_switch_find_first_entry(struct list_head *head)
198 return list_first_or_null_rcu(head, struct rpc_xprt, xprt_switch);
201 static
202 struct rpc_xprt *xprt_iter_first_entry(struct rpc_xprt_iter *xpi)
204 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
206 if (xps == NULL)
207 return NULL;
208 return xprt_switch_find_first_entry(&xps->xps_xprt_list);
211 static
212 struct rpc_xprt *xprt_switch_find_current_entry(struct list_head *head,
213 const struct rpc_xprt *cur)
215 struct rpc_xprt *pos;
217 list_for_each_entry_rcu(pos, head, xprt_switch) {
218 if (cur == pos)
219 return pos;
221 return NULL;
224 static
225 struct rpc_xprt *xprt_iter_current_entry(struct rpc_xprt_iter *xpi)
227 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
228 struct list_head *head;
230 if (xps == NULL)
231 return NULL;
232 head = &xps->xps_xprt_list;
233 if (xpi->xpi_cursor == NULL || xps->xps_nxprts < 2)
234 return xprt_switch_find_first_entry(head);
235 return xprt_switch_find_current_entry(head, xpi->xpi_cursor);
238 bool rpc_xprt_switch_has_addr(struct rpc_xprt_switch *xps,
239 const struct sockaddr *sap)
241 struct list_head *head;
242 struct rpc_xprt *pos;
244 if (xps == NULL || sap == NULL)
245 return false;
247 head = &xps->xps_xprt_list;
248 list_for_each_entry_rcu(pos, head, xprt_switch) {
249 if (rpc_cmp_addr_port(sap, (struct sockaddr *)&pos->addr)) {
250 pr_info("RPC: addr %s already in xprt switch\n",
251 pos->address_strings[RPC_DISPLAY_ADDR]);
252 return true;
255 return false;
258 static
259 struct rpc_xprt *xprt_switch_find_next_entry(struct list_head *head,
260 const struct rpc_xprt *cur)
262 struct rpc_xprt *pos, *prev = NULL;
264 list_for_each_entry_rcu(pos, head, xprt_switch) {
265 if (cur == prev)
266 return pos;
267 prev = pos;
269 return NULL;
272 static
273 struct rpc_xprt *xprt_switch_set_next_cursor(struct list_head *head,
274 struct rpc_xprt **cursor,
275 xprt_switch_find_xprt_t find_next)
277 struct rpc_xprt *cur, *pos, *old;
279 cur = READ_ONCE(*cursor);
280 for (;;) {
281 old = cur;
282 pos = find_next(head, old);
283 if (pos == NULL)
284 break;
285 cur = cmpxchg_relaxed(cursor, old, pos);
286 if (cur == old)
287 break;
289 return pos;
292 static
293 struct rpc_xprt *xprt_iter_next_entry_multiple(struct rpc_xprt_iter *xpi,
294 xprt_switch_find_xprt_t find_next)
296 struct rpc_xprt_switch *xps = rcu_dereference(xpi->xpi_xpswitch);
298 if (xps == NULL)
299 return NULL;
300 return xprt_switch_set_next_cursor(&xps->xps_xprt_list,
301 &xpi->xpi_cursor,
302 find_next);
305 static
306 struct rpc_xprt *xprt_switch_find_next_entry_roundrobin(struct list_head *head,
307 const struct rpc_xprt *cur)
309 struct rpc_xprt *ret;
311 ret = xprt_switch_find_next_entry(head, cur);
312 if (ret != NULL)
313 return ret;
314 return xprt_switch_find_first_entry(head);
317 static
318 struct rpc_xprt *xprt_iter_next_entry_roundrobin(struct rpc_xprt_iter *xpi)
320 return xprt_iter_next_entry_multiple(xpi,
321 xprt_switch_find_next_entry_roundrobin);
324 static
325 struct rpc_xprt *xprt_iter_next_entry_all(struct rpc_xprt_iter *xpi)
327 return xprt_iter_next_entry_multiple(xpi, xprt_switch_find_next_entry);
331 * xprt_iter_rewind - Resets the xprt iterator
332 * @xpi: pointer to rpc_xprt_iter
334 * Resets xpi to ensure that it points to the first entry in the list
335 * of transports.
337 static
338 void xprt_iter_rewind(struct rpc_xprt_iter *xpi)
340 rcu_read_lock();
341 xprt_iter_ops(xpi)->xpi_rewind(xpi);
342 rcu_read_unlock();
345 static void __xprt_iter_init(struct rpc_xprt_iter *xpi,
346 struct rpc_xprt_switch *xps,
347 const struct rpc_xprt_iter_ops *ops)
349 rcu_assign_pointer(xpi->xpi_xpswitch, xprt_switch_get(xps));
350 xpi->xpi_cursor = NULL;
351 xpi->xpi_ops = ops;
355 * xprt_iter_init - Initialise an xprt iterator
356 * @xpi: pointer to rpc_xprt_iter
357 * @xps: pointer to rpc_xprt_switch
359 * Initialises the iterator to use the default iterator ops
360 * as set in xps. This function is mainly intended for internal
361 * use in the rpc_client.
363 void xprt_iter_init(struct rpc_xprt_iter *xpi,
364 struct rpc_xprt_switch *xps)
366 __xprt_iter_init(xpi, xps, NULL);
370 * xprt_iter_init_listall - Initialise an xprt iterator
371 * @xpi: pointer to rpc_xprt_iter
372 * @xps: pointer to rpc_xprt_switch
374 * Initialises the iterator to iterate once through the entire list
375 * of entries in xps.
377 void xprt_iter_init_listall(struct rpc_xprt_iter *xpi,
378 struct rpc_xprt_switch *xps)
380 __xprt_iter_init(xpi, xps, &rpc_xprt_iter_listall);
384 * xprt_iter_xchg_switch - Atomically swap out the rpc_xprt_switch
385 * @xpi: pointer to rpc_xprt_iter
386 * @xps: pointer to a new rpc_xprt_switch or NULL
388 * Swaps out the existing xpi->xpi_xpswitch with a new value.
390 struct rpc_xprt_switch *xprt_iter_xchg_switch(struct rpc_xprt_iter *xpi,
391 struct rpc_xprt_switch *newswitch)
393 struct rpc_xprt_switch __rcu *oldswitch;
395 /* Atomically swap out the old xpswitch */
396 oldswitch = xchg(&xpi->xpi_xpswitch, RCU_INITIALIZER(newswitch));
397 if (newswitch != NULL)
398 xprt_iter_rewind(xpi);
399 return rcu_dereference_protected(oldswitch, true);
403 * xprt_iter_destroy - Destroys the xprt iterator
404 * @xpi pointer to rpc_xprt_iter
406 void xprt_iter_destroy(struct rpc_xprt_iter *xpi)
408 xprt_switch_put(xprt_iter_xchg_switch(xpi, NULL));
412 * xprt_iter_xprt - Returns the rpc_xprt pointed to by the cursor
413 * @xpi: pointer to rpc_xprt_iter
415 * Returns a pointer to the struct rpc_xprt that is currently
416 * pointed to by the cursor.
417 * Caller must be holding rcu_read_lock().
419 struct rpc_xprt *xprt_iter_xprt(struct rpc_xprt_iter *xpi)
421 WARN_ON_ONCE(!rcu_read_lock_held());
422 return xprt_iter_ops(xpi)->xpi_xprt(xpi);
425 static
426 struct rpc_xprt *xprt_iter_get_helper(struct rpc_xprt_iter *xpi,
427 struct rpc_xprt *(*fn)(struct rpc_xprt_iter *))
429 struct rpc_xprt *ret;
431 do {
432 ret = fn(xpi);
433 if (ret == NULL)
434 break;
435 ret = xprt_get(ret);
436 } while (ret == NULL);
437 return ret;
441 * xprt_iter_get_xprt - Returns the rpc_xprt pointed to by the cursor
442 * @xpi: pointer to rpc_xprt_iter
444 * Returns a reference to the struct rpc_xprt that is currently
445 * pointed to by the cursor.
447 struct rpc_xprt *xprt_iter_get_xprt(struct rpc_xprt_iter *xpi)
449 struct rpc_xprt *xprt;
451 rcu_read_lock();
452 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_xprt);
453 rcu_read_unlock();
454 return xprt;
458 * xprt_iter_get_next - Returns the next rpc_xprt following the cursor
459 * @xpi: pointer to rpc_xprt_iter
461 * Returns a reference to the struct rpc_xprt that immediately follows the
462 * entry pointed to by the cursor.
464 struct rpc_xprt *xprt_iter_get_next(struct rpc_xprt_iter *xpi)
466 struct rpc_xprt *xprt;
468 rcu_read_lock();
469 xprt = xprt_iter_get_helper(xpi, xprt_iter_ops(xpi)->xpi_next);
470 rcu_read_unlock();
471 return xprt;
474 /* Policy for always returning the first entry in the rpc_xprt_switch */
475 static
476 const struct rpc_xprt_iter_ops rpc_xprt_iter_singular = {
477 .xpi_rewind = xprt_iter_no_rewind,
478 .xpi_xprt = xprt_iter_first_entry,
479 .xpi_next = xprt_iter_first_entry,
482 /* Policy for round-robin iteration of entries in the rpc_xprt_switch */
483 static
484 const struct rpc_xprt_iter_ops rpc_xprt_iter_roundrobin = {
485 .xpi_rewind = xprt_iter_default_rewind,
486 .xpi_xprt = xprt_iter_current_entry,
487 .xpi_next = xprt_iter_next_entry_roundrobin,
490 /* Policy for once-through iteration of entries in the rpc_xprt_switch */
491 static
492 const struct rpc_xprt_iter_ops rpc_xprt_iter_listall = {
493 .xpi_rewind = xprt_iter_default_rewind,
494 .xpi_xprt = xprt_iter_current_entry,
495 .xpi_next = xprt_iter_next_entry_all,