Loading...
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, true);
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 * bizarre 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#if 0
323/*
324 * try and get a cell record
325 */
326struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
327{
328 write_lock(&afs_cells_lock);
329
330 if (cell && !list_empty(&cell->link))
331 afs_get_cell(cell);
332 else
333 cell = NULL;
334
335 write_unlock(&afs_cells_lock);
336 return cell;
337}
338#endif /* 0 */
339
340/*
341 * destroy a cell record
342 */
343void afs_put_cell(struct afs_cell *cell)
344{
345 if (!cell)
346 return;
347
348 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
349
350 ASSERTCMP(atomic_read(&cell->usage), >, 0);
351
352 /* to prevent a race, the decrement and the dequeue must be effectively
353 * atomic */
354 write_lock(&afs_cells_lock);
355
356 if (likely(!atomic_dec_and_test(&cell->usage))) {
357 write_unlock(&afs_cells_lock);
358 _leave("");
359 return;
360 }
361
362 ASSERT(list_empty(&cell->servers));
363 ASSERT(list_empty(&cell->vl_list));
364
365 write_unlock(&afs_cells_lock);
366
367 wake_up(&afs_cells_freeable_wq);
368
369 _leave(" [unused]");
370}
371
372/*
373 * destroy a cell record
374 * - must be called with the afs_cells_sem write-locked
375 * - cell->link should have been broken by the caller
376 */
377static void afs_cell_destroy(struct afs_cell *cell)
378{
379 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
380
381 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
382 ASSERT(list_empty(&cell->link));
383
384 /* wait for everyone to stop using the cell */
385 if (atomic_read(&cell->usage) > 0) {
386 DECLARE_WAITQUEUE(myself, current);
387
388 _debug("wait for cell %s", cell->name);
389 set_current_state(TASK_UNINTERRUPTIBLE);
390 add_wait_queue(&afs_cells_freeable_wq, &myself);
391
392 while (atomic_read(&cell->usage) > 0) {
393 schedule();
394 set_current_state(TASK_UNINTERRUPTIBLE);
395 }
396
397 remove_wait_queue(&afs_cells_freeable_wq, &myself);
398 set_current_state(TASK_RUNNING);
399 }
400
401 _debug("cell dead");
402 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
403 ASSERT(list_empty(&cell->servers));
404 ASSERT(list_empty(&cell->vl_list));
405
406 afs_proc_cell_remove(cell);
407
408 down_write(&afs_proc_cells_sem);
409 list_del_init(&cell->proc_link);
410 up_write(&afs_proc_cells_sem);
411
412#ifdef CONFIG_AFS_FSCACHE
413 fscache_relinquish_cookie(cell->cache, 0);
414#endif
415 key_put(cell->anonymous_key);
416 kfree(cell);
417
418 _leave(" [destroyed]");
419}
420
421/*
422 * purge in-memory cell database on module unload or afs_init() failure
423 * - the timeout daemon is stopped before calling this
424 */
425void afs_cell_purge(void)
426{
427 struct afs_cell *cell;
428
429 _enter("");
430
431 afs_put_cell(afs_cell_root);
432
433 down_write(&afs_cells_sem);
434
435 while (!list_empty(&afs_cells)) {
436 cell = NULL;
437
438 /* remove the next cell from the front of the list */
439 write_lock(&afs_cells_lock);
440
441 if (!list_empty(&afs_cells)) {
442 cell = list_entry(afs_cells.next,
443 struct afs_cell, link);
444 list_del_init(&cell->link);
445 }
446
447 write_unlock(&afs_cells_lock);
448
449 if (cell) {
450 _debug("PURGING CELL %s (%d)",
451 cell->name, atomic_read(&cell->usage));
452
453 /* now the cell should be left with no references */
454 afs_cell_destroy(cell);
455 }
456 }
457
458 up_write(&afs_cells_sem);
459 _leave("");
460}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/* AFS cell and server record management
3 *
4 * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#include <linux/slab.h>
9#include <linux/key.h>
10#include <linux/ctype.h>
11#include <linux/dns_resolver.h>
12#include <linux/sched.h>
13#include <linux/inet.h>
14#include <linux/namei.h>
15#include <keys/rxrpc-type.h>
16#include "internal.h"
17
18static unsigned __read_mostly afs_cell_gc_delay = 10;
19static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
20static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
21static atomic_t cell_debug_id;
22
23static void afs_queue_cell_manager(struct afs_net *);
24static void afs_manage_cell_work(struct work_struct *);
25
26static void afs_dec_cells_outstanding(struct afs_net *net)
27{
28 if (atomic_dec_and_test(&net->cells_outstanding))
29 wake_up_var(&net->cells_outstanding);
30}
31
32/*
33 * Set the cell timer to fire after a given delay, assuming it's not already
34 * set for an earlier time.
35 */
36static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
37{
38 if (net->live) {
39 atomic_inc(&net->cells_outstanding);
40 if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
41 afs_dec_cells_outstanding(net);
42 } else {
43 afs_queue_cell_manager(net);
44 }
45}
46
47/*
48 * Look up and get an activation reference on a cell record. The caller must
49 * hold net->cells_lock at least read-locked.
50 */
51static struct afs_cell *afs_find_cell_locked(struct afs_net *net,
52 const char *name, unsigned int namesz,
53 enum afs_cell_trace reason)
54{
55 struct afs_cell *cell = NULL;
56 struct rb_node *p;
57 int n;
58
59 _enter("%*.*s", namesz, namesz, name);
60
61 if (name && namesz == 0)
62 return ERR_PTR(-EINVAL);
63 if (namesz > AFS_MAXCELLNAME)
64 return ERR_PTR(-ENAMETOOLONG);
65
66 if (!name) {
67 cell = net->ws_cell;
68 if (!cell)
69 return ERR_PTR(-EDESTADDRREQ);
70 goto found;
71 }
72
73 p = net->cells.rb_node;
74 while (p) {
75 cell = rb_entry(p, struct afs_cell, net_node);
76
77 n = strncasecmp(cell->name, name,
78 min_t(size_t, cell->name_len, namesz));
79 if (n == 0)
80 n = cell->name_len - namesz;
81 if (n < 0)
82 p = p->rb_left;
83 else if (n > 0)
84 p = p->rb_right;
85 else
86 goto found;
87 }
88
89 return ERR_PTR(-ENOENT);
90
91found:
92 return afs_use_cell(cell, reason);
93}
94
95/*
96 * Look up and get an activation reference on a cell record.
97 */
98struct afs_cell *afs_find_cell(struct afs_net *net,
99 const char *name, unsigned int namesz,
100 enum afs_cell_trace reason)
101{
102 struct afs_cell *cell;
103
104 down_read(&net->cells_lock);
105 cell = afs_find_cell_locked(net, name, namesz, reason);
106 up_read(&net->cells_lock);
107 return cell;
108}
109
110/*
111 * Set up a cell record and fill in its name, VL server address list and
112 * allocate an anonymous key
113 */
114static struct afs_cell *afs_alloc_cell(struct afs_net *net,
115 const char *name, unsigned int namelen,
116 const char *addresses)
117{
118 struct afs_vlserver_list *vllist;
119 struct afs_cell *cell;
120 int i, ret;
121
122 ASSERT(name);
123 if (namelen == 0)
124 return ERR_PTR(-EINVAL);
125 if (namelen > AFS_MAXCELLNAME) {
126 _leave(" = -ENAMETOOLONG");
127 return ERR_PTR(-ENAMETOOLONG);
128 }
129
130 /* Prohibit cell names that contain unprintable chars, '/' and '@' or
131 * that begin with a dot. This also precludes "@cell".
132 */
133 if (name[0] == '.')
134 return ERR_PTR(-EINVAL);
135 for (i = 0; i < namelen; i++) {
136 char ch = name[i];
137 if (!isprint(ch) || ch == '/' || ch == '@')
138 return ERR_PTR(-EINVAL);
139 }
140
141 _enter("%*.*s,%s", namelen, namelen, name, addresses);
142
143 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
144 if (!cell) {
145 _leave(" = -ENOMEM");
146 return ERR_PTR(-ENOMEM);
147 }
148
149 cell->name = kmalloc(namelen + 1, GFP_KERNEL);
150 if (!cell->name) {
151 kfree(cell);
152 return ERR_PTR(-ENOMEM);
153 }
154
155 cell->net = net;
156 cell->name_len = namelen;
157 for (i = 0; i < namelen; i++)
158 cell->name[i] = tolower(name[i]);
159 cell->name[i] = 0;
160
161 refcount_set(&cell->ref, 1);
162 atomic_set(&cell->active, 0);
163 INIT_WORK(&cell->manager, afs_manage_cell_work);
164 init_rwsem(&cell->vs_lock);
165 cell->volumes = RB_ROOT;
166 INIT_HLIST_HEAD(&cell->proc_volumes);
167 seqlock_init(&cell->volume_lock);
168 cell->fs_servers = RB_ROOT;
169 seqlock_init(&cell->fs_lock);
170 rwlock_init(&cell->vl_servers_lock);
171 cell->flags = (1 << AFS_CELL_FL_CHECK_ALIAS);
172
173 /* Provide a VL server list, filling it in if we were given a list of
174 * addresses to use.
175 */
176 if (addresses) {
177 vllist = afs_parse_text_addrs(net,
178 addresses, strlen(addresses), ':',
179 VL_SERVICE, AFS_VL_PORT);
180 if (IS_ERR(vllist)) {
181 ret = PTR_ERR(vllist);
182 goto parse_failed;
183 }
184
185 vllist->source = DNS_RECORD_FROM_CONFIG;
186 vllist->status = DNS_LOOKUP_NOT_DONE;
187 cell->dns_expiry = TIME64_MAX;
188 } else {
189 ret = -ENOMEM;
190 vllist = afs_alloc_vlserver_list(0);
191 if (!vllist)
192 goto error;
193 vllist->source = DNS_RECORD_UNAVAILABLE;
194 vllist->status = DNS_LOOKUP_NOT_DONE;
195 cell->dns_expiry = ktime_get_real_seconds();
196 }
197
198 rcu_assign_pointer(cell->vl_servers, vllist);
199
200 cell->dns_source = vllist->source;
201 cell->dns_status = vllist->status;
202 smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
203 atomic_inc(&net->cells_outstanding);
204 cell->debug_id = atomic_inc_return(&cell_debug_id);
205 trace_afs_cell(cell->debug_id, 1, 0, afs_cell_trace_alloc);
206
207 _leave(" = %p", cell);
208 return cell;
209
210parse_failed:
211 if (ret == -EINVAL)
212 printk(KERN_ERR "kAFS: bad VL server IP address\n");
213error:
214 kfree(cell->name);
215 kfree(cell);
216 _leave(" = %d", ret);
217 return ERR_PTR(ret);
218}
219
220/*
221 * afs_lookup_cell - Look up or create a cell record.
222 * @net: The network namespace
223 * @name: The name of the cell.
224 * @namesz: The strlen of the cell name.
225 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
226 * @excl: T if an error should be given if the cell name already exists.
227 *
228 * Look up a cell record by name and query the DNS for VL server addresses if
229 * needed. Note that that actual DNS query is punted off to the manager thread
230 * so that this function can return immediately if interrupted whilst allowing
231 * cell records to be shared even if not yet fully constructed.
232 */
233struct afs_cell *afs_lookup_cell(struct afs_net *net,
234 const char *name, unsigned int namesz,
235 const char *vllist, bool excl)
236{
237 struct afs_cell *cell, *candidate, *cursor;
238 struct rb_node *parent, **pp;
239 enum afs_cell_state state;
240 int ret, n;
241
242 _enter("%s,%s", name, vllist);
243
244 if (!excl) {
245 cell = afs_find_cell(net, name, namesz, afs_cell_trace_use_lookup);
246 if (!IS_ERR(cell))
247 goto wait_for_cell;
248 }
249
250 /* Assume we're probably going to create a cell and preallocate and
251 * mostly set up a candidate record. We can then use this to stash the
252 * name, the net namespace and VL server addresses.
253 *
254 * We also want to do this before we hold any locks as it may involve
255 * upcalling to userspace to make DNS queries.
256 */
257 candidate = afs_alloc_cell(net, name, namesz, vllist);
258 if (IS_ERR(candidate)) {
259 _leave(" = %ld", PTR_ERR(candidate));
260 return candidate;
261 }
262
263 /* Find the insertion point and check to see if someone else added a
264 * cell whilst we were allocating.
265 */
266 down_write(&net->cells_lock);
267
268 pp = &net->cells.rb_node;
269 parent = NULL;
270 while (*pp) {
271 parent = *pp;
272 cursor = rb_entry(parent, struct afs_cell, net_node);
273
274 n = strncasecmp(cursor->name, name,
275 min_t(size_t, cursor->name_len, namesz));
276 if (n == 0)
277 n = cursor->name_len - namesz;
278 if (n < 0)
279 pp = &(*pp)->rb_left;
280 else if (n > 0)
281 pp = &(*pp)->rb_right;
282 else
283 goto cell_already_exists;
284 }
285
286 cell = candidate;
287 candidate = NULL;
288 atomic_set(&cell->active, 2);
289 trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), 2, afs_cell_trace_insert);
290 rb_link_node_rcu(&cell->net_node, parent, pp);
291 rb_insert_color(&cell->net_node, &net->cells);
292 up_write(&net->cells_lock);
293
294 afs_queue_cell(cell, afs_cell_trace_get_queue_new);
295
296wait_for_cell:
297 trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), atomic_read(&cell->active),
298 afs_cell_trace_wait);
299 _debug("wait_for_cell");
300 wait_var_event(&cell->state,
301 ({
302 state = smp_load_acquire(&cell->state); /* vs error */
303 state == AFS_CELL_ACTIVE || state == AFS_CELL_REMOVED;
304 }));
305
306 /* Check the state obtained from the wait check. */
307 if (state == AFS_CELL_REMOVED) {
308 ret = cell->error;
309 goto error;
310 }
311
312 _leave(" = %p [cell]", cell);
313 return cell;
314
315cell_already_exists:
316 _debug("cell exists");
317 cell = cursor;
318 if (excl) {
319 ret = -EEXIST;
320 } else {
321 afs_use_cell(cursor, afs_cell_trace_use_lookup);
322 ret = 0;
323 }
324 up_write(&net->cells_lock);
325 if (candidate)
326 afs_put_cell(candidate, afs_cell_trace_put_candidate);
327 if (ret == 0)
328 goto wait_for_cell;
329 goto error_noput;
330error:
331 afs_unuse_cell(net, cell, afs_cell_trace_unuse_lookup);
332error_noput:
333 _leave(" = %d [error]", ret);
334 return ERR_PTR(ret);
335}
336
337/*
338 * set the root cell information
339 * - can be called with a module parameter string
340 * - can be called from a write to /proc/fs/afs/rootcell
341 */
342int afs_cell_init(struct afs_net *net, const char *rootcell)
343{
344 struct afs_cell *old_root, *new_root;
345 const char *cp, *vllist;
346 size_t len;
347
348 _enter("");
349
350 if (!rootcell) {
351 /* module is loaded with no parameters, or built statically.
352 * - in the future we might initialize cell DB here.
353 */
354 _leave(" = 0 [no root]");
355 return 0;
356 }
357
358 cp = strchr(rootcell, ':');
359 if (!cp) {
360 _debug("kAFS: no VL server IP addresses specified");
361 vllist = NULL;
362 len = strlen(rootcell);
363 } else {
364 vllist = cp + 1;
365 len = cp - rootcell;
366 }
367
368 /* allocate a cell record for the root cell */
369 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
370 if (IS_ERR(new_root)) {
371 _leave(" = %ld", PTR_ERR(new_root));
372 return PTR_ERR(new_root);
373 }
374
375 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
376 afs_use_cell(new_root, afs_cell_trace_use_pin);
377
378 /* install the new cell */
379 down_write(&net->cells_lock);
380 afs_see_cell(new_root, afs_cell_trace_see_ws);
381 old_root = net->ws_cell;
382 net->ws_cell = new_root;
383 up_write(&net->cells_lock);
384
385 afs_unuse_cell(net, old_root, afs_cell_trace_unuse_ws);
386 _leave(" = 0");
387 return 0;
388}
389
390/*
391 * Update a cell's VL server address list from the DNS.
392 */
393static int afs_update_cell(struct afs_cell *cell)
394{
395 struct afs_vlserver_list *vllist, *old = NULL, *p;
396 unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
397 unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
398 time64_t now, expiry = 0;
399 int ret = 0;
400
401 _enter("%s", cell->name);
402
403 vllist = afs_dns_query(cell, &expiry);
404 if (IS_ERR(vllist)) {
405 ret = PTR_ERR(vllist);
406
407 _debug("%s: fail %d", cell->name, ret);
408 if (ret == -ENOMEM)
409 goto out_wake;
410
411 vllist = afs_alloc_vlserver_list(0);
412 if (!vllist) {
413 if (ret >= 0)
414 ret = -ENOMEM;
415 goto out_wake;
416 }
417
418 switch (ret) {
419 case -ENODATA:
420 case -EDESTADDRREQ:
421 vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
422 break;
423 case -EAGAIN:
424 case -ECONNREFUSED:
425 vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
426 break;
427 default:
428 vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
429 break;
430 }
431 }
432
433 _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
434 cell->dns_status = vllist->status;
435
436 now = ktime_get_real_seconds();
437 if (min_ttl > max_ttl)
438 max_ttl = min_ttl;
439 if (expiry < now + min_ttl)
440 expiry = now + min_ttl;
441 else if (expiry > now + max_ttl)
442 expiry = now + max_ttl;
443
444 _debug("%s: status %d", cell->name, vllist->status);
445 if (vllist->source == DNS_RECORD_UNAVAILABLE) {
446 switch (vllist->status) {
447 case DNS_LOOKUP_GOT_NOT_FOUND:
448 /* The DNS said that the cell does not exist or there
449 * weren't any addresses to be had.
450 */
451 cell->dns_expiry = expiry;
452 break;
453
454 case DNS_LOOKUP_BAD:
455 case DNS_LOOKUP_GOT_LOCAL_FAILURE:
456 case DNS_LOOKUP_GOT_TEMP_FAILURE:
457 case DNS_LOOKUP_GOT_NS_FAILURE:
458 default:
459 cell->dns_expiry = now + 10;
460 break;
461 }
462 } else {
463 cell->dns_expiry = expiry;
464 }
465
466 /* Replace the VL server list if the new record has servers or the old
467 * record doesn't.
468 */
469 write_lock(&cell->vl_servers_lock);
470 p = rcu_dereference_protected(cell->vl_servers, true);
471 if (vllist->nr_servers > 0 || p->nr_servers == 0) {
472 rcu_assign_pointer(cell->vl_servers, vllist);
473 cell->dns_source = vllist->source;
474 old = p;
475 }
476 write_unlock(&cell->vl_servers_lock);
477 afs_put_vlserverlist(cell->net, old);
478
479out_wake:
480 smp_store_release(&cell->dns_lookup_count,
481 cell->dns_lookup_count + 1); /* vs source/status */
482 wake_up_var(&cell->dns_lookup_count);
483 _leave(" = %d", ret);
484 return ret;
485}
486
487/*
488 * Destroy a cell record
489 */
490static void afs_cell_destroy(struct rcu_head *rcu)
491{
492 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
493 struct afs_net *net = cell->net;
494 int r;
495
496 _enter("%p{%s}", cell, cell->name);
497
498 r = refcount_read(&cell->ref);
499 ASSERTCMP(r, ==, 0);
500 trace_afs_cell(cell->debug_id, r, atomic_read(&cell->active), afs_cell_trace_free);
501
502 afs_put_vlserverlist(net, rcu_access_pointer(cell->vl_servers));
503 afs_unuse_cell(net, cell->alias_of, afs_cell_trace_unuse_alias);
504 key_put(cell->anonymous_key);
505 kfree(cell->name);
506 kfree(cell);
507
508 afs_dec_cells_outstanding(net);
509 _leave(" [destroyed]");
510}
511
512/*
513 * Queue the cell manager.
514 */
515static void afs_queue_cell_manager(struct afs_net *net)
516{
517 int outstanding = atomic_inc_return(&net->cells_outstanding);
518
519 _enter("%d", outstanding);
520
521 if (!queue_work(afs_wq, &net->cells_manager))
522 afs_dec_cells_outstanding(net);
523}
524
525/*
526 * Cell management timer. We have an increment on cells_outstanding that we
527 * need to pass along to the work item.
528 */
529void afs_cells_timer(struct timer_list *timer)
530{
531 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
532
533 _enter("");
534 if (!queue_work(afs_wq, &net->cells_manager))
535 afs_dec_cells_outstanding(net);
536}
537
538/*
539 * Get a reference on a cell record.
540 */
541struct afs_cell *afs_get_cell(struct afs_cell *cell, enum afs_cell_trace reason)
542{
543 int r;
544
545 __refcount_inc(&cell->ref, &r);
546 trace_afs_cell(cell->debug_id, r + 1, atomic_read(&cell->active), reason);
547 return cell;
548}
549
550/*
551 * Drop a reference on a cell record.
552 */
553void afs_put_cell(struct afs_cell *cell, enum afs_cell_trace reason)
554{
555 if (cell) {
556 unsigned int debug_id = cell->debug_id;
557 unsigned int a;
558 bool zero;
559 int r;
560
561 a = atomic_read(&cell->active);
562 zero = __refcount_dec_and_test(&cell->ref, &r);
563 trace_afs_cell(debug_id, r - 1, a, reason);
564 if (zero) {
565 a = atomic_read(&cell->active);
566 WARN(a != 0, "Cell active count %u > 0\n", a);
567 call_rcu(&cell->rcu, afs_cell_destroy);
568 }
569 }
570}
571
572/*
573 * Note a cell becoming more active.
574 */
575struct afs_cell *afs_use_cell(struct afs_cell *cell, enum afs_cell_trace reason)
576{
577 int r, a;
578
579 r = refcount_read(&cell->ref);
580 WARN_ON(r == 0);
581 a = atomic_inc_return(&cell->active);
582 trace_afs_cell(cell->debug_id, r, a, reason);
583 return cell;
584}
585
586/*
587 * Record a cell becoming less active. When the active counter reaches 1, it
588 * is scheduled for destruction, but may get reactivated.
589 */
590void afs_unuse_cell(struct afs_net *net, struct afs_cell *cell, enum afs_cell_trace reason)
591{
592 unsigned int debug_id;
593 time64_t now, expire_delay;
594 int r, a;
595
596 if (!cell)
597 return;
598
599 _enter("%s", cell->name);
600
601 now = ktime_get_real_seconds();
602 cell->last_inactive = now;
603 expire_delay = 0;
604 if (cell->vl_servers->nr_servers)
605 expire_delay = afs_cell_gc_delay;
606
607 debug_id = cell->debug_id;
608 r = refcount_read(&cell->ref);
609 a = atomic_dec_return(&cell->active);
610 trace_afs_cell(debug_id, r, a, reason);
611 WARN_ON(a == 0);
612 if (a == 1)
613 /* 'cell' may now be garbage collected. */
614 afs_set_cell_timer(net, expire_delay);
615}
616
617/*
618 * Note that a cell has been seen.
619 */
620void afs_see_cell(struct afs_cell *cell, enum afs_cell_trace reason)
621{
622 int r, a;
623
624 r = refcount_read(&cell->ref);
625 a = atomic_read(&cell->active);
626 trace_afs_cell(cell->debug_id, r, a, reason);
627}
628
629/*
630 * Queue a cell for management, giving the workqueue a ref to hold.
631 */
632void afs_queue_cell(struct afs_cell *cell, enum afs_cell_trace reason)
633{
634 afs_get_cell(cell, reason);
635 if (!queue_work(afs_wq, &cell->manager))
636 afs_put_cell(cell, afs_cell_trace_put_queue_fail);
637}
638
639/*
640 * Allocate a key to use as a placeholder for anonymous user security.
641 */
642static int afs_alloc_anon_key(struct afs_cell *cell)
643{
644 struct key *key;
645 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
646
647 /* Create a key to represent an anonymous user. */
648 memcpy(keyname, "afs@", 4);
649 dp = keyname + 4;
650 cp = cell->name;
651 do {
652 *dp++ = tolower(*cp);
653 } while (*cp++);
654
655 key = rxrpc_get_null_key(keyname);
656 if (IS_ERR(key))
657 return PTR_ERR(key);
658
659 cell->anonymous_key = key;
660
661 _debug("anon key %p{%x}",
662 cell->anonymous_key, key_serial(cell->anonymous_key));
663 return 0;
664}
665
666/*
667 * Activate a cell.
668 */
669static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
670{
671 struct hlist_node **p;
672 struct afs_cell *pcell;
673 int ret;
674
675 if (!cell->anonymous_key) {
676 ret = afs_alloc_anon_key(cell);
677 if (ret < 0)
678 return ret;
679 }
680
681 ret = afs_proc_cell_setup(cell);
682 if (ret < 0)
683 return ret;
684
685 mutex_lock(&net->proc_cells_lock);
686 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
687 pcell = hlist_entry(*p, struct afs_cell, proc_link);
688 if (strcmp(cell->name, pcell->name) < 0)
689 break;
690 }
691
692 cell->proc_link.pprev = p;
693 cell->proc_link.next = *p;
694 rcu_assign_pointer(*p, &cell->proc_link.next);
695 if (cell->proc_link.next)
696 cell->proc_link.next->pprev = &cell->proc_link.next;
697
698 afs_dynroot_mkdir(net, cell);
699 mutex_unlock(&net->proc_cells_lock);
700 return 0;
701}
702
703/*
704 * Deactivate a cell.
705 */
706static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
707{
708 _enter("%s", cell->name);
709
710 afs_proc_cell_remove(cell);
711
712 mutex_lock(&net->proc_cells_lock);
713 hlist_del_rcu(&cell->proc_link);
714 afs_dynroot_rmdir(net, cell);
715 mutex_unlock(&net->proc_cells_lock);
716
717 _leave("");
718}
719
720/*
721 * Manage a cell record, initialising and destroying it, maintaining its DNS
722 * records.
723 */
724static void afs_manage_cell(struct afs_cell *cell)
725{
726 struct afs_net *net = cell->net;
727 int ret, active;
728
729 _enter("%s", cell->name);
730
731again:
732 _debug("state %u", cell->state);
733 switch (cell->state) {
734 case AFS_CELL_INACTIVE:
735 case AFS_CELL_FAILED:
736 down_write(&net->cells_lock);
737 active = 1;
738 if (atomic_try_cmpxchg_relaxed(&cell->active, &active, 0)) {
739 rb_erase(&cell->net_node, &net->cells);
740 trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), 0,
741 afs_cell_trace_unuse_delete);
742 smp_store_release(&cell->state, AFS_CELL_REMOVED);
743 }
744 up_write(&net->cells_lock);
745 if (cell->state == AFS_CELL_REMOVED) {
746 wake_up_var(&cell->state);
747 goto final_destruction;
748 }
749 if (cell->state == AFS_CELL_FAILED)
750 goto done;
751 smp_store_release(&cell->state, AFS_CELL_UNSET);
752 wake_up_var(&cell->state);
753 goto again;
754
755 case AFS_CELL_UNSET:
756 smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
757 wake_up_var(&cell->state);
758 goto again;
759
760 case AFS_CELL_ACTIVATING:
761 ret = afs_activate_cell(net, cell);
762 if (ret < 0)
763 goto activation_failed;
764
765 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
766 wake_up_var(&cell->state);
767 goto again;
768
769 case AFS_CELL_ACTIVE:
770 if (atomic_read(&cell->active) > 1) {
771 if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
772 ret = afs_update_cell(cell);
773 if (ret < 0)
774 cell->error = ret;
775 }
776 goto done;
777 }
778 smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
779 wake_up_var(&cell->state);
780 goto again;
781
782 case AFS_CELL_DEACTIVATING:
783 if (atomic_read(&cell->active) > 1)
784 goto reverse_deactivation;
785 afs_deactivate_cell(net, cell);
786 smp_store_release(&cell->state, AFS_CELL_INACTIVE);
787 wake_up_var(&cell->state);
788 goto again;
789
790 case AFS_CELL_REMOVED:
791 goto done;
792
793 default:
794 break;
795 }
796 _debug("bad state %u", cell->state);
797 BUG(); /* Unhandled state */
798
799activation_failed:
800 cell->error = ret;
801 afs_deactivate_cell(net, cell);
802
803 smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
804 wake_up_var(&cell->state);
805 goto again;
806
807reverse_deactivation:
808 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
809 wake_up_var(&cell->state);
810 _leave(" [deact->act]");
811 return;
812
813done:
814 _leave(" [done %u]", cell->state);
815 return;
816
817final_destruction:
818 /* The root volume is pinning the cell */
819 afs_put_volume(cell->root_volume, afs_volume_trace_put_cell_root);
820 cell->root_volume = NULL;
821 afs_put_cell(cell, afs_cell_trace_put_destroy);
822}
823
824static void afs_manage_cell_work(struct work_struct *work)
825{
826 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
827
828 afs_manage_cell(cell);
829 afs_put_cell(cell, afs_cell_trace_put_queue_work);
830}
831
832/*
833 * Manage the records of cells known to a network namespace. This includes
834 * updating the DNS records and garbage collecting unused cells that were
835 * automatically added.
836 *
837 * Note that constructed cell records may only be removed from net->cells by
838 * this work item, so it is safe for this work item to stash a cursor pointing
839 * into the tree and then return to caller (provided it skips cells that are
840 * still under construction).
841 *
842 * Note also that we were given an increment on net->cells_outstanding by
843 * whoever queued us that we need to deal with before returning.
844 */
845void afs_manage_cells(struct work_struct *work)
846{
847 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
848 struct rb_node *cursor;
849 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
850 bool purging = !net->live;
851
852 _enter("");
853
854 /* Trawl the cell database looking for cells that have expired from
855 * lack of use and cells whose DNS results have expired and dispatch
856 * their managers.
857 */
858 down_read(&net->cells_lock);
859
860 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
861 struct afs_cell *cell =
862 rb_entry(cursor, struct afs_cell, net_node);
863 unsigned active;
864 bool sched_cell = false;
865
866 active = atomic_read(&cell->active);
867 trace_afs_cell(cell->debug_id, refcount_read(&cell->ref),
868 active, afs_cell_trace_manage);
869
870 ASSERTCMP(active, >=, 1);
871
872 if (purging) {
873 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags)) {
874 active = atomic_dec_return(&cell->active);
875 trace_afs_cell(cell->debug_id, refcount_read(&cell->ref),
876 active, afs_cell_trace_unuse_pin);
877 }
878 }
879
880 if (active == 1) {
881 struct afs_vlserver_list *vllist;
882 time64_t expire_at = cell->last_inactive;
883
884 read_lock(&cell->vl_servers_lock);
885 vllist = rcu_dereference_protected(
886 cell->vl_servers,
887 lockdep_is_held(&cell->vl_servers_lock));
888 if (vllist->nr_servers > 0)
889 expire_at += afs_cell_gc_delay;
890 read_unlock(&cell->vl_servers_lock);
891 if (purging || expire_at <= now)
892 sched_cell = true;
893 else if (expire_at < next_manage)
894 next_manage = expire_at;
895 }
896
897 if (!purging) {
898 if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
899 sched_cell = true;
900 }
901
902 if (sched_cell)
903 afs_queue_cell(cell, afs_cell_trace_get_queue_manage);
904 }
905
906 up_read(&net->cells_lock);
907
908 /* Update the timer on the way out. We have to pass an increment on
909 * cells_outstanding in the namespace that we are in to the timer or
910 * the work scheduler.
911 */
912 if (!purging && next_manage < TIME64_MAX) {
913 now = ktime_get_real_seconds();
914
915 if (next_manage - now <= 0) {
916 if (queue_work(afs_wq, &net->cells_manager))
917 atomic_inc(&net->cells_outstanding);
918 } else {
919 afs_set_cell_timer(net, next_manage - now);
920 }
921 }
922
923 afs_dec_cells_outstanding(net);
924 _leave(" [%d]", atomic_read(&net->cells_outstanding));
925}
926
927/*
928 * Purge in-memory cell database.
929 */
930void afs_cell_purge(struct afs_net *net)
931{
932 struct afs_cell *ws;
933
934 _enter("");
935
936 down_write(&net->cells_lock);
937 ws = net->ws_cell;
938 net->ws_cell = NULL;
939 up_write(&net->cells_lock);
940 afs_unuse_cell(net, ws, afs_cell_trace_unuse_ws);
941
942 _debug("del timer");
943 if (del_timer_sync(&net->cells_timer))
944 atomic_dec(&net->cells_outstanding);
945
946 _debug("kick mgr");
947 afs_queue_cell_manager(net);
948
949 _debug("wait");
950 wait_var_event(&net->cells_outstanding,
951 !atomic_read(&net->cells_outstanding));
952 _leave("");
953}