xref: /openbmc/linux/fs/afs/fs_probe.c (revision ff75ec43a2f6fbf7049472312bab322d77eb1bde)
1b4d0d230SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
23bf0fb6fSDavid Howells /* AFS fileserver probing
33bf0fb6fSDavid Howells  *
4f6cbb368SDavid Howells  * Copyright (C) 2018, 2020 Red Hat, Inc. All Rights Reserved.
53bf0fb6fSDavid Howells  * Written by David Howells (dhowells@redhat.com)
63bf0fb6fSDavid Howells  */
73bf0fb6fSDavid Howells 
83bf0fb6fSDavid Howells #include <linux/sched.h>
93bf0fb6fSDavid Howells #include <linux/slab.h>
103bf0fb6fSDavid Howells #include "afs_fs.h"
113bf0fb6fSDavid Howells #include "internal.h"
12b537a3c2SDavid Howells #include "protocol_afs.h"
133bf0fb6fSDavid Howells #include "protocol_yfs.h"
143bf0fb6fSDavid Howells 
15f6cbb368SDavid Howells static unsigned int afs_fs_probe_fast_poll_interval = 30 * HZ;
16f6cbb368SDavid Howells static unsigned int afs_fs_probe_slow_poll_interval = 5 * 60 * HZ;
173bf0fb6fSDavid Howells 
18f6cbb368SDavid Howells /*
19f6cbb368SDavid Howells  * Start the probe polling timer.  We have to supply it with an inc on the
20f6cbb368SDavid Howells  * outstanding server count.
21f6cbb368SDavid Howells  */
afs_schedule_fs_probe(struct afs_net * net,struct afs_server * server,bool fast)22f6cbb368SDavid Howells static void afs_schedule_fs_probe(struct afs_net *net,
23f6cbb368SDavid Howells 				  struct afs_server *server, bool fast)
24f6cbb368SDavid Howells {
25f6cbb368SDavid Howells 	unsigned long atj;
26f6cbb368SDavid Howells 
27f6cbb368SDavid Howells 	if (!net->live)
28f6cbb368SDavid Howells 		return;
29f6cbb368SDavid Howells 
30f6cbb368SDavid Howells 	atj = server->probed_at;
31f6cbb368SDavid Howells 	atj += fast ? afs_fs_probe_fast_poll_interval : afs_fs_probe_slow_poll_interval;
32f6cbb368SDavid Howells 
33f6cbb368SDavid Howells 	afs_inc_servers_outstanding(net);
34f6cbb368SDavid Howells 	if (timer_reduce(&net->fs_probe_timer, atj))
35f6cbb368SDavid Howells 		afs_dec_servers_outstanding(net);
36f6cbb368SDavid Howells }
37f6cbb368SDavid Howells 
38f6cbb368SDavid Howells /*
39f6cbb368SDavid Howells  * Handle the completion of a set of probes.
40f6cbb368SDavid Howells  */
afs_finished_fs_probe(struct afs_net * net,struct afs_server * server)41f6cbb368SDavid Howells static void afs_finished_fs_probe(struct afs_net *net, struct afs_server *server)
42f6cbb368SDavid Howells {
43f6cbb368SDavid Howells 	bool responded = server->probe.responded;
44f6cbb368SDavid Howells 
45f6cbb368SDavid Howells 	write_seqlock(&net->fs_lock);
46f3c130e6SDavid Howells 	if (responded) {
47f6cbb368SDavid Howells 		list_add_tail(&server->probe_link, &net->fs_probe_slow);
48f3c130e6SDavid Howells 	} else {
49f3c130e6SDavid Howells 		server->rtt = UINT_MAX;
50f3c130e6SDavid Howells 		clear_bit(AFS_SERVER_FL_RESPONDING, &server->flags);
51f6cbb368SDavid Howells 		list_add_tail(&server->probe_link, &net->fs_probe_fast);
52f3c130e6SDavid Howells 	}
53f6cbb368SDavid Howells 	write_sequnlock(&net->fs_lock);
54f6cbb368SDavid Howells 
55f6cbb368SDavid Howells 	afs_schedule_fs_probe(net, server, !responded);
56f6cbb368SDavid Howells }
57f6cbb368SDavid Howells 
58f6cbb368SDavid Howells /*
59f6cbb368SDavid Howells  * Handle the completion of a probe.
60f6cbb368SDavid Howells  */
afs_done_one_fs_probe(struct afs_net * net,struct afs_server * server)61f6cbb368SDavid Howells static void afs_done_one_fs_probe(struct afs_net *net, struct afs_server *server)
62f6cbb368SDavid Howells {
63f6cbb368SDavid Howells 	_enter("");
64f6cbb368SDavid Howells 
65f6cbb368SDavid Howells 	if (atomic_dec_and_test(&server->probe_outstanding))
66f6cbb368SDavid Howells 		afs_finished_fs_probe(net, server);
67f6cbb368SDavid Howells 
68f6cbb368SDavid Howells 	wake_up_all(&server->probe_wq);
69f6cbb368SDavid Howells }
70f6cbb368SDavid Howells 
71f6cbb368SDavid Howells /*
72f6cbb368SDavid Howells  * Handle inability to send a probe due to ENOMEM when trying to allocate a
73f6cbb368SDavid Howells  * call struct.
74f6cbb368SDavid Howells  */
afs_fs_probe_not_done(struct afs_net * net,struct afs_server * server,struct afs_addr_cursor * ac)75f6cbb368SDavid Howells static void afs_fs_probe_not_done(struct afs_net *net,
76f6cbb368SDavid Howells 				  struct afs_server *server,
77f6cbb368SDavid Howells 				  struct afs_addr_cursor *ac)
78f6cbb368SDavid Howells {
79f6cbb368SDavid Howells 	struct afs_addr_list *alist = ac->alist;
80f6cbb368SDavid Howells 	unsigned int index = ac->index;
81f6cbb368SDavid Howells 
82f6cbb368SDavid Howells 	_enter("");
83f6cbb368SDavid Howells 
84f6cbb368SDavid Howells 	trace_afs_io_error(0, -ENOMEM, afs_io_error_fs_probe_fail);
85f6cbb368SDavid Howells 	spin_lock(&server->probe_lock);
86f6cbb368SDavid Howells 
87f6cbb368SDavid Howells 	server->probe.local_failure = true;
88f6cbb368SDavid Howells 	if (server->probe.error == 0)
89f6cbb368SDavid Howells 		server->probe.error = -ENOMEM;
90f6cbb368SDavid Howells 
91f6cbb368SDavid Howells 	set_bit(index, &alist->failed);
92f6cbb368SDavid Howells 
93f6cbb368SDavid Howells 	spin_unlock(&server->probe_lock);
94f6cbb368SDavid Howells 	return afs_done_one_fs_probe(net, server);
953bf0fb6fSDavid Howells }
963bf0fb6fSDavid Howells 
973bf0fb6fSDavid Howells /*
983bf0fb6fSDavid Howells  * Process the result of probing a fileserver.  This is called after successful
993bf0fb6fSDavid Howells  * or failed delivery of an FS.GetCapabilities operation.
1003bf0fb6fSDavid Howells  */
afs_fileserver_probe_result(struct afs_call * call)1013bf0fb6fSDavid Howells void afs_fileserver_probe_result(struct afs_call *call)
1023bf0fb6fSDavid Howells {
1033bf0fb6fSDavid Howells 	struct afs_addr_list *alist = call->alist;
104ffba718eSDavid Howells 	struct afs_server *server = call->server;
1053bf0fb6fSDavid Howells 	unsigned int index = call->addr_ix;
106b537a3c2SDavid Howells 	unsigned int rtt_us = 0, cap0;
1073bf0fb6fSDavid Howells 	int ret = call->error;
1083bf0fb6fSDavid Howells 
1093bf0fb6fSDavid Howells 	_enter("%pU,%u", &server->uuid, index);
1103bf0fb6fSDavid Howells 
1113bf0fb6fSDavid Howells 	spin_lock(&server->probe_lock);
1123bf0fb6fSDavid Howells 
1133bf0fb6fSDavid Howells 	switch (ret) {
1143bf0fb6fSDavid Howells 	case 0:
1153bf0fb6fSDavid Howells 		server->probe.error = 0;
1163bf0fb6fSDavid Howells 		goto responded;
1173bf0fb6fSDavid Howells 	case -ECONNABORTED:
1183bf0fb6fSDavid Howells 		if (!server->probe.responded) {
1193bf0fb6fSDavid Howells 			server->probe.abort_code = call->abort_code;
1203bf0fb6fSDavid Howells 			server->probe.error = ret;
1213bf0fb6fSDavid Howells 		}
1223bf0fb6fSDavid Howells 		goto responded;
1233bf0fb6fSDavid Howells 	case -ENOMEM:
1243bf0fb6fSDavid Howells 	case -ENONET:
125f6cbb368SDavid Howells 		clear_bit(index, &alist->responded);
1263bf0fb6fSDavid Howells 		server->probe.local_failure = true;
127f6cbb368SDavid Howells 		trace_afs_io_error(call->debug_id, ret, afs_io_error_fs_probe_fail);
1283bf0fb6fSDavid Howells 		goto out;
1293bf0fb6fSDavid Howells 	case -ECONNRESET: /* Responded, but call expired. */
1304584ae96SDavid Howells 	case -ERFKILL:
1314584ae96SDavid Howells 	case -EADDRNOTAVAIL:
1323bf0fb6fSDavid Howells 	case -ENETUNREACH:
1333bf0fb6fSDavid Howells 	case -EHOSTUNREACH:
1344584ae96SDavid Howells 	case -EHOSTDOWN:
1353bf0fb6fSDavid Howells 	case -ECONNREFUSED:
1363bf0fb6fSDavid Howells 	case -ETIMEDOUT:
1373bf0fb6fSDavid Howells 	case -ETIME:
1383bf0fb6fSDavid Howells 	default:
1393bf0fb6fSDavid Howells 		clear_bit(index, &alist->responded);
1403bf0fb6fSDavid Howells 		set_bit(index, &alist->failed);
1413bf0fb6fSDavid Howells 		if (!server->probe.responded &&
1423bf0fb6fSDavid Howells 		    (server->probe.error == 0 ||
1433bf0fb6fSDavid Howells 		     server->probe.error == -ETIMEDOUT ||
1443bf0fb6fSDavid Howells 		     server->probe.error == -ETIME))
1453bf0fb6fSDavid Howells 			server->probe.error = ret;
146f6cbb368SDavid Howells 		trace_afs_io_error(call->debug_id, ret, afs_io_error_fs_probe_fail);
1473bf0fb6fSDavid Howells 		goto out;
1483bf0fb6fSDavid Howells 	}
1493bf0fb6fSDavid Howells 
1503bf0fb6fSDavid Howells responded:
1513bf0fb6fSDavid Howells 	clear_bit(index, &alist->failed);
1523bf0fb6fSDavid Howells 
1533bf0fb6fSDavid Howells 	if (call->service_id == YFS_FS_SERVICE) {
1543bf0fb6fSDavid Howells 		server->probe.is_yfs = true;
1553bf0fb6fSDavid Howells 		set_bit(AFS_SERVER_FL_IS_YFS, &server->flags);
1563bf0fb6fSDavid Howells 		alist->addrs[index].srx_service = call->service_id;
1573bf0fb6fSDavid Howells 	} else {
1583bf0fb6fSDavid Howells 		server->probe.not_yfs = true;
1593bf0fb6fSDavid Howells 		if (!server->probe.is_yfs) {
1603bf0fb6fSDavid Howells 			clear_bit(AFS_SERVER_FL_IS_YFS, &server->flags);
1613bf0fb6fSDavid Howells 			alist->addrs[index].srx_service = call->service_id;
1623bf0fb6fSDavid Howells 		}
163b537a3c2SDavid Howells 		cap0 = ntohl(call->tmp);
164b537a3c2SDavid Howells 		if (cap0 & AFS3_VICED_CAPABILITY_64BITFILES)
165b537a3c2SDavid Howells 			set_bit(AFS_SERVER_FL_HAS_FS64, &server->flags);
166b537a3c2SDavid Howells 		else
167b537a3c2SDavid Howells 			clear_bit(AFS_SERVER_FL_HAS_FS64, &server->flags);
1683bf0fb6fSDavid Howells 	}
1693bf0fb6fSDavid Howells 
170ca57f022SDavid Howells 	rxrpc_kernel_get_srtt(call->net->socket, call->rxcall, &rtt_us);
171ca57f022SDavid Howells 	if (rtt_us < server->probe.rtt) {
172c410bf01SDavid Howells 		server->probe.rtt = rtt_us;
173f3c130e6SDavid Howells 		server->rtt = rtt_us;
1743bf0fb6fSDavid Howells 		alist->preferred = index;
1753bf0fb6fSDavid Howells 	}
1763bf0fb6fSDavid Howells 
1773bf0fb6fSDavid Howells 	smp_wmb(); /* Set rtt before responded. */
1783bf0fb6fSDavid Howells 	server->probe.responded = true;
179f6cbb368SDavid Howells 	set_bit(index, &alist->responded);
180f3c130e6SDavid Howells 	set_bit(AFS_SERVER_FL_RESPONDING, &server->flags);
1813bf0fb6fSDavid Howells out:
1823bf0fb6fSDavid Howells 	spin_unlock(&server->probe_lock);
1833bf0fb6fSDavid Howells 
184f6cbb368SDavid Howells 	_debug("probe %pU [%u] %pISpc rtt=%u ret=%d",
185f6cbb368SDavid Howells 	       &server->uuid, index, &alist->addrs[index].transport,
186f6cbb368SDavid Howells 	       rtt_us, ret);
1873bf0fb6fSDavid Howells 
188f6cbb368SDavid Howells 	return afs_done_one_fs_probe(call->net, server);
1893bf0fb6fSDavid Howells }
1903bf0fb6fSDavid Howells 
1913bf0fb6fSDavid Howells /*
192f6cbb368SDavid Howells  * Probe one or all of a fileserver's addresses to find out the best route and
193f6cbb368SDavid Howells  * to query its capabilities.
1943bf0fb6fSDavid Howells  */
afs_fs_probe_fileserver(struct afs_net * net,struct afs_server * server,struct key * key,bool all)195f6cbb368SDavid Howells void afs_fs_probe_fileserver(struct afs_net *net, struct afs_server *server,
196f6cbb368SDavid Howells 			     struct key *key, bool all)
1973bf0fb6fSDavid Howells {
1983bf0fb6fSDavid Howells 	struct afs_addr_cursor ac = {
1993bf0fb6fSDavid Howells 		.index = 0,
2003bf0fb6fSDavid Howells 	};
2013bf0fb6fSDavid Howells 
2023bf0fb6fSDavid Howells 	_enter("%pU", &server->uuid);
2033bf0fb6fSDavid Howells 
2043bf0fb6fSDavid Howells 	read_lock(&server->fs_lock);
2053bf0fb6fSDavid Howells 	ac.alist = rcu_dereference_protected(server->addresses,
2063bf0fb6fSDavid Howells 					     lockdep_is_held(&server->fs_lock));
2079efcc4a1SDavid Howells 	afs_get_addrlist(ac.alist);
2083bf0fb6fSDavid Howells 	read_unlock(&server->fs_lock);
2093bf0fb6fSDavid Howells 
210f6cbb368SDavid Howells 	server->probed_at = jiffies;
211f6cbb368SDavid Howells 	atomic_set(&server->probe_outstanding, all ? ac.alist->nr_addrs : 1);
2123bf0fb6fSDavid Howells 	memset(&server->probe, 0, sizeof(server->probe));
2133bf0fb6fSDavid Howells 	server->probe.rtt = UINT_MAX;
2143bf0fb6fSDavid Howells 
215f6cbb368SDavid Howells 	ac.index = ac.alist->preferred;
216f6cbb368SDavid Howells 	if (ac.index < 0 || ac.index >= ac.alist->nr_addrs)
217f6cbb368SDavid Howells 		all = true;
218f6cbb368SDavid Howells 
219f6cbb368SDavid Howells 	if (all) {
220f6cbb368SDavid Howells 		for (ac.index = 0; ac.index < ac.alist->nr_addrs; ac.index++)
221f6cbb368SDavid Howells 			if (!afs_fs_get_capabilities(net, server, &ac, key))
222f6cbb368SDavid Howells 				afs_fs_probe_not_done(net, server, &ac);
2230b9bf381SDavid Howells 	} else {
224f6cbb368SDavid Howells 		if (!afs_fs_get_capabilities(net, server, &ac, key))
225f6cbb368SDavid Howells 			afs_fs_probe_not_done(net, server, &ac);
2263bf0fb6fSDavid Howells 	}
2273bf0fb6fSDavid Howells 
2289efcc4a1SDavid Howells 	afs_put_addrlist(ac.alist);
2293bf0fb6fSDavid Howells }
2303bf0fb6fSDavid Howells 
2313bf0fb6fSDavid Howells /*
2323bf0fb6fSDavid Howells  * Wait for the first as-yet untried fileserver to respond.
2333bf0fb6fSDavid Howells  */
afs_wait_for_fs_probes(struct afs_server_list * slist,unsigned long untried)2343bf0fb6fSDavid Howells int afs_wait_for_fs_probes(struct afs_server_list *slist, unsigned long untried)
2353bf0fb6fSDavid Howells {
2363bf0fb6fSDavid Howells 	struct wait_queue_entry *waits;
2373bf0fb6fSDavid Howells 	struct afs_server *server;
238f3c130e6SDavid Howells 	unsigned int rtt = UINT_MAX, rtt_s;
2393bf0fb6fSDavid Howells 	bool have_responders = false;
2403bf0fb6fSDavid Howells 	int pref = -1, i;
2413bf0fb6fSDavid Howells 
2423bf0fb6fSDavid Howells 	_enter("%u,%lx", slist->nr_servers, untried);
2433bf0fb6fSDavid Howells 
2443bf0fb6fSDavid Howells 	/* Only wait for servers that have a probe outstanding. */
2453bf0fb6fSDavid Howells 	for (i = 0; i < slist->nr_servers; i++) {
2463bf0fb6fSDavid Howells 		if (test_bit(i, &untried)) {
2473bf0fb6fSDavid Howells 			server = slist->servers[i].server;
248f6cbb368SDavid Howells 			if (!atomic_read(&server->probe_outstanding))
2493bf0fb6fSDavid Howells 				__clear_bit(i, &untried);
2503bf0fb6fSDavid Howells 			if (server->probe.responded)
2513bf0fb6fSDavid Howells 				have_responders = true;
2523bf0fb6fSDavid Howells 		}
2533bf0fb6fSDavid Howells 	}
2543bf0fb6fSDavid Howells 	if (have_responders || !untried)
2553bf0fb6fSDavid Howells 		return 0;
2563bf0fb6fSDavid Howells 
2573bf0fb6fSDavid Howells 	waits = kmalloc(array_size(slist->nr_servers, sizeof(*waits)), GFP_KERNEL);
2583bf0fb6fSDavid Howells 	if (!waits)
2593bf0fb6fSDavid Howells 		return -ENOMEM;
2603bf0fb6fSDavid Howells 
2613bf0fb6fSDavid Howells 	for (i = 0; i < slist->nr_servers; i++) {
2623bf0fb6fSDavid Howells 		if (test_bit(i, &untried)) {
2633bf0fb6fSDavid Howells 			server = slist->servers[i].server;
2643bf0fb6fSDavid Howells 			init_waitqueue_entry(&waits[i], current);
2653bf0fb6fSDavid Howells 			add_wait_queue(&server->probe_wq, &waits[i]);
2663bf0fb6fSDavid Howells 		}
2673bf0fb6fSDavid Howells 	}
2683bf0fb6fSDavid Howells 
2693bf0fb6fSDavid Howells 	for (;;) {
2703bf0fb6fSDavid Howells 		bool still_probing = false;
2713bf0fb6fSDavid Howells 
2723bf0fb6fSDavid Howells 		set_current_state(TASK_INTERRUPTIBLE);
2733bf0fb6fSDavid Howells 		for (i = 0; i < slist->nr_servers; i++) {
2743bf0fb6fSDavid Howells 			if (test_bit(i, &untried)) {
2753bf0fb6fSDavid Howells 				server = slist->servers[i].server;
2763bf0fb6fSDavid Howells 				if (server->probe.responded)
2773bf0fb6fSDavid Howells 					goto stop;
278f6cbb368SDavid Howells 				if (atomic_read(&server->probe_outstanding))
2793bf0fb6fSDavid Howells 					still_probing = true;
2803bf0fb6fSDavid Howells 			}
2813bf0fb6fSDavid Howells 		}
2823bf0fb6fSDavid Howells 
28308d405c8SDavidlohr Bueso 		if (!still_probing || signal_pending(current))
2843bf0fb6fSDavid Howells 			goto stop;
2853bf0fb6fSDavid Howells 		schedule();
2863bf0fb6fSDavid Howells 	}
2873bf0fb6fSDavid Howells 
2883bf0fb6fSDavid Howells stop:
2893bf0fb6fSDavid Howells 	set_current_state(TASK_RUNNING);
2903bf0fb6fSDavid Howells 
2913bf0fb6fSDavid Howells 	for (i = 0; i < slist->nr_servers; i++) {
2923bf0fb6fSDavid Howells 		if (test_bit(i, &untried)) {
2933bf0fb6fSDavid Howells 			server = slist->servers[i].server;
294f3c130e6SDavid Howells 			rtt_s = READ_ONCE(server->rtt);
295f3c130e6SDavid Howells 			if (test_bit(AFS_SERVER_FL_RESPONDING, &server->flags) &&
296f3c130e6SDavid Howells 			    rtt_s < rtt) {
2973bf0fb6fSDavid Howells 				pref = i;
298f3c130e6SDavid Howells 				rtt = rtt_s;
2993bf0fb6fSDavid Howells 			}
3003bf0fb6fSDavid Howells 
3013bf0fb6fSDavid Howells 			remove_wait_queue(&server->probe_wq, &waits[i]);
3023bf0fb6fSDavid Howells 		}
3033bf0fb6fSDavid Howells 	}
3043bf0fb6fSDavid Howells 
3053bf0fb6fSDavid Howells 	kfree(waits);
3063bf0fb6fSDavid Howells 
3073bf0fb6fSDavid Howells 	if (pref == -1 && signal_pending(current))
3083bf0fb6fSDavid Howells 		return -ERESTARTSYS;
3093bf0fb6fSDavid Howells 
3103bf0fb6fSDavid Howells 	if (pref >= 0)
3113bf0fb6fSDavid Howells 		slist->preferred = pref;
3123bf0fb6fSDavid Howells 	return 0;
3133bf0fb6fSDavid Howells }
314f6cbb368SDavid Howells 
315f6cbb368SDavid Howells /*
316f6cbb368SDavid Howells  * Probe timer.  We have an increment on fs_outstanding that we need to pass
317f6cbb368SDavid Howells  * along to the work item.
318f6cbb368SDavid Howells  */
afs_fs_probe_timer(struct timer_list * timer)319f6cbb368SDavid Howells void afs_fs_probe_timer(struct timer_list *timer)
320f6cbb368SDavid Howells {
321f6cbb368SDavid Howells 	struct afs_net *net = container_of(timer, struct afs_net, fs_probe_timer);
322f6cbb368SDavid Howells 
3235481fc6eSDavid Howells 	if (!net->live || !queue_work(afs_wq, &net->fs_prober))
324f6cbb368SDavid Howells 		afs_dec_servers_outstanding(net);
325f6cbb368SDavid Howells }
326f6cbb368SDavid Howells 
327f6cbb368SDavid Howells /*
328f6cbb368SDavid Howells  * Dispatch a probe to a server.
329f6cbb368SDavid Howells  */
afs_dispatch_fs_probe(struct afs_net * net,struct afs_server * server,bool all)330f6cbb368SDavid Howells static void afs_dispatch_fs_probe(struct afs_net *net, struct afs_server *server, bool all)
331f6cbb368SDavid Howells 	__releases(&net->fs_lock)
332f6cbb368SDavid Howells {
333f6cbb368SDavid Howells 	struct key *key = NULL;
334f6cbb368SDavid Howells 
335f6cbb368SDavid Howells 	/* We remove it from the queues here - it will be added back to
336f6cbb368SDavid Howells 	 * one of the queues on the completion of the probe.
337f6cbb368SDavid Howells 	 */
338f6cbb368SDavid Howells 	list_del_init(&server->probe_link);
339f6cbb368SDavid Howells 
340f6cbb368SDavid Howells 	afs_get_server(server, afs_server_trace_get_probe);
341f6cbb368SDavid Howells 	write_sequnlock(&net->fs_lock);
342f6cbb368SDavid Howells 
343f6cbb368SDavid Howells 	afs_fs_probe_fileserver(net, server, key, all);
344f6cbb368SDavid Howells 	afs_put_server(net, server, afs_server_trace_put_probe);
345f6cbb368SDavid Howells }
346f6cbb368SDavid Howells 
347f6cbb368SDavid Howells /*
3488409f67bSDavid Howells  * Probe a server immediately without waiting for its due time to come
3498409f67bSDavid Howells  * round.  This is used when all of the addresses have been tried.
3508409f67bSDavid Howells  */
afs_probe_fileserver(struct afs_net * net,struct afs_server * server)3518409f67bSDavid Howells void afs_probe_fileserver(struct afs_net *net, struct afs_server *server)
3528409f67bSDavid Howells {
3538409f67bSDavid Howells 	write_seqlock(&net->fs_lock);
3548409f67bSDavid Howells 	if (!list_empty(&server->probe_link))
3558409f67bSDavid Howells 		return afs_dispatch_fs_probe(net, server, true);
3568409f67bSDavid Howells 	write_sequnlock(&net->fs_lock);
3578409f67bSDavid Howells }
3588409f67bSDavid Howells 
3598409f67bSDavid Howells /*
360f6cbb368SDavid Howells  * Probe dispatcher to regularly dispatch probes to keep NAT alive.
361f6cbb368SDavid Howells  */
afs_fs_probe_dispatcher(struct work_struct * work)362f6cbb368SDavid Howells void afs_fs_probe_dispatcher(struct work_struct *work)
363f6cbb368SDavid Howells {
364f6cbb368SDavid Howells 	struct afs_net *net = container_of(work, struct afs_net, fs_prober);
365f6cbb368SDavid Howells 	struct afs_server *fast, *slow, *server;
366f6cbb368SDavid Howells 	unsigned long nowj, timer_at, poll_at;
367f6cbb368SDavid Howells 	bool first_pass = true, set_timer = false;
368f6cbb368SDavid Howells 
369*36f82c93SDavid Howells 	if (!net->live) {
370*36f82c93SDavid Howells 		afs_dec_servers_outstanding(net);
371f6cbb368SDavid Howells 		return;
372*36f82c93SDavid Howells 	}
373f6cbb368SDavid Howells 
374f6cbb368SDavid Howells 	_enter("");
375f6cbb368SDavid Howells 
376f6cbb368SDavid Howells 	if (list_empty(&net->fs_probe_fast) && list_empty(&net->fs_probe_slow)) {
377*36f82c93SDavid Howells 		afs_dec_servers_outstanding(net);
378f6cbb368SDavid Howells 		_leave(" [none]");
379f6cbb368SDavid Howells 		return;
380f6cbb368SDavid Howells 	}
381f6cbb368SDavid Howells 
382f6cbb368SDavid Howells again:
383f6cbb368SDavid Howells 	write_seqlock(&net->fs_lock);
384f6cbb368SDavid Howells 
385f6cbb368SDavid Howells 	fast = slow = server = NULL;
386f6cbb368SDavid Howells 	nowj = jiffies;
387f6cbb368SDavid Howells 	timer_at = nowj + MAX_JIFFY_OFFSET;
388f6cbb368SDavid Howells 
389f6cbb368SDavid Howells 	if (!list_empty(&net->fs_probe_fast)) {
390f6cbb368SDavid Howells 		fast = list_first_entry(&net->fs_probe_fast, struct afs_server, probe_link);
391f6cbb368SDavid Howells 		poll_at = fast->probed_at + afs_fs_probe_fast_poll_interval;
392f6cbb368SDavid Howells 		if (time_before(nowj, poll_at)) {
393f6cbb368SDavid Howells 			timer_at = poll_at;
394f6cbb368SDavid Howells 			set_timer = true;
395f6cbb368SDavid Howells 			fast = NULL;
396f6cbb368SDavid Howells 		}
397f6cbb368SDavid Howells 	}
398f6cbb368SDavid Howells 
399f6cbb368SDavid Howells 	if (!list_empty(&net->fs_probe_slow)) {
400f6cbb368SDavid Howells 		slow = list_first_entry(&net->fs_probe_slow, struct afs_server, probe_link);
401f6cbb368SDavid Howells 		poll_at = slow->probed_at + afs_fs_probe_slow_poll_interval;
402f6cbb368SDavid Howells 		if (time_before(nowj, poll_at)) {
403f6cbb368SDavid Howells 			if (time_before(poll_at, timer_at))
404f6cbb368SDavid Howells 			    timer_at = poll_at;
405f6cbb368SDavid Howells 			set_timer = true;
406f6cbb368SDavid Howells 			slow = NULL;
407f6cbb368SDavid Howells 		}
408f6cbb368SDavid Howells 	}
409f6cbb368SDavid Howells 
410f6cbb368SDavid Howells 	server = fast ?: slow;
411f6cbb368SDavid Howells 	if (server)
412f6cbb368SDavid Howells 		_debug("probe %pU", &server->uuid);
413f6cbb368SDavid Howells 
414f6cbb368SDavid Howells 	if (server && (first_pass || !need_resched())) {
415f6cbb368SDavid Howells 		afs_dispatch_fs_probe(net, server, server == fast);
416f6cbb368SDavid Howells 		first_pass = false;
417f6cbb368SDavid Howells 		goto again;
418f6cbb368SDavid Howells 	}
419f6cbb368SDavid Howells 
420f6cbb368SDavid Howells 	write_sequnlock(&net->fs_lock);
421f6cbb368SDavid Howells 
422f6cbb368SDavid Howells 	if (server) {
423f6cbb368SDavid Howells 		if (!queue_work(afs_wq, &net->fs_prober))
424f6cbb368SDavid Howells 			afs_dec_servers_outstanding(net);
425f6cbb368SDavid Howells 		_leave(" [requeue]");
426f6cbb368SDavid Howells 	} else if (set_timer) {
427f6cbb368SDavid Howells 		if (timer_reduce(&net->fs_probe_timer, timer_at))
428f6cbb368SDavid Howells 			afs_dec_servers_outstanding(net);
429f6cbb368SDavid Howells 		_leave(" [timer]");
430f6cbb368SDavid Howells 	} else {
431f6cbb368SDavid Howells 		afs_dec_servers_outstanding(net);
432f6cbb368SDavid Howells 		_leave(" [quiesce]");
433f6cbb368SDavid Howells 	}
434f6cbb368SDavid Howells }
4358409f67bSDavid Howells 
4368409f67bSDavid Howells /*
4378409f67bSDavid Howells  * Wait for a probe on a particular fileserver to complete for 2s.
4388409f67bSDavid Howells  */
afs_wait_for_one_fs_probe(struct afs_server * server,bool is_intr)4398409f67bSDavid Howells int afs_wait_for_one_fs_probe(struct afs_server *server, bool is_intr)
4408409f67bSDavid Howells {
4418409f67bSDavid Howells 	struct wait_queue_entry wait;
4428409f67bSDavid Howells 	unsigned long timo = 2 * HZ;
4438409f67bSDavid Howells 
4448409f67bSDavid Howells 	if (atomic_read(&server->probe_outstanding) == 0)
4458409f67bSDavid Howells 		goto dont_wait;
4468409f67bSDavid Howells 
4478409f67bSDavid Howells 	init_wait_entry(&wait, 0);
4488409f67bSDavid Howells 	for (;;) {
4498409f67bSDavid Howells 		prepare_to_wait_event(&server->probe_wq, &wait,
4508409f67bSDavid Howells 				      is_intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
4518409f67bSDavid Howells 		if (timo == 0 ||
4528409f67bSDavid Howells 		    server->probe.responded ||
4538409f67bSDavid Howells 		    atomic_read(&server->probe_outstanding) == 0 ||
4548409f67bSDavid Howells 		    (is_intr && signal_pending(current)))
4558409f67bSDavid Howells 			break;
4568409f67bSDavid Howells 		timo = schedule_timeout(timo);
4578409f67bSDavid Howells 	}
4588409f67bSDavid Howells 
4598409f67bSDavid Howells 	finish_wait(&server->probe_wq, &wait);
4608409f67bSDavid Howells 
4618409f67bSDavid Howells dont_wait:
4628409f67bSDavid Howells 	if (server->probe.responded)
4638409f67bSDavid Howells 		return 0;
4648409f67bSDavid Howells 	if (is_intr && signal_pending(current))
4658409f67bSDavid Howells 		return -ERESTARTSYS;
4668409f67bSDavid Howells 	if (timo == 0)
4678409f67bSDavid Howells 		return -ETIME;
4688409f67bSDavid Howells 	return -EDESTADDRREQ;
4698409f67bSDavid Howells }
4705481fc6eSDavid Howells 
4715481fc6eSDavid Howells /*
4725481fc6eSDavid Howells  * Clean up the probing when the namespace is killed off.
4735481fc6eSDavid Howells  */
afs_fs_probe_cleanup(struct afs_net * net)4745481fc6eSDavid Howells void afs_fs_probe_cleanup(struct afs_net *net)
4755481fc6eSDavid Howells {
4765481fc6eSDavid Howells 	if (del_timer_sync(&net->fs_probe_timer))
4775481fc6eSDavid Howells 		afs_dec_servers_outstanding(net);
4785481fc6eSDavid Howells }
479