1/* AFS cell and server record management
2 *
3 * Copyright (C) 2002 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/module.h>
13#include <linux/slab.h>
14#include <linux/key.h>
15#include <linux/ctype.h>
16#include <linux/dns_resolver.h>
17#include <linux/sched.h>
18#include <keys/rxrpc-type.h>
19#include "internal.h"
20
21DECLARE_RWSEM(afs_proc_cells_sem);
22LIST_HEAD(afs_proc_cells);
23
24static LIST_HEAD(afs_cells);
25static DEFINE_RWLOCK(afs_cells_lock);
26static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
27static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
28static struct afs_cell *afs_cell_root;
29
30/*
31 * allocate a cell record and fill in its name, VL server address list and
32 * allocate an anonymous key
33 */
34static struct afs_cell *afs_cell_alloc(const char *name, unsigned namelen,
35				       char *vllist)
36{
37	struct afs_cell *cell;
38	struct key *key;
39	char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
40	char  *dvllist = NULL, *_vllist = NULL;
41	char  delimiter = ':';
42	int ret;
43
44	_enter("%*.*s,%s", namelen, namelen, name ?: "", vllist);
45
46	BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
47
48	if (namelen > AFS_MAXCELLNAME) {
49		_leave(" = -ENAMETOOLONG");
50		return ERR_PTR(-ENAMETOOLONG);
51	}
52
53	/* allocate and initialise a cell record */
54	cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
55	if (!cell) {
56		_leave(" = -ENOMEM");
57		return ERR_PTR(-ENOMEM);
58	}
59
60	memcpy(cell->name, name, namelen);
61	cell->name[namelen] = 0;
62
63	atomic_set(&cell->usage, 1);
64	INIT_LIST_HEAD(&cell->link);
65	rwlock_init(&cell->servers_lock);
66	INIT_LIST_HEAD(&cell->servers);
67	init_rwsem(&cell->vl_sem);
68	INIT_LIST_HEAD(&cell->vl_list);
69	spin_lock_init(&cell->vl_lock);
70
71	/* if the ip address is invalid, try dns query */
72	if (!vllist || strlen(vllist) < 7) {
73		ret = dns_query("afsdb", name, namelen, "ipv4", &dvllist, NULL);
74		if (ret < 0) {
75			if (ret == -ENODATA || ret == -EAGAIN || ret == -ENOKEY)
76				/* translate these errors into something
77				 * userspace might understand */
78				ret = -EDESTADDRREQ;
79			_leave(" = %d", ret);
80			return ERR_PTR(ret);
81		}
82		_vllist = dvllist;
83
84		/* change the delimiter for user-space reply */
85		delimiter = ',';
86
87	} else {
88		_vllist = vllist;
89	}
90
91	/* fill in the VL server list from the rest of the string */
92	do {
93		unsigned a, b, c, d;
94
95		next = strchr(_vllist, delimiter);
96		if (next)
97			*next++ = 0;
98
99		if (sscanf(_vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
100			goto bad_address;
101
102		if (a > 255 || b > 255 || c > 255 || d > 255)
103			goto bad_address;
104
105		cell->vl_addrs[cell->vl_naddrs++].s_addr =
106			htonl((a << 24) | (b << 16) | (c << 8) | d);
107
108	} while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (_vllist = next));
109
110	/* create a key to represent an anonymous user */
111	memcpy(keyname, "afs@", 4);
112	dp = keyname + 4;
113	cp = cell->name;
114	do {
115		*dp++ = toupper(*cp);
116	} while (*cp++);
117
118	key = rxrpc_get_null_key(keyname);
119	if (IS_ERR(key)) {
120		_debug("no key");
121		ret = PTR_ERR(key);
122		goto error;
123	}
124	cell->anonymous_key = key;
125
126	_debug("anon key %p{%x}",
127	       cell->anonymous_key, key_serial(cell->anonymous_key));
128
129	_leave(" = %p", cell);
130	return cell;
131
132bad_address:
133	printk(KERN_ERR "kAFS: bad VL server IP address\n");
134	ret = -EINVAL;
135error:
136	key_put(cell->anonymous_key);
137	kfree(dvllist);
138	kfree(cell);
139	_leave(" = %d", ret);
140	return ERR_PTR(ret);
141}
142
143/*
144 * afs_cell_crate() - create a cell record
145 * @name:	is the name of the cell.
146 * @namsesz:	is the strlen of the cell name.
147 * @vllist:	is a colon separated list of IP addresses in "a.b.c.d" format.
148 * @retref:	is T to return the cell reference when the cell exists.
149 */
150struct afs_cell *afs_cell_create(const char *name, unsigned namesz,
151				 char *vllist, bool retref)
152{
153	struct afs_cell *cell;
154	int ret;
155
156	_enter("%*.*s,%s", namesz, namesz, name ?: "", vllist);
157
158	down_write(&afs_cells_sem);
159	read_lock(&afs_cells_lock);
160	list_for_each_entry(cell, &afs_cells, link) {
161		if (strncasecmp(cell->name, name, namesz) == 0)
162			goto duplicate_name;
163	}
164	read_unlock(&afs_cells_lock);
165
166	cell = afs_cell_alloc(name, namesz, vllist);
167	if (IS_ERR(cell)) {
168		_leave(" = %ld", PTR_ERR(cell));
169		up_write(&afs_cells_sem);
170		return cell;
171	}
172
173	/* add a proc directory for this cell */
174	ret = afs_proc_cell_setup(cell);
175	if (ret < 0)
176		goto error;
177
178#ifdef CONFIG_AFS_FSCACHE
179	/* put it up for caching (this never returns an error) */
180	cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
181					     &afs_cell_cache_index_def,
182					     cell);
183#endif
184
185	/* add to the cell lists */
186	write_lock(&afs_cells_lock);
187	list_add_tail(&cell->link, &afs_cells);
188	write_unlock(&afs_cells_lock);
189
190	down_write(&afs_proc_cells_sem);
191	list_add_tail(&cell->proc_link, &afs_proc_cells);
192	up_write(&afs_proc_cells_sem);
193	up_write(&afs_cells_sem);
194
195	_leave(" = %p", cell);
196	return cell;
197
198error:
199	up_write(&afs_cells_sem);
200	key_put(cell->anonymous_key);
201	kfree(cell);
202	_leave(" = %d", ret);
203	return ERR_PTR(ret);
204
205duplicate_name:
206	if (retref && !IS_ERR(cell))
207		afs_get_cell(cell);
208
209	read_unlock(&afs_cells_lock);
210	up_write(&afs_cells_sem);
211
212	if (retref) {
213		_leave(" = %p", cell);
214		return cell;
215	}
216
217	_leave(" = -EEXIST");
218	return ERR_PTR(-EEXIST);
219}
220
221/*
222 * set the root cell information
223 * - can be called with a module parameter string
224 * - can be called from a write to /proc/fs/afs/rootcell
225 */
226int afs_cell_init(char *rootcell)
227{
228	struct afs_cell *old_root, *new_root;
229	char *cp;
230
231	_enter("");
232
233	if (!rootcell) {
234		/* module is loaded with no parameters, or built statically.
235		 * - in the future we might initialize cell DB here.
236		 */
237		_leave(" = 0 [no root]");
238		return 0;
239	}
240
241	cp = strchr(rootcell, ':');
242	if (!cp)
243		_debug("kAFS: no VL server IP addresses specified");
244	else
245		*cp++ = 0;
246
247	/* allocate a cell record for the root cell */
248	new_root = afs_cell_create(rootcell, strlen(rootcell), cp, false);
249	if (IS_ERR(new_root)) {
250		_leave(" = %ld", PTR_ERR(new_root));
251		return PTR_ERR(new_root);
252	}
253
254	/* install the new cell */
255	write_lock(&afs_cells_lock);
256	old_root = afs_cell_root;
257	afs_cell_root = new_root;
258	write_unlock(&afs_cells_lock);
259	afs_put_cell(old_root);
260
261	_leave(" = 0");
262	return 0;
263}
264
265/*
266 * lookup a cell record
267 */
268struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz,
269				 bool dns_cell)
270{
271	struct afs_cell *cell;
272
273	_enter("\"%*.*s\",", namesz, namesz, name ?: "");
274
275	down_read(&afs_cells_sem);
276	read_lock(&afs_cells_lock);
277
278	if (name) {
279		/* if the cell was named, look for it in the cell record list */
280		list_for_each_entry(cell, &afs_cells, link) {
281			if (strncmp(cell->name, name, namesz) == 0) {
282				afs_get_cell(cell);
283				goto found;
284			}
285		}
286		cell = ERR_PTR(-ENOENT);
287		if (dns_cell)
288			goto create_cell;
289	found:
290		;
291	} else {
292		cell = afs_cell_root;
293		if (!cell) {
294			/* this should not happen unless user tries to mount
295			 * when root cell is not set. Return an impossibly
296			 * bizzare errno to alert the user. Things like
297			 * ENOENT might be "more appropriate" but they happen
298			 * for other reasons.
299			 */
300			cell = ERR_PTR(-EDESTADDRREQ);
301		} else {
302			afs_get_cell(cell);
303		}
304
305	}
306
307	read_unlock(&afs_cells_lock);
308	up_read(&afs_cells_sem);
309	_leave(" = %p", cell);
310	return cell;
311
312create_cell:
313	read_unlock(&afs_cells_lock);
314	up_read(&afs_cells_sem);
315
316	cell = afs_cell_create(name, namesz, NULL, true);
317
318	_leave(" = %p", cell);
319	return cell;
320}
321
322
323/*
324 * destroy a cell record
325 */
326void afs_put_cell(struct afs_cell *cell)
327{
328	if (!cell)
329		return;
330
331	_enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
332
333	ASSERTCMP(atomic_read(&cell->usage), >, 0);
334
335	/* to prevent a race, the decrement and the dequeue must be effectively
336	 * atomic */
337	write_lock(&afs_cells_lock);
338
339	if (likely(!atomic_dec_and_test(&cell->usage))) {
340		write_unlock(&afs_cells_lock);
341		_leave("");
342		return;
343	}
344
345	ASSERT(list_empty(&cell->servers));
346	ASSERT(list_empty(&cell->vl_list));
347
348	write_unlock(&afs_cells_lock);
349
350	wake_up(&afs_cells_freeable_wq);
351
352	_leave(" [unused]");
353}
354
355/*
356 * destroy a cell record
357 * - must be called with the afs_cells_sem write-locked
358 * - cell->link should have been broken by the caller
359 */
360static void afs_cell_destroy(struct afs_cell *cell)
361{
362	_enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
363
364	ASSERTCMP(atomic_read(&cell->usage), >=, 0);
365	ASSERT(list_empty(&cell->link));
366
367	/* wait for everyone to stop using the cell */
368	if (atomic_read(&cell->usage) > 0) {
369		DECLARE_WAITQUEUE(myself, current);
370
371		_debug("wait for cell %s", cell->name);
372		set_current_state(TASK_UNINTERRUPTIBLE);
373		add_wait_queue(&afs_cells_freeable_wq, &myself);
374
375		while (atomic_read(&cell->usage) > 0) {
376			schedule();
377			set_current_state(TASK_UNINTERRUPTIBLE);
378		}
379
380		remove_wait_queue(&afs_cells_freeable_wq, &myself);
381		set_current_state(TASK_RUNNING);
382	}
383
384	_debug("cell dead");
385	ASSERTCMP(atomic_read(&cell->usage), ==, 0);
386	ASSERT(list_empty(&cell->servers));
387	ASSERT(list_empty(&cell->vl_list));
388
389	afs_proc_cell_remove(cell);
390
391	down_write(&afs_proc_cells_sem);
392	list_del_init(&cell->proc_link);
393	up_write(&afs_proc_cells_sem);
394
395#ifdef CONFIG_AFS_FSCACHE
396	fscache_relinquish_cookie(cell->cache, 0);
397#endif
398	key_put(cell->anonymous_key);
399	kfree(cell);
400
401	_leave(" [destroyed]");
402}
403
404/*
405 * purge in-memory cell database on module unload or afs_init() failure
406 * - the timeout daemon is stopped before calling this
407 */
408void afs_cell_purge(void)
409{
410	struct afs_cell *cell;
411
412	_enter("");
413
414	afs_put_cell(afs_cell_root);
415
416	down_write(&afs_cells_sem);
417
418	while (!list_empty(&afs_cells)) {
419		cell = NULL;
420
421		/* remove the next cell from the front of the list */
422		write_lock(&afs_cells_lock);
423
424		if (!list_empty(&afs_cells)) {
425			cell = list_entry(afs_cells.next,
426					  struct afs_cell, link);
427			list_del_init(&cell->link);
428		}
429
430		write_unlock(&afs_cells_lock);
431
432		if (cell) {
433			_debug("PURGING CELL %s (%d)",
434			       cell->name, atomic_read(&cell->usage));
435
436			/* now the cell should be left with no references */
437			afs_cell_destroy(cell);
438		}
439	}
440
441	up_write(&afs_cells_sem);
442	_leave("");
443}
444