Linux Audio

Check our new training course

Loading...
v4.10.11
  1/* AFS server record management
  2 *
  3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
  4 * Written by David Howells (dhowells@redhat.com)
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version
  9 * 2 of the License, or (at your option) any later version.
 10 */
 11
 12#include <linux/sched.h>
 13#include <linux/slab.h>
 
 14#include "internal.h"
 15
 16static unsigned afs_server_timeout = 10;	/* server timeout in seconds */
 
 17
 18static void afs_reap_server(struct work_struct *);
 
 
 
 
 
 
 
 
 
 19
 20/* tree of all the servers, indexed by IP address */
 21static struct rb_root afs_servers = RB_ROOT;
 22static DEFINE_RWLOCK(afs_servers_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 23
 24/* LRU list of all the servers not currently in use */
 25static LIST_HEAD(afs_server_graveyard);
 26static DEFINE_SPINLOCK(afs_server_graveyard_lock);
 27static DECLARE_DELAYED_WORK(afs_server_reaper, afs_reap_server);
 
 
 
 
 
 
 
 
 28
 29/*
 30 * install a server record in the master tree
 31 */
 32static int afs_install_server(struct afs_server *server)
 33{
 34	struct afs_server *xserver;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 35	struct rb_node **pp, *p;
 36	int ret;
 37
 38	_enter("%p", server);
 39
 40	write_lock(&afs_servers_lock);
 41
 42	ret = -EEXIST;
 43	pp = &afs_servers.rb_node;
 44	p = NULL;
 45	while (*pp) {
 46		p = *pp;
 47		_debug("- consider %p", p);
 48		xserver = rb_entry(p, struct afs_server, master_rb);
 49		if (server->addr.s_addr < xserver->addr.s_addr)
 
 50			pp = &(*pp)->rb_left;
 51		else if (server->addr.s_addr > xserver->addr.s_addr)
 52			pp = &(*pp)->rb_right;
 53		else
 54			goto error;
 55	}
 56
 57	rb_link_node(&server->master_rb, p, pp);
 58	rb_insert_color(&server->master_rb, &afs_servers);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 59	ret = 0;
 60
 61error:
 62	write_unlock(&afs_servers_lock);
 63	return ret;
 
 64}
 65
 66/*
 67 * allocate a new server record
 68 */
 69static struct afs_server *afs_alloc_server(struct afs_cell *cell,
 70					   const struct in_addr *addr)
 
 71{
 72	struct afs_server *server;
 73
 74	_enter("");
 75
 76	server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
 77	if (server) {
 78		atomic_set(&server->usage, 1);
 79		server->cell = cell;
 80
 81		INIT_LIST_HEAD(&server->link);
 82		INIT_LIST_HEAD(&server->grave);
 83		init_rwsem(&server->sem);
 84		spin_lock_init(&server->fs_lock);
 85		server->fs_vnodes = RB_ROOT;
 86		server->cb_promises = RB_ROOT;
 87		spin_lock_init(&server->cb_lock);
 88		init_waitqueue_head(&server->cb_break_waitq);
 89		INIT_DELAYED_WORK(&server->cb_break_work,
 90				  afs_dispatch_give_up_callbacks);
 91
 92		memcpy(&server->addr, addr, sizeof(struct in_addr));
 93		server->addr.s_addr = addr->s_addr;
 94		_leave(" = %p{%d}", server, atomic_read(&server->usage));
 95	} else {
 96		_leave(" = NULL [nomem]");
 97	}
 98	return server;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 99}
100
101/*
102 * get an FS-server record for a cell
103 */
104struct afs_server *afs_lookup_server(struct afs_cell *cell,
105				     const struct in_addr *addr)
106{
 
107	struct afs_server *server, *candidate;
108
109	_enter("%p,%pI4", cell, &addr->s_addr);
110
111	/* quick scan of the list to see if we already have the server */
112	read_lock(&cell->servers_lock);
 
113
114	list_for_each_entry(server, &cell->servers, link) {
115		if (server->addr.s_addr == addr->s_addr)
116			goto found_server_quickly;
117	}
118	read_unlock(&cell->servers_lock);
119
120	candidate = afs_alloc_server(cell, addr);
121	if (!candidate) {
122		_leave(" = -ENOMEM");
123		return ERR_PTR(-ENOMEM);
124	}
125
126	write_lock(&cell->servers_lock);
127
128	/* check the cell's server list again */
129	list_for_each_entry(server, &cell->servers, link) {
130		if (server->addr.s_addr == addr->s_addr)
131			goto found_server;
132	}
133
134	_debug("new");
135	server = candidate;
136	if (afs_install_server(server) < 0)
137		goto server_in_two_cells;
138
139	afs_get_cell(cell);
140	list_add_tail(&server->link, &cell->servers);
141
142	write_unlock(&cell->servers_lock);
143	_leave(" = %p{%d}", server, atomic_read(&server->usage));
144	return server;
 
145
146	/* found a matching server quickly */
147found_server_quickly:
148	_debug("found quickly");
149	afs_get_server(server);
150	read_unlock(&cell->servers_lock);
151no_longer_unused:
152	if (!list_empty(&server->grave)) {
153		spin_lock(&afs_server_graveyard_lock);
154		list_del_init(&server->grave);
155		spin_unlock(&afs_server_graveyard_lock);
156	}
157	_leave(" = %p{%d}", server, atomic_read(&server->usage));
158	return server;
159
160	/* found a matching server on the second pass */
161found_server:
162	_debug("found");
163	afs_get_server(server);
164	write_unlock(&cell->servers_lock);
165	kfree(candidate);
166	goto no_longer_unused;
167
168	/* found a server that seems to be in two cells */
169server_in_two_cells:
170	write_unlock(&cell->servers_lock);
171	kfree(candidate);
172	printk(KERN_NOTICE "kAFS: Server %pI4 appears to be in two cells\n",
173	       addr);
174	_leave(" = -EEXIST");
175	return ERR_PTR(-EEXIST);
176}
177
178/*
179 * look up a server by its IP address
180 */
181struct afs_server *afs_find_server(const struct sockaddr_rxrpc *srx)
182{
183	struct afs_server *server = NULL;
184	struct rb_node *p;
185	struct in_addr addr = srx->transport.sin.sin_addr;
186
187	_enter("{%d,%pI4}", srx->transport.family, &addr.s_addr);
 
188
189	if (srx->transport.family != AF_INET) {
190		WARN(true, "AFS does not yes support non-IPv4 addresses\n");
191		return NULL;
192	}
193
194	read_lock(&afs_servers_lock);
195
196	p = afs_servers.rb_node;
197	while (p) {
198		server = rb_entry(p, struct afs_server, master_rb);
199
200		_debug("- consider %p", p);
 
201
202		if (addr.s_addr < server->addr.s_addr) {
203			p = p->rb_left;
204		} else if (addr.s_addr > server->addr.s_addr) {
205			p = p->rb_right;
206		} else {
207			afs_get_server(server);
208			goto found;
209		}
210	}
211
212	server = NULL;
213found:
214	read_unlock(&afs_servers_lock);
215	ASSERTIFCMP(server, server->addr.s_addr, ==, addr.s_addr);
216	_leave(" = %p", server);
217	return server;
218}
219
220/*
221 * destroy a server record
222 * - removes from the cell list
223 */
224void afs_put_server(struct afs_server *server)
225{
226	if (!server)
227		return;
 
 
 
 
 
 
 
228
229	_enter("%p{%d}", server, atomic_read(&server->usage));
 
230
231	_debug("PUT SERVER %d", atomic_read(&server->usage));
 
 
232
233	ASSERTCMP(atomic_read(&server->usage), >, 0);
 
 
 
 
 
 
 
234
235	if (likely(!atomic_dec_and_test(&server->usage))) {
236		_leave("");
237		return;
238	}
239
240	afs_flush_callback_breaks(server);
 
 
 
 
 
 
 
241
242	spin_lock(&afs_server_graveyard_lock);
243	if (atomic_read(&server->usage) == 0) {
244		list_move_tail(&server->grave, &afs_server_graveyard);
245		server->time_of_death = get_seconds();
246		queue_delayed_work(afs_wq, &afs_server_reaper,
247				   afs_server_timeout * HZ);
 
 
 
248	}
249	spin_unlock(&afs_server_graveyard_lock);
250	_leave(" [dead]");
251}
252
253/*
254 * destroy a dead server
 
 
 
 
255 */
256static void afs_destroy_server(struct afs_server *server)
257{
258	_enter("%p", server);
 
 
 
 
259
260	ASSERTIF(server->cb_break_head != server->cb_break_tail,
261		 delayed_work_pending(&server->cb_break_work));
262
263	ASSERTCMP(server->fs_vnodes.rb_node, ==, NULL);
264	ASSERTCMP(server->cb_promises.rb_node, ==, NULL);
265	ASSERTCMP(server->cb_break_head, ==, server->cb_break_tail);
266	ASSERTCMP(atomic_read(&server->cb_break_n), ==, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
267
268	afs_put_cell(server->cell);
269	kfree(server);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270}
271
272/*
273 * reap dead server records
274 */
275static void afs_reap_server(struct work_struct *work)
276{
277	LIST_HEAD(corpses);
278	struct afs_server *server;
279	unsigned long delay, expiry;
280	time_t now;
281
282	now = get_seconds();
283	spin_lock(&afs_server_graveyard_lock);
284
285	while (!list_empty(&afs_server_graveyard)) {
286		server = list_entry(afs_server_graveyard.next,
287				    struct afs_server, grave);
288
289		/* the queue is ordered most dead first */
290		expiry = server->time_of_death + afs_server_timeout;
291		if (expiry > now) {
292			delay = (expiry - now) * HZ;
293			mod_delayed_work(afs_wq, &afs_server_reaper, delay);
294			break;
295		}
296
297		write_lock(&server->cell->servers_lock);
298		write_lock(&afs_servers_lock);
299		if (atomic_read(&server->usage) > 0) {
300			list_del_init(&server->grave);
301		} else {
302			list_move_tail(&server->grave, &corpses);
303			list_del_init(&server->link);
304			rb_erase(&server->master_rb, &afs_servers);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305		}
306		write_unlock(&afs_servers_lock);
307		write_unlock(&server->cell->servers_lock);
308	}
309
310	spin_unlock(&afs_server_graveyard_lock);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
311
312	/* now reap the corpses we've extracted */
313	while (!list_empty(&corpses)) {
314		server = list_entry(corpses.next, struct afs_server, grave);
315		list_del(&server->grave);
316		afs_destroy_server(server);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318}
319
320/*
321 * discard all the server records for rmmod
322 */
323void __exit afs_purge_servers(void)
324{
325	afs_server_timeout = 0;
326	mod_delayed_work(afs_wq, &afs_server_reaper, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327}
v4.17
  1/* AFS server record management
  2 *
  3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
  4 * Written by David Howells (dhowells@redhat.com)
  5 *
  6 * This program is free software; you can redistribute it and/or
  7 * modify it under the terms of the GNU General Public License
  8 * as published by the Free Software Foundation; either version
  9 * 2 of the License, or (at your option) any later version.
 10 */
 11
 12#include <linux/sched.h>
 13#include <linux/slab.h>
 14#include "afs_fs.h"
 15#include "internal.h"
 16
 17static unsigned afs_server_gc_delay = 10;	/* Server record timeout in seconds */
 18static unsigned afs_server_update_delay = 30;	/* Time till VLDB recheck in secs */
 19
 20static void afs_inc_servers_outstanding(struct afs_net *net)
 21{
 22	atomic_inc(&net->servers_outstanding);
 23}
 24
 25static void afs_dec_servers_outstanding(struct afs_net *net)
 26{
 27	if (atomic_dec_and_test(&net->servers_outstanding))
 28		wake_up_var(&net->servers_outstanding);
 29}
 30
 31/*
 32 * Find a server by one of its addresses.
 33 */
 34struct afs_server *afs_find_server(struct afs_net *net,
 35				   const struct sockaddr_rxrpc *srx)
 36{
 37	const struct sockaddr_in6 *a = &srx->transport.sin6, *b;
 38	const struct afs_addr_list *alist;
 39	struct afs_server *server = NULL;
 40	unsigned int i;
 41	bool ipv6 = true;
 42	int seq = 0, diff;
 43
 44	if (srx->transport.sin6.sin6_addr.s6_addr32[0] == 0 ||
 45	    srx->transport.sin6.sin6_addr.s6_addr32[1] == 0 ||
 46	    srx->transport.sin6.sin6_addr.s6_addr32[2] == htonl(0xffff))
 47		ipv6 = false;
 48
 49	rcu_read_lock();
 50
 51	do {
 52		if (server)
 53			afs_put_server(net, server);
 54		server = NULL;
 55		read_seqbegin_or_lock(&net->fs_addr_lock, &seq);
 56
 57		if (ipv6) {
 58			hlist_for_each_entry_rcu(server, &net->fs_addresses6, addr6_link) {
 59				alist = rcu_dereference(server->addresses);
 60				for (i = alist->nr_ipv4; i < alist->nr_addrs; i++) {
 61					b = &alist->addrs[i].transport.sin6;
 62					diff = ((u16 __force)a->sin6_port -
 63						(u16 __force)b->sin6_port);
 64					if (diff == 0)
 65						diff = memcmp(&a->sin6_addr,
 66							      &b->sin6_addr,
 67							      sizeof(struct in6_addr));
 68					if (diff == 0)
 69						goto found;
 70				}
 71			}
 72		} else {
 73			hlist_for_each_entry_rcu(server, &net->fs_addresses4, addr4_link) {
 74				alist = rcu_dereference(server->addresses);
 75				for (i = 0; i < alist->nr_ipv4; i++) {
 76					b = &alist->addrs[i].transport.sin6;
 77					diff = ((u16 __force)a->sin6_port -
 78						(u16 __force)b->sin6_port);
 79					if (diff == 0)
 80						diff = ((u32 __force)a->sin6_addr.s6_addr32[3] -
 81							(u32 __force)b->sin6_addr.s6_addr32[3]);
 82					if (diff == 0)
 83						goto found;
 84				}
 85			}
 86		}
 87
 88		server = NULL;
 89	found:
 90		if (server && !atomic_inc_not_zero(&server->usage))
 91			server = NULL;
 92
 93	} while (need_seqretry(&net->fs_addr_lock, seq));
 94
 95	done_seqretry(&net->fs_addr_lock, seq);
 96
 97	rcu_read_unlock();
 98	return server;
 99}
100
101/*
102 * Look up a server by its UUID
103 */
104struct afs_server *afs_find_server_by_uuid(struct afs_net *net, const uuid_t *uuid)
105{
106	struct afs_server *server = NULL;
107	struct rb_node *p;
108	int diff, seq = 0;
109
110	_enter("%pU", uuid);
111
112	do {
113		/* Unfortunately, rbtree walking doesn't give reliable results
114		 * under just the RCU read lock, so we have to check for
115		 * changes.
116		 */
117		if (server)
118			afs_put_server(net, server);
119		server = NULL;
120
121		read_seqbegin_or_lock(&net->fs_lock, &seq);
122
123		p = net->fs_servers.rb_node;
124		while (p) {
125			server = rb_entry(p, struct afs_server, uuid_rb);
126
127			diff = memcmp(uuid, &server->uuid, sizeof(*uuid));
128			if (diff < 0) {
129				p = p->rb_left;
130			} else if (diff > 0) {
131				p = p->rb_right;
132			} else {
133				afs_get_server(server);
134				break;
135			}
136
137			server = NULL;
138		}
139	} while (need_seqretry(&net->fs_lock, seq));
140
141	done_seqretry(&net->fs_lock, seq);
142
143	_leave(" = %p", server);
144	return server;
145}
146
147/*
148 * Install a server record in the namespace tree
149 */
150static struct afs_server *afs_install_server(struct afs_net *net,
151					     struct afs_server *candidate)
152{
153	const struct afs_addr_list *alist;
154	struct afs_server *server;
155	struct rb_node **pp, *p;
156	int ret = -EEXIST, diff;
157
158	_enter("%p", candidate);
159
160	write_seqlock(&net->fs_lock);
161
162	/* Firstly install the server in the UUID lookup tree */
163	pp = &net->fs_servers.rb_node;
164	p = NULL;
165	while (*pp) {
166		p = *pp;
167		_debug("- consider %p", p);
168		server = rb_entry(p, struct afs_server, uuid_rb);
169		diff = memcmp(&candidate->uuid, &server->uuid, sizeof(uuid_t));
170		if (diff < 0)
171			pp = &(*pp)->rb_left;
172		else if (diff > 0)
173			pp = &(*pp)->rb_right;
174		else
175			goto exists;
176	}
177
178	server = candidate;
179	rb_link_node(&server->uuid_rb, p, pp);
180	rb_insert_color(&server->uuid_rb, &net->fs_servers);
181	hlist_add_head_rcu(&server->proc_link, &net->fs_proc);
182
183	write_seqlock(&net->fs_addr_lock);
184	alist = rcu_dereference_protected(server->addresses,
185					  lockdep_is_held(&net->fs_addr_lock.lock));
186
187	/* Secondly, if the server has any IPv4 and/or IPv6 addresses, install
188	 * it in the IPv4 and/or IPv6 reverse-map lists.
189	 *
190	 * TODO: For speed we want to use something other than a flat list
191	 * here; even sorting the list in terms of lowest address would help a
192	 * bit, but anything we might want to do gets messy and memory
193	 * intensive.
194	 */
195	if (alist->nr_ipv4 > 0)
196		hlist_add_head_rcu(&server->addr4_link, &net->fs_addresses4);
197	if (alist->nr_addrs > alist->nr_ipv4)
198		hlist_add_head_rcu(&server->addr6_link, &net->fs_addresses6);
199
200	write_sequnlock(&net->fs_addr_lock);
201	ret = 0;
202
203exists:
204	afs_get_server(server);
205	write_sequnlock(&net->fs_lock);
206	return server;
207}
208
209/*
210 * allocate a new server record
211 */
212static struct afs_server *afs_alloc_server(struct afs_net *net,
213					   const uuid_t *uuid,
214					   struct afs_addr_list *alist)
215{
216	struct afs_server *server;
217
218	_enter("");
219
220	server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
221	if (!server)
222		goto enomem;
223
224	atomic_set(&server->usage, 1);
225	RCU_INIT_POINTER(server->addresses, alist);
226	server->addr_version = alist->version;
227	server->uuid = *uuid;
228	server->flags = (1UL << AFS_SERVER_FL_NEW);
229	server->update_at = ktime_get_real_seconds() + afs_server_update_delay;
230	rwlock_init(&server->fs_lock);
231	INIT_LIST_HEAD(&server->cb_interests);
232	rwlock_init(&server->cb_break_lock);
233
234	afs_inc_servers_outstanding(net);
235	_leave(" = %p", server);
 
 
 
 
 
 
236	return server;
237
238enomem:
239	_leave(" = NULL [nomem]");
240	return NULL;
241}
242
243/*
244 * Look up an address record for a server
245 */
246static struct afs_addr_list *afs_vl_lookup_addrs(struct afs_cell *cell,
247						 struct key *key, const uuid_t *uuid)
248{
249	struct afs_addr_cursor ac;
250	struct afs_addr_list *alist;
251	int ret;
252
253	ret = afs_set_vl_cursor(&ac, cell);
254	if (ret < 0)
255		return ERR_PTR(ret);
256
257	while (afs_iterate_addresses(&ac)) {
258		if (test_bit(ac.index, &ac.alist->yfs))
259			alist = afs_yfsvl_get_endpoints(cell->net, &ac, key, uuid);
260		else
261			alist = afs_vl_get_addrs_u(cell->net, &ac, key, uuid);
262		switch (ac.error) {
263		case 0:
264			afs_end_cursor(&ac);
265			return alist;
266		case -ECONNABORTED:
267			ac.error = afs_abort_to_error(ac.abort_code);
268			goto error;
269		case -ENOMEM:
270		case -ENONET:
271			goto error;
272		case -ENETUNREACH:
273		case -EHOSTUNREACH:
274		case -ECONNREFUSED:
275			break;
276		default:
277			ac.error = -EIO;
278			goto error;
279		}
280	}
281
282error:
283	return ERR_PTR(afs_end_cursor(&ac));
284}
285
286/*
287 * Get or create a fileserver record.
288 */
289struct afs_server *afs_lookup_server(struct afs_cell *cell, struct key *key,
290				     const uuid_t *uuid)
291{
292	struct afs_addr_list *alist;
293	struct afs_server *server, *candidate;
294
295	_enter("%p,%pU", cell->net, uuid);
296
297	server = afs_find_server_by_uuid(cell->net, uuid);
298	if (server)
299		return server;
300
301	alist = afs_vl_lookup_addrs(cell, key, uuid);
302	if (IS_ERR(alist))
303		return ERR_CAST(alist);
 
 
304
305	candidate = afs_alloc_server(cell->net, uuid, alist);
306	if (!candidate) {
307		afs_put_addrlist(alist);
308		return ERR_PTR(-ENOMEM);
309	}
310
311	server = afs_install_server(cell->net, candidate);
312	if (server != candidate) {
313		afs_put_addrlist(alist);
314		kfree(candidate);
 
 
315	}
316
 
 
 
 
 
 
 
 
 
317	_leave(" = %p{%d}", server, atomic_read(&server->usage));
318	return server;
319}
320
321/*
322 * Set the server timer to fire after a given delay, assuming it's not already
323 * set for an earlier time.
324 */
325static void afs_set_server_timer(struct afs_net *net, time64_t delay)
326{
327	if (net->live) {
328		afs_inc_servers_outstanding(net);
329		if (timer_reduce(&net->fs_timer, jiffies + delay * HZ))
330			afs_dec_servers_outstanding(net);
331	}
332}
 
333
334/*
335 * Server management timer.  We have an increment on fs_outstanding that we
336 * need to pass along to the work item.
337 */
338void afs_servers_timer(struct timer_list *timer)
339{
340	struct afs_net *net = container_of(timer, struct afs_net, fs_timer);
341
342	_enter("");
343	if (!queue_work(afs_wq, &net->fs_manager))
344		afs_dec_servers_outstanding(net);
 
 
 
 
 
345}
346
347/*
348 * Release a reference on a server record.
349 */
350void afs_put_server(struct afs_net *net, struct afs_server *server)
351{
352	unsigned int usage;
 
 
353
354	if (!server)
355		return;
356
357	server->put_time = ktime_get_real_seconds();
 
 
 
358
359	usage = atomic_dec_return(&server->usage);
360
361	_enter("{%u}", usage);
 
 
362
363	if (likely(usage > 0))
364		return;
365
366	afs_set_server_timer(net, afs_server_gc_delay);
367}
 
 
 
 
 
 
 
368
369static void afs_server_rcu(struct rcu_head *rcu)
370{
371	struct afs_server *server = container_of(rcu, struct afs_server, rcu);
372
373	afs_put_addrlist(rcu_access_pointer(server->addresses));
374	kfree(server);
375}
376
377/*
378 * destroy a dead server
 
379 */
380static void afs_destroy_server(struct afs_net *net, struct afs_server *server)
381{
382	struct afs_addr_list *alist = rcu_access_pointer(server->addresses);
383	struct afs_addr_cursor ac = {
384		.alist	= alist,
385		.start	= alist->index,
386		.index	= 0,
387		.addr	= &alist->addrs[alist->index],
388		.error	= 0,
389	};
390	_enter("%p", server);
391
392	if (test_bit(AFS_SERVER_FL_MAY_HAVE_CB, &server->flags))
393		afs_fs_give_up_all_callbacks(net, server, &ac, NULL);
394
395	call_rcu(&server->rcu, afs_server_rcu);
396	afs_dec_servers_outstanding(net);
397}
398
399/*
400 * Garbage collect any expired servers.
401 */
402static void afs_gc_servers(struct afs_net *net, struct afs_server *gc_list)
403{
404	struct afs_server *server;
405	bool deleted;
406	int usage;
407
408	while ((server = gc_list)) {
409		gc_list = server->gc_next;
 
 
410
411		write_seqlock(&net->fs_lock);
412		usage = 1;
413		deleted = atomic_try_cmpxchg(&server->usage, &usage, 0);
414		if (deleted) {
415			rb_erase(&server->uuid_rb, &net->fs_servers);
416			hlist_del_rcu(&server->proc_link);
417		}
418		write_sequnlock(&net->fs_lock);
419
420		if (deleted) {
421			write_seqlock(&net->fs_addr_lock);
422			if (!hlist_unhashed(&server->addr4_link))
423				hlist_del_rcu(&server->addr4_link);
424			if (!hlist_unhashed(&server->addr6_link))
425				hlist_del_rcu(&server->addr6_link);
426			write_sequnlock(&net->fs_addr_lock);
427			afs_destroy_server(net, server);
428		}
429	}
 
 
430}
431
432/*
433 * Manage the records of servers known to be within a network namespace.  This
434 * includes garbage collecting unused servers.
435 *
436 * Note also that we were given an increment on net->servers_outstanding by
437 * whoever queued us that we need to deal with before returning.
438 */
439void afs_manage_servers(struct work_struct *work)
440{
441	struct afs_net *net = container_of(work, struct afs_net, fs_manager);
442	struct afs_server *gc_list = NULL;
443	struct rb_node *cursor;
444	time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
445	bool purging = !net->live;
446
447	_enter("");
 
448
449	/* Trawl the server list looking for servers that have expired from
450	 * lack of use.
451	 */
452	read_seqlock_excl(&net->fs_lock);
453
454	for (cursor = rb_first(&net->fs_servers); cursor; cursor = rb_next(cursor)) {
455		struct afs_server *server =
456			rb_entry(cursor, struct afs_server, uuid_rb);
457		int usage = atomic_read(&server->usage);
458
459		_debug("manage %pU %u", &server->uuid, usage);
460
461		ASSERTCMP(usage, >=, 1);
462		ASSERTIFCMP(purging, usage, ==, 1);
463
464		if (usage == 1) {
465			time64_t expire_at = server->put_time;
466
467			if (!test_bit(AFS_SERVER_FL_VL_FAIL, &server->flags) &&
468			    !test_bit(AFS_SERVER_FL_NOT_FOUND, &server->flags))
469				expire_at += afs_server_gc_delay;
470			if (purging || expire_at <= now) {
471				server->gc_next = gc_list;
472				gc_list = server;
473			} else if (expire_at < next_manage) {
474				next_manage = expire_at;
475			}
476		}
477	}
478
479	read_sequnlock_excl(&net->fs_lock);
480
481	/* Update the timer on the way out.  We have to pass an increment on
482	 * servers_outstanding in the namespace that we are in to the timer or
483	 * the work scheduler.
484	 */
485	if (!purging && next_manage < TIME64_MAX) {
486		now = ktime_get_real_seconds();
487
488		if (next_manage - now <= 0) {
489			if (queue_work(afs_wq, &net->fs_manager))
490				afs_inc_servers_outstanding(net);
491		} else {
492			afs_set_server_timer(net, next_manage - now);
493		}
494	}
495
496	afs_gc_servers(net, gc_list);
497
498	afs_dec_servers_outstanding(net);
499	_leave(" [%d]", atomic_read(&net->servers_outstanding));
500}
501
502static void afs_queue_server_manager(struct afs_net *net)
503{
504	afs_inc_servers_outstanding(net);
505	if (!queue_work(afs_wq, &net->fs_manager))
506		afs_dec_servers_outstanding(net);
507}
508
509/*
510 * Purge list of servers.
511 */
512void afs_purge_servers(struct afs_net *net)
513{
514	_enter("");
 
 
 
515
516	if (del_timer_sync(&net->fs_timer))
517		atomic_dec(&net->servers_outstanding);
518
519	afs_queue_server_manager(net);
 
 
 
 
 
 
 
 
 
 
520
521	_debug("wait");
522	wait_var_event(&net->servers_outstanding,
523		       !atomic_read(&net->servers_outstanding));
524	_leave("");
525}
526
527/*
528 * Probe a fileserver to find its capabilities.
529 *
530 * TODO: Try service upgrade.
531 */
532static bool afs_do_probe_fileserver(struct afs_fs_cursor *fc)
533{
534	_enter("");
535
536	fc->ac.addr = NULL;
537	fc->ac.start = READ_ONCE(fc->ac.alist->index);
538	fc->ac.index = fc->ac.start;
539	fc->ac.error = 0;
540	fc->ac.begun = false;
541
542	while (afs_iterate_addresses(&fc->ac)) {
543		afs_fs_get_capabilities(afs_v2net(fc->vnode), fc->cbi->server,
544					&fc->ac, fc->key);
545		switch (fc->ac.error) {
546		case 0:
547			afs_end_cursor(&fc->ac);
548			set_bit(AFS_SERVER_FL_PROBED, &fc->cbi->server->flags);
549			return true;
550		case -ECONNABORTED:
551			fc->ac.error = afs_abort_to_error(fc->ac.abort_code);
552			goto error;
553		case -ENOMEM:
554		case -ENONET:
555			goto error;
556		case -ENETUNREACH:
557		case -EHOSTUNREACH:
558		case -ECONNREFUSED:
559		case -ETIMEDOUT:
560		case -ETIME:
561			break;
562		default:
563			fc->ac.error = -EIO;
564			goto error;
565		}
 
 
566	}
567
568error:
569	afs_end_cursor(&fc->ac);
570	return false;
571}
572
573/*
574 * If we haven't already, try probing the fileserver to get its capabilities.
575 * We try not to instigate parallel probes, but it's possible that the parallel
576 * probes will fail due to authentication failure when ours would succeed.
577 *
578 * TODO: Try sending an anonymous probe if an authenticated probe fails.
579 */
580bool afs_probe_fileserver(struct afs_fs_cursor *fc)
581{
582	bool success;
583	int ret, retries = 0;
584
585	_enter("");
586
587retry:
588	if (test_bit(AFS_SERVER_FL_PROBED, &fc->cbi->server->flags)) {
589		_leave(" = t");
590		return true;
591	}
592
593	if (!test_and_set_bit_lock(AFS_SERVER_FL_PROBING, &fc->cbi->server->flags)) {
594		success = afs_do_probe_fileserver(fc);
595		clear_bit_unlock(AFS_SERVER_FL_PROBING, &fc->cbi->server->flags);
596		wake_up_bit(&fc->cbi->server->flags, AFS_SERVER_FL_PROBING);
597		_leave(" = t");
598		return success;
599	}
600
601	_debug("wait");
602	ret = wait_on_bit(&fc->cbi->server->flags, AFS_SERVER_FL_PROBING,
603			  TASK_INTERRUPTIBLE);
604	if (ret == -ERESTARTSYS) {
605		fc->ac.error = ret;
606		_leave(" = f [%d]", ret);
607		return false;
608	}
609
610	retries++;
611	if (retries == 4) {
612		fc->ac.error = -ESTALE;
613		_leave(" = f [stale]");
614		return false;
615	}
616	_debug("retry");
617	goto retry;
618}
619
620/*
621 * Get an update for a server's address list.
622 */
623static noinline bool afs_update_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
624{
625	struct afs_addr_list *alist, *discard;
626
627	_enter("");
628
629	alist = afs_vl_lookup_addrs(fc->vnode->volume->cell, fc->key,
630				    &server->uuid);
631	if (IS_ERR(alist)) {
632		fc->ac.error = PTR_ERR(alist);
633		_leave(" = f [%d]", fc->ac.error);
634		return false;
635	}
636
637	discard = alist;
638	if (server->addr_version != alist->version) {
639		write_lock(&server->fs_lock);
640		discard = rcu_dereference_protected(server->addresses,
641						    lockdep_is_held(&server->fs_lock));
642		rcu_assign_pointer(server->addresses, alist);
643		server->addr_version = alist->version;
644		write_unlock(&server->fs_lock);
645	}
646
647	server->update_at = ktime_get_real_seconds() + afs_server_update_delay;
648	afs_put_addrlist(discard);
649	_leave(" = t");
650	return true;
651}
652
653/*
654 * See if a server's address list needs updating.
655 */
656bool afs_check_server_record(struct afs_fs_cursor *fc, struct afs_server *server)
657{
658	time64_t now = ktime_get_real_seconds();
659	long diff;
660	bool success;
661	int ret, retries = 0;
662
663	_enter("");
664
665	ASSERT(server);
666
667retry:
668	diff = READ_ONCE(server->update_at) - now;
669	if (diff > 0) {
670		_leave(" = t [not now %ld]", diff);
671		return true;
672	}
673
674	if (!test_and_set_bit_lock(AFS_SERVER_FL_UPDATING, &server->flags)) {
675		success = afs_update_server_record(fc, server);
676		clear_bit_unlock(AFS_SERVER_FL_UPDATING, &server->flags);
677		wake_up_bit(&server->flags, AFS_SERVER_FL_UPDATING);
678		_leave(" = %d", success);
679		return success;
680	}
681
682	ret = wait_on_bit(&server->flags, AFS_SERVER_FL_UPDATING,
683			  TASK_INTERRUPTIBLE);
684	if (ret == -ERESTARTSYS) {
685		fc->ac.error = ret;
686		_leave(" = f [intr]");
687		return false;
688	}
689
690	retries++;
691	if (retries == 4) {
692		_leave(" = f [stale]");
693		ret = -ESTALE;
694		return false;
695	}
696	goto retry;
697}