blob: 07ad744eef77fd433bc89ec51f52e5518168df0e [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howellsec268152007-04-26 15:49:28 -07002/* AFS cell and server record management
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Howells989782d2017-11-02 15:27:50 +00004 * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by David Howells (dhowells@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/slab.h>
David Howells00d3b7a2007-04-26 15:57:07 -07009#include <linux/key.h>
10#include <linux/ctype.h>
Wang Lei07567a52010-08-04 15:16:38 +010011#include <linux/dns_resolver.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040012#include <linux/sched.h>
David Howells3838d3e2017-11-02 15:27:47 +000013#include <linux/inet.h>
David Howells0da0b7f2018-06-15 15:19:22 +010014#include <linux/namei.h>
David Howells00d3b7a2007-04-26 15:57:07 -070015#include <keys/rxrpc-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "internal.h"
17
David Howellsfe342cf2018-04-09 21:12:31 +010018static unsigned __read_mostly afs_cell_gc_delay = 10;
David Howellsded2f4c2018-10-20 00:57:57 +010019static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
20static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
David Howellsdca54a72020-10-13 20:51:59 +010021static atomic_t cell_debug_id;
David Howells989782d2017-11-02 15:27:50 +000022
David Howells286377f62020-10-15 11:05:01 +010023static void afs_queue_cell_manager(struct afs_net *);
David Howells88c853c2019-07-23 11:24:59 +010024static void afs_manage_cell_work(struct work_struct *);
David Howells989782d2017-11-02 15:27:50 +000025
26static void afs_dec_cells_outstanding(struct afs_net *net)
27{
28 if (atomic_dec_and_test(&net->cells_outstanding))
Peter Zijlstraab1fbe32018-03-15 11:42:28 +010029 wake_up_var(&net->cells_outstanding);
David Howells989782d2017-11-02 15:27:50 +000030}
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
David Howells989782d2017-11-02 15:27:50 +000033 * 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);
David Howells286377f62020-10-15 11:05:01 +010042 } else {
43 afs_queue_cell_manager(net);
David Howells989782d2017-11-02 15:27:50 +000044 }
45}
46
47/*
David Howells92e3cc92020-10-09 14:11:58 +010048 * Look up and get an activation reference on a cell record. The caller must
49 * hold net->cells_lock at least read-locked.
David Howells989782d2017-11-02 15:27:50 +000050 */
David Howells92e3cc92020-10-09 14:11:58 +010051static struct afs_cell *afs_find_cell_locked(struct afs_net *net,
David Howellsdca54a72020-10-13 20:51:59 +010052 const char *name, unsigned int namesz,
53 enum afs_cell_trace reason)
David Howells989782d2017-11-02 15:27:50 +000054{
55 struct afs_cell *cell = NULL;
56 struct rb_node *p;
David Howells92e3cc92020-10-09 14:11:58 +010057 int n;
David Howells989782d2017-11-02 15:27:50 +000058
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
David Howells92e3cc92020-10-09 14:11:58 +010066 if (!name) {
67 cell = net->ws_cell;
68 if (!cell)
69 return ERR_PTR(-EDESTADDRREQ);
David Howells88c853c2019-07-23 11:24:59 +010070 goto found;
David Howells92e3cc92020-10-09 14:11:58 +010071 }
David Howells989782d2017-11-02 15:27:50 +000072
David Howells92e3cc92020-10-09 14:11:58 +010073 p = net->cells.rb_node;
74 while (p) {
75 cell = rb_entry(p, struct afs_cell, net_node);
David Howells989782d2017-11-02 15:27:50 +000076
David Howells92e3cc92020-10-09 14:11:58 +010077 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 }
David Howells989782d2017-11-02 15:27:50 +000088
David Howells92e3cc92020-10-09 14:11:58 +010089 return ERR_PTR(-ENOENT);
David Howells989782d2017-11-02 15:27:50 +000090
David Howells92e3cc92020-10-09 14:11:58 +010091found:
David Howellsdca54a72020-10-13 20:51:59 +010092 return afs_use_cell(cell, reason);
David Howells92e3cc92020-10-09 14:11:58 +010093}
David Howells989782d2017-11-02 15:27:50 +000094
David Howells88c853c2019-07-23 11:24:59 +010095/*
96 * Look up and get an activation reference on a cell record.
97 */
David Howells92e3cc92020-10-09 14:11:58 +010098struct afs_cell *afs_find_cell(struct afs_net *net,
David Howellsdca54a72020-10-13 20:51:59 +010099 const char *name, unsigned int namesz,
100 enum afs_cell_trace reason)
David Howells92e3cc92020-10-09 14:11:58 +0100101{
102 struct afs_cell *cell;
David Howells989782d2017-11-02 15:27:50 +0000103
David Howells92e3cc92020-10-09 14:11:58 +0100104 down_read(&net->cells_lock);
David Howellsdca54a72020-10-13 20:51:59 +0100105 cell = afs_find_cell_locked(net, name, namesz, reason);
David Howells92e3cc92020-10-09 14:11:58 +0100106 up_read(&net->cells_lock);
107 return cell;
David Howells989782d2017-11-02 15:27:50 +0000108}
109
110/*
111 * Set up a cell record and fill in its name, VL server address list and
David Howells00d3b7a2007-04-26 15:57:07 -0700112 * allocate an anonymous key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
David Howells989782d2017-11-02 15:27:50 +0000114static struct afs_cell *afs_alloc_cell(struct afs_net *net,
115 const char *name, unsigned int namelen,
David Howells0a5143f2018-10-20 00:57:57 +0100116 const char *addresses)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
David Howellsca1cbbd2019-05-07 15:30:34 +0100118 struct afs_vlserver_list *vllist;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct afs_cell *cell;
David Howells989782d2017-11-02 15:27:50 +0000120 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
David Howells989782d2017-11-02 15:27:50 +0000122 ASSERT(name);
123 if (namelen == 0)
124 return ERR_PTR(-EINVAL);
Wang Lei07567a52010-08-04 15:16:38 +0100125 if (namelen > AFS_MAXCELLNAME) {
126 _leave(" = -ENAMETOOLONG");
David Howells00d3b7a2007-04-26 15:57:07 -0700127 return ERR_PTR(-ENAMETOOLONG);
Wang Lei07567a52010-08-04 15:16:38 +0100128 }
David Howellsa45ea482020-01-26 01:02:53 +0000129
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] == '.')
David Howells37ab6362018-04-06 14:17:23 +0100134 return ERR_PTR(-EINVAL);
David Howellsa45ea482020-01-26 01:02:53 +0000135 for (i = 0; i < namelen; i++) {
136 char ch = name[i];
137 if (!isprint(ch) || ch == '/' || ch == '@')
138 return ERR_PTR(-EINVAL);
139 }
David Howells00d3b7a2007-04-26 15:57:07 -0700140
David Howells0a5143f2018-10-20 00:57:57 +0100141 _enter("%*.*s,%s", namelen, namelen, name, addresses);
David Howells989782d2017-11-02 15:27:50 +0000142
143 cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!cell) {
145 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -0700146 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
David Howells719fdd32020-06-24 17:00:24 +0100149 cell->name = kmalloc(namelen + 1, GFP_KERNEL);
150 if (!cell->name) {
151 kfree(cell);
152 return ERR_PTR(-ENOMEM);
153 }
154
David Howellsf044c882017-11-02 15:27:45 +0000155 cell->net = net;
David Howells989782d2017-11-02 15:27:50 +0000156 cell->name_len = namelen;
157 for (i = 0; i < namelen; i++)
158 cell->name[i] = tolower(name[i]);
David Howells719fdd32020-06-24 17:00:24 +0100159 cell->name[i] = 0;
David Howells989782d2017-11-02 15:27:50 +0000160
David Howells88c853c2019-07-23 11:24:59 +0100161 atomic_set(&cell->ref, 1);
162 atomic_set(&cell->active, 0);
163 INIT_WORK(&cell->manager, afs_manage_cell_work);
David Howells20325962020-04-30 01:03:49 +0100164 cell->volumes = RB_ROOT;
165 INIT_HLIST_HEAD(&cell->proc_volumes);
166 seqlock_init(&cell->volume_lock);
167 cell->fs_servers = RB_ROOT;
168 seqlock_init(&cell->fs_lock);
David Howells6e0e99d2021-09-02 16:43:10 +0100169 INIT_LIST_HEAD(&cell->fs_open_mmaps);
170 init_rwsem(&cell->fs_open_mmaps_lock);
David Howells0a5143f2018-10-20 00:57:57 +0100171 rwlock_init(&cell->vl_servers_lock);
David Howells8a070a92020-04-25 10:26:02 +0100172 cell->flags = (1 << AFS_CELL_FL_CHECK_ALIAS);
David Howells4d9df982017-11-02 15:27:47 +0000173
David Howellsca1cbbd2019-05-07 15:30:34 +0100174 /* Provide a VL server list, filling it in if we were given a list of
175 * addresses to use.
David Howells989782d2017-11-02 15:27:50 +0000176 */
David Howells0a5143f2018-10-20 00:57:57 +0100177 if (addresses) {
David Howells0a5143f2018-10-20 00:57:57 +0100178 vllist = afs_parse_text_addrs(net,
179 addresses, strlen(addresses), ':',
180 VL_SERVICE, AFS_VL_PORT);
181 if (IS_ERR(vllist)) {
182 ret = PTR_ERR(vllist);
David Howells8b2a4642017-11-02 15:27:50 +0000183 goto parse_failed;
184 }
David Howells989782d2017-11-02 15:27:50 +0000185
David Howellsd5c32c82019-05-07 15:06:36 +0100186 vllist->source = DNS_RECORD_FROM_CONFIG;
187 vllist->status = DNS_LOOKUP_NOT_DONE;
David Howells989782d2017-11-02 15:27:50 +0000188 cell->dns_expiry = TIME64_MAX;
David Howellsded2f4c2018-10-20 00:57:57 +0100189 } else {
David Howellsca1cbbd2019-05-07 15:30:34 +0100190 ret = -ENOMEM;
191 vllist = afs_alloc_vlserver_list(0);
192 if (!vllist)
193 goto error;
David Howellsd5c32c82019-05-07 15:06:36 +0100194 vllist->source = DNS_RECORD_UNAVAILABLE;
195 vllist->status = DNS_LOOKUP_NOT_DONE;
David Howellsded2f4c2018-10-20 00:57:57 +0100196 cell->dns_expiry = ktime_get_real_seconds();
Wang Lei07567a52010-08-04 15:16:38 +0100197 }
198
David Howellsca1cbbd2019-05-07 15:30:34 +0100199 rcu_assign_pointer(cell->vl_servers, vllist);
200
David Howellsd5c32c82019-05-07 15:06:36 +0100201 cell->dns_source = vllist->source;
202 cell->dns_status = vllist->status;
203 smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
David Howells88c853c2019-07-23 11:24:59 +0100204 atomic_inc(&net->cells_outstanding);
David Howellsdca54a72020-10-13 20:51:59 +0100205 cell->debug_id = atomic_inc_return(&cell_debug_id);
206 trace_afs_cell(cell->debug_id, 1, 0, afs_cell_trace_alloc);
David Howellsd5c32c82019-05-07 15:06:36 +0100207
David Howells00d3b7a2007-04-26 15:57:07 -0700208 _leave(" = %p", cell);
209 return cell;
210
David Howells8b2a4642017-11-02 15:27:50 +0000211parse_failed:
212 if (ret == -EINVAL)
213 printk(KERN_ERR "kAFS: bad VL server IP address\n");
David Howellsca1cbbd2019-05-07 15:30:34 +0100214error:
David Howells719fdd32020-06-24 17:00:24 +0100215 kfree(cell->name);
David Howells00d3b7a2007-04-26 15:57:07 -0700216 kfree(cell);
217 _leave(" = %d", ret);
218 return ERR_PTR(ret);
219}
220
221/*
David Howells989782d2017-11-02 15:27:50 +0000222 * afs_lookup_cell - Look up or create a cell record.
David Howellsf044c882017-11-02 15:27:45 +0000223 * @net: The network namespace
David Howells989782d2017-11-02 15:27:50 +0000224 * @name: The name of the cell.
225 * @namesz: The strlen of the cell name.
226 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
227 * @excl: T if an error should be given if the cell name already exists.
228 *
229 * Look up a cell record by name and query the DNS for VL server addresses if
230 * needed. Note that that actual DNS query is punted off to the manager thread
231 * so that this function can return immediately if interrupted whilst allowing
232 * cell records to be shared even if not yet fully constructed.
David Howells00d3b7a2007-04-26 15:57:07 -0700233 */
David Howells989782d2017-11-02 15:27:50 +0000234struct afs_cell *afs_lookup_cell(struct afs_net *net,
235 const char *name, unsigned int namesz,
236 const char *vllist, bool excl)
David Howells00d3b7a2007-04-26 15:57:07 -0700237{
David Howells989782d2017-11-02 15:27:50 +0000238 struct afs_cell *cell, *candidate, *cursor;
239 struct rb_node *parent, **pp;
David Howellsd5c32c82019-05-07 15:06:36 +0100240 enum afs_cell_state state;
David Howells989782d2017-11-02 15:27:50 +0000241 int ret, n;
David Howells00d3b7a2007-04-26 15:57:07 -0700242
David Howells989782d2017-11-02 15:27:50 +0000243 _enter("%s,%s", name, vllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700244
David Howells989782d2017-11-02 15:27:50 +0000245 if (!excl) {
David Howellsdca54a72020-10-13 20:51:59 +0100246 cell = afs_find_cell(net, name, namesz, afs_cell_trace_use_lookup);
Gustavo A. R. Silva68327952017-11-17 16:40:32 -0600247 if (!IS_ERR(cell))
David Howells989782d2017-11-02 15:27:50 +0000248 goto wait_for_cell;
David Howells00d3b7a2007-04-26 15:57:07 -0700249 }
250
David Howells989782d2017-11-02 15:27:50 +0000251 /* Assume we're probably going to create a cell and preallocate and
252 * mostly set up a candidate record. We can then use this to stash the
253 * name, the net namespace and VL server addresses.
254 *
255 * We also want to do this before we hold any locks as it may involve
256 * upcalling to userspace to make DNS queries.
257 */
258 candidate = afs_alloc_cell(net, name, namesz, vllist);
259 if (IS_ERR(candidate)) {
260 _leave(" = %ld", PTR_ERR(candidate));
261 return candidate;
262 }
263
264 /* Find the insertion point and check to see if someone else added a
265 * cell whilst we were allocating.
266 */
David Howells92e3cc92020-10-09 14:11:58 +0100267 down_write(&net->cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000268
269 pp = &net->cells.rb_node;
270 parent = NULL;
271 while (*pp) {
272 parent = *pp;
273 cursor = rb_entry(parent, struct afs_cell, net_node);
274
275 n = strncasecmp(cursor->name, name,
276 min_t(size_t, cursor->name_len, namesz));
277 if (n == 0)
278 n = cursor->name_len - namesz;
279 if (n < 0)
280 pp = &(*pp)->rb_left;
281 else if (n > 0)
282 pp = &(*pp)->rb_right;
283 else
284 goto cell_already_exists;
285 }
286
287 cell = candidate;
288 candidate = NULL;
David Howells88c853c2019-07-23 11:24:59 +0100289 atomic_set(&cell->active, 2);
David Howellsdca54a72020-10-13 20:51:59 +0100290 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref), 2, afs_cell_trace_insert);
David Howells989782d2017-11-02 15:27:50 +0000291 rb_link_node_rcu(&cell->net_node, parent, pp);
292 rb_insert_color(&cell->net_node, &net->cells);
David Howells92e3cc92020-10-09 14:11:58 +0100293 up_write(&net->cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000294
David Howellsdca54a72020-10-13 20:51:59 +0100295 afs_queue_cell(cell, afs_cell_trace_get_queue_new);
David Howells989782d2017-11-02 15:27:50 +0000296
297wait_for_cell:
David Howellsdca54a72020-10-13 20:51:59 +0100298 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref), atomic_read(&cell->active),
299 afs_cell_trace_wait);
David Howells989782d2017-11-02 15:27:50 +0000300 _debug("wait_for_cell");
David Howellsd5c32c82019-05-07 15:06:36 +0100301 wait_var_event(&cell->state,
302 ({
303 state = smp_load_acquire(&cell->state); /* vs error */
David Howells1d0e8502020-10-16 13:21:14 +0100304 state == AFS_CELL_ACTIVE || state == AFS_CELL_REMOVED;
David Howellsd5c32c82019-05-07 15:06:36 +0100305 }));
David Howells989782d2017-11-02 15:27:50 +0000306
David Howellsd5c32c82019-05-07 15:06:36 +0100307 /* Check the state obtained from the wait check. */
David Howells1d0e8502020-10-16 13:21:14 +0100308 if (state == AFS_CELL_REMOVED) {
David Howells989782d2017-11-02 15:27:50 +0000309 ret = cell->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto error;
David Howells989782d2017-11-02 15:27:50 +0000311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
David Howells989782d2017-11-02 15:27:50 +0000313 _leave(" = %p [cell]", cell);
David Howells08e0e7c2007-04-26 15:55:03 -0700314 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
David Howells989782d2017-11-02 15:27:50 +0000316cell_already_exists:
317 _debug("cell exists");
318 cell = cursor;
319 if (excl) {
320 ret = -EEXIST;
321 } else {
David Howellsdca54a72020-10-13 20:51:59 +0100322 afs_use_cell(cursor, afs_cell_trace_use_lookup);
David Howells989782d2017-11-02 15:27:50 +0000323 ret = 0;
wangleibec5eb62010-08-11 09:38:04 +0100324 }
David Howells92e3cc92020-10-09 14:11:58 +0100325 up_write(&net->cells_lock);
David Howells88c853c2019-07-23 11:24:59 +0100326 if (candidate)
David Howellsdca54a72020-10-13 20:51:59 +0100327 afs_put_cell(candidate, afs_cell_trace_put_candidate);
David Howells989782d2017-11-02 15:27:50 +0000328 if (ret == 0)
329 goto wait_for_cell;
David Howells8b2a4642017-11-02 15:27:50 +0000330 goto error_noput;
David Howells989782d2017-11-02 15:27:50 +0000331error:
David Howellsdca54a72020-10-13 20:51:59 +0100332 afs_unuse_cell(net, cell, afs_cell_trace_unuse_lookup);
David Howells8b2a4642017-11-02 15:27:50 +0000333error_noput:
David Howells989782d2017-11-02 15:27:50 +0000334 _leave(" = %d [error]", ret);
335 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700336}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/*
David Howells08e0e7c2007-04-26 15:55:03 -0700339 * set the root cell information
340 * - can be called with a module parameter string
341 * - can be called from a write to /proc/fs/afs/rootcell
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 */
David Howells989782d2017-11-02 15:27:50 +0000343int afs_cell_init(struct afs_net *net, const char *rootcell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 struct afs_cell *old_root, *new_root;
David Howells989782d2017-11-02 15:27:50 +0000346 const char *cp, *vllist;
347 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 _enter("");
350
351 if (!rootcell) {
352 /* module is loaded with no parameters, or built statically.
353 * - in the future we might initialize cell DB here.
354 */
David Howells08e0e7c2007-04-26 15:55:03 -0700355 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return 0;
357 }
358
359 cp = strchr(rootcell, ':');
David Howells989782d2017-11-02 15:27:50 +0000360 if (!cp) {
Wang Lei07567a52010-08-04 15:16:38 +0100361 _debug("kAFS: no VL server IP addresses specified");
David Howells989782d2017-11-02 15:27:50 +0000362 vllist = NULL;
363 len = strlen(rootcell);
364 } else {
365 vllist = cp + 1;
366 len = cp - rootcell;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 /* allocate a cell record for the root cell */
David Howells989782d2017-11-02 15:27:50 +0000370 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
David Howells08e0e7c2007-04-26 15:55:03 -0700371 if (IS_ERR(new_root)) {
372 _leave(" = %ld", PTR_ERR(new_root));
373 return PTR_ERR(new_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
David Howells17814ae2018-04-09 21:12:31 +0100376 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
David Howellsdca54a72020-10-13 20:51:59 +0100377 afs_use_cell(new_root, afs_cell_trace_use_pin);
David Howells989782d2017-11-02 15:27:50 +0000378
David Howells08e0e7c2007-04-26 15:55:03 -0700379 /* install the new cell */
David Howells92e3cc92020-10-09 14:11:58 +0100380 down_write(&net->cells_lock);
David Howellsdca54a72020-10-13 20:51:59 +0100381 afs_see_cell(new_root, afs_cell_trace_see_ws);
David Howells92e3cc92020-10-09 14:11:58 +0100382 old_root = net->ws_cell;
383 net->ws_cell = new_root;
384 up_write(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
David Howellsdca54a72020-10-13 20:51:59 +0100386 afs_unuse_cell(net, old_root, afs_cell_trace_unuse_ws);
David Howells08e0e7c2007-04-26 15:55:03 -0700387 _leave(" = 0");
388 return 0;
David Howellsec268152007-04-26 15:49:28 -0700389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
David Howells989782d2017-11-02 15:27:50 +0000392 * Update a cell's VL server address list from the DNS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
David Howellsd5c32c82019-05-07 15:06:36 +0100394static int afs_update_cell(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
David Howellsd5c32c82019-05-07 15:06:36 +0100396 struct afs_vlserver_list *vllist, *old = NULL, *p;
David Howellsded2f4c2018-10-20 00:57:57 +0100397 unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
398 unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
399 time64_t now, expiry = 0;
David Howellsd5c32c82019-05-07 15:06:36 +0100400 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David Howells989782d2017-11-02 15:27:50 +0000402 _enter("%s", cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
David Howells0a5143f2018-10-20 00:57:57 +0100404 vllist = afs_dns_query(cell, &expiry);
David Howellsd5c32c82019-05-07 15:06:36 +0100405 if (IS_ERR(vllist)) {
406 ret = PTR_ERR(vllist);
407
408 _debug("%s: fail %d", cell->name, ret);
409 if (ret == -ENOMEM)
410 goto out_wake;
411
412 ret = -ENOMEM;
413 vllist = afs_alloc_vlserver_list(0);
414 if (!vllist)
415 goto out_wake;
416
417 switch (ret) {
418 case -ENODATA:
419 case -EDESTADDRREQ:
420 vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
421 break;
422 case -EAGAIN:
423 case -ECONNREFUSED:
424 vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
425 break;
426 default:
427 vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
428 break;
429 }
430 }
431
432 _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
433 cell->dns_status = vllist->status;
David Howellsded2f4c2018-10-20 00:57:57 +0100434
435 now = ktime_get_real_seconds();
436 if (min_ttl > max_ttl)
437 max_ttl = min_ttl;
438 if (expiry < now + min_ttl)
439 expiry = now + min_ttl;
440 else if (expiry > now + max_ttl)
441 expiry = now + max_ttl;
442
David Howellsd5c32c82019-05-07 15:06:36 +0100443 _debug("%s: status %d", cell->name, vllist->status);
444 if (vllist->source == DNS_RECORD_UNAVAILABLE) {
445 switch (vllist->status) {
446 case DNS_LOOKUP_GOT_NOT_FOUND:
David Howellsded2f4c2018-10-20 00:57:57 +0100447 /* The DNS said that the cell does not exist or there
448 * weren't any addresses to be had.
449 */
David Howellsded2f4c2018-10-20 00:57:57 +0100450 cell->dns_expiry = expiry;
David Howells8b2a4642017-11-02 15:27:50 +0000451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
David Howellsd5c32c82019-05-07 15:06:36 +0100453 case DNS_LOOKUP_BAD:
454 case DNS_LOOKUP_GOT_LOCAL_FAILURE:
455 case DNS_LOOKUP_GOT_TEMP_FAILURE:
456 case DNS_LOOKUP_GOT_NS_FAILURE:
David Howells8b2a4642017-11-02 15:27:50 +0000457 default:
David Howellsded2f4c2018-10-20 00:57:57 +0100458 cell->dns_expiry = now + 10;
David Howells8b2a4642017-11-02 15:27:50 +0000459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
David Howells8b2a4642017-11-02 15:27:50 +0000461 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000462 cell->dns_expiry = expiry;
David Howells8b2a4642017-11-02 15:27:50 +0000463 }
464
David Howellsd5c32c82019-05-07 15:06:36 +0100465 /* Replace the VL server list if the new record has servers or the old
466 * record doesn't.
467 */
468 write_lock(&cell->vl_servers_lock);
469 p = rcu_dereference_protected(cell->vl_servers, true);
470 if (vllist->nr_servers > 0 || p->nr_servers == 0) {
471 rcu_assign_pointer(cell->vl_servers, vllist);
472 cell->dns_source = vllist->source;
473 old = p;
474 }
475 write_unlock(&cell->vl_servers_lock);
476 afs_put_vlserverlist(cell->net, old);
wangleibec5eb62010-08-11 09:38:04 +0100477
David Howellsd5c32c82019-05-07 15:06:36 +0100478out_wake:
479 smp_store_release(&cell->dns_lookup_count,
480 cell->dns_lookup_count + 1); /* vs source/status */
481 wake_up_var(&cell->dns_lookup_count);
482 _leave(" = %d", ret);
483 return ret;
David Howellsec268152007-04-26 15:49:28 -0700484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
David Howells989782d2017-11-02 15:27:50 +0000487 * Destroy a cell record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 */
David Howells989782d2017-11-02 15:27:50 +0000489static void afs_cell_destroy(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
David Howells989782d2017-11-02 15:27:50 +0000491 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
David Howells88c853c2019-07-23 11:24:59 +0100492 struct afs_net *net = cell->net;
493 int u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David Howells989782d2017-11-02 15:27:50 +0000495 _enter("%p{%s}", cell, cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
David Howells88c853c2019-07-23 11:24:59 +0100497 u = atomic_read(&cell->ref);
498 ASSERTCMP(u, ==, 0);
David Howellsdca54a72020-10-13 20:51:59 +0100499 trace_afs_cell(cell->debug_id, u, atomic_read(&cell->active), afs_cell_trace_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
David Howells88c853c2019-07-23 11:24:59 +0100501 afs_put_vlserverlist(net, rcu_access_pointer(cell->vl_servers));
David Howellsdca54a72020-10-13 20:51:59 +0100502 afs_unuse_cell(net, cell->alias_of, afs_cell_trace_unuse_alias);
David Howells00d3b7a2007-04-26 15:57:07 -0700503 key_put(cell->anonymous_key);
David Howells719fdd32020-06-24 17:00:24 +0100504 kfree(cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 kfree(cell);
506
David Howells88c853c2019-07-23 11:24:59 +0100507 afs_dec_cells_outstanding(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511/*
David Howells989782d2017-11-02 15:27:50 +0000512 * Queue the cell manager.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
David Howells989782d2017-11-02 15:27:50 +0000514static void afs_queue_cell_manager(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
David Howells989782d2017-11-02 15:27:50 +0000516 int outstanding = atomic_inc_return(&net->cells_outstanding);
517
518 _enter("%d", outstanding);
519
520 if (!queue_work(afs_wq, &net->cells_manager))
521 afs_dec_cells_outstanding(net);
522}
523
524/*
525 * Cell management timer. We have an increment on cells_outstanding that we
526 * need to pass along to the work item.
527 */
528void afs_cells_timer(struct timer_list *timer)
529{
530 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
531
532 _enter("");
533 if (!queue_work(afs_wq, &net->cells_manager))
534 afs_dec_cells_outstanding(net);
535}
536
537/*
David Howells8b2a4642017-11-02 15:27:50 +0000538 * Get a reference on a cell record.
539 */
David Howellsdca54a72020-10-13 20:51:59 +0100540struct afs_cell *afs_get_cell(struct afs_cell *cell, enum afs_cell_trace reason)
David Howells8b2a4642017-11-02 15:27:50 +0000541{
David Howellsdca54a72020-10-13 20:51:59 +0100542 int u;
543
David Howells88c853c2019-07-23 11:24:59 +0100544 if (atomic_read(&cell->ref) <= 0)
545 BUG();
546
David Howellsdca54a72020-10-13 20:51:59 +0100547 u = atomic_inc_return(&cell->ref);
548 trace_afs_cell(cell->debug_id, u, atomic_read(&cell->active), reason);
David Howells8b2a4642017-11-02 15:27:50 +0000549 return cell;
550}
551
552/*
David Howells989782d2017-11-02 15:27:50 +0000553 * Drop a reference on a cell record.
554 */
David Howellsdca54a72020-10-13 20:51:59 +0100555void afs_put_cell(struct afs_cell *cell, enum afs_cell_trace reason)
David Howells88c853c2019-07-23 11:24:59 +0100556{
557 if (cell) {
David Howellsdca54a72020-10-13 20:51:59 +0100558 unsigned int debug_id = cell->debug_id;
David Howells88c853c2019-07-23 11:24:59 +0100559 unsigned int u, a;
560
David Howellsdca54a72020-10-13 20:51:59 +0100561 a = atomic_read(&cell->active);
David Howells88c853c2019-07-23 11:24:59 +0100562 u = atomic_dec_return(&cell->ref);
David Howellsdca54a72020-10-13 20:51:59 +0100563 trace_afs_cell(debug_id, u, a, reason);
David Howells88c853c2019-07-23 11:24:59 +0100564 if (u == 0) {
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 */
David Howellsdca54a72020-10-13 20:51:59 +0100575struct afs_cell *afs_use_cell(struct afs_cell *cell, enum afs_cell_trace reason)
David Howells88c853c2019-07-23 11:24:59 +0100576{
David Howellsdca54a72020-10-13 20:51:59 +0100577 int u, a;
578
David Howells88c853c2019-07-23 11:24:59 +0100579 if (atomic_read(&cell->ref) <= 0)
580 BUG();
581
David Howellsdca54a72020-10-13 20:51:59 +0100582 u = atomic_read(&cell->ref);
583 a = atomic_inc_return(&cell->active);
584 trace_afs_cell(cell->debug_id, u, a, reason);
David Howells88c853c2019-07-23 11:24:59 +0100585 return cell;
586}
587
588/*
589 * Record a cell becoming less active. When the active counter reaches 1, it
590 * is scheduled for destruction, but may get reactivated.
591 */
David Howellsdca54a72020-10-13 20:51:59 +0100592void afs_unuse_cell(struct afs_net *net, struct afs_cell *cell, enum afs_cell_trace reason)
David Howells989782d2017-11-02 15:27:50 +0000593{
David Howellsacc080d2020-10-27 10:42:56 +0000594 unsigned int debug_id;
David Howells989782d2017-11-02 15:27:50 +0000595 time64_t now, expire_delay;
David Howellsdca54a72020-10-13 20:51:59 +0100596 int u, a;
David Howells989782d2017-11-02 15:27:50 +0000597
598 if (!cell)
599 return;
600
601 _enter("%s", cell->name);
602
603 now = ktime_get_real_seconds();
604 cell->last_inactive = now;
605 expire_delay = 0;
David Howellsd5c32c82019-05-07 15:06:36 +0100606 if (cell->vl_servers->nr_servers)
David Howells989782d2017-11-02 15:27:50 +0000607 expire_delay = afs_cell_gc_delay;
608
David Howellsacc080d2020-10-27 10:42:56 +0000609 debug_id = cell->debug_id;
David Howellsdca54a72020-10-13 20:51:59 +0100610 u = atomic_read(&cell->ref);
David Howells88c853c2019-07-23 11:24:59 +0100611 a = atomic_dec_return(&cell->active);
David Howellsdca54a72020-10-13 20:51:59 +0100612 trace_afs_cell(debug_id, u, a, reason);
David Howells88c853c2019-07-23 11:24:59 +0100613 WARN_ON(a == 0);
614 if (a == 1)
615 /* 'cell' may now be garbage collected. */
616 afs_set_cell_timer(net, expire_delay);
617}
David Howells989782d2017-11-02 15:27:50 +0000618
David Howells88c853c2019-07-23 11:24:59 +0100619/*
David Howellsdca54a72020-10-13 20:51:59 +0100620 * Note that a cell has been seen.
621 */
622void afs_see_cell(struct afs_cell *cell, enum afs_cell_trace reason)
623{
624 int u, a;
625
626 u = atomic_read(&cell->ref);
627 a = atomic_read(&cell->active);
628 trace_afs_cell(cell->debug_id, u, a, reason);
629}
630
631/*
David Howells88c853c2019-07-23 11:24:59 +0100632 * Queue a cell for management, giving the workqueue a ref to hold.
633 */
David Howellsdca54a72020-10-13 20:51:59 +0100634void afs_queue_cell(struct afs_cell *cell, enum afs_cell_trace reason)
David Howells88c853c2019-07-23 11:24:59 +0100635{
David Howellsdca54a72020-10-13 20:51:59 +0100636 afs_get_cell(cell, reason);
David Howells88c853c2019-07-23 11:24:59 +0100637 if (!queue_work(afs_wq, &cell->manager))
David Howellsdca54a72020-10-13 20:51:59 +0100638 afs_put_cell(cell, afs_cell_trace_put_queue_fail);
David Howells989782d2017-11-02 15:27:50 +0000639}
640
641/*
642 * Allocate a key to use as a placeholder for anonymous user security.
643 */
644static int afs_alloc_anon_key(struct afs_cell *cell)
645{
646 struct key *key;
647 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
648
649 /* Create a key to represent an anonymous user. */
650 memcpy(keyname, "afs@", 4);
651 dp = keyname + 4;
652 cp = cell->name;
653 do {
654 *dp++ = tolower(*cp);
655 } while (*cp++);
656
657 key = rxrpc_get_null_key(keyname);
658 if (IS_ERR(key))
659 return PTR_ERR(key);
660
661 cell->anonymous_key = key;
662
663 _debug("anon key %p{%x}",
664 cell->anonymous_key, key_serial(cell->anonymous_key));
665 return 0;
666}
667
668/*
669 * Activate a cell.
670 */
671static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
672{
David Howells6b3944e2018-10-11 22:45:49 +0100673 struct hlist_node **p;
674 struct afs_cell *pcell;
David Howells989782d2017-11-02 15:27:50 +0000675 int ret;
676
677 if (!cell->anonymous_key) {
678 ret = afs_alloc_anon_key(cell);
679 if (ret < 0)
680 return ret;
681 }
682
David Howells5b86d4f2018-05-18 11:46:15 +0100683 ret = afs_proc_cell_setup(cell);
David Howells989782d2017-11-02 15:27:50 +0000684 if (ret < 0)
685 return ret;
David Howells0da0b7f2018-06-15 15:19:22 +0100686
687 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100688 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
689 pcell = hlist_entry(*p, struct afs_cell, proc_link);
690 if (strcmp(cell->name, pcell->name) < 0)
691 break;
692 }
693
694 cell->proc_link.pprev = p;
695 cell->proc_link.next = *p;
696 rcu_assign_pointer(*p, &cell->proc_link.next);
697 if (cell->proc_link.next)
698 cell->proc_link.next->pprev = &cell->proc_link.next;
699
David Howells0da0b7f2018-06-15 15:19:22 +0100700 afs_dynroot_mkdir(net, cell);
701 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000702 return 0;
703}
704
705/*
706 * Deactivate a cell.
707 */
708static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
709{
710 _enter("%s", cell->name);
711
David Howells5b86d4f2018-05-18 11:46:15 +0100712 afs_proc_cell_remove(cell);
David Howells989782d2017-11-02 15:27:50 +0000713
David Howells0da0b7f2018-06-15 15:19:22 +0100714 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100715 hlist_del_rcu(&cell->proc_link);
David Howells0da0b7f2018-06-15 15:19:22 +0100716 afs_dynroot_rmdir(net, cell);
717 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000718
David Howells989782d2017-11-02 15:27:50 +0000719 _leave("");
720}
721
722/*
723 * Manage a cell record, initialising and destroying it, maintaining its DNS
724 * records.
725 */
David Howells88c853c2019-07-23 11:24:59 +0100726static void afs_manage_cell(struct afs_cell *cell)
David Howells989782d2017-11-02 15:27:50 +0000727{
David Howells989782d2017-11-02 15:27:50 +0000728 struct afs_net *net = cell->net;
David Howells88c853c2019-07-23 11:24:59 +0100729 int ret, active;
David Howells989782d2017-11-02 15:27:50 +0000730
731 _enter("%s", cell->name);
732
733again:
734 _debug("state %u", cell->state);
735 switch (cell->state) {
736 case AFS_CELL_INACTIVE:
737 case AFS_CELL_FAILED:
David Howells92e3cc92020-10-09 14:11:58 +0100738 down_write(&net->cells_lock);
David Howells88c853c2019-07-23 11:24:59 +0100739 active = 1;
David Howells1d0e8502020-10-16 13:21:14 +0100740 if (atomic_try_cmpxchg_relaxed(&cell->active, &active, 0)) {
David Howells989782d2017-11-02 15:27:50 +0000741 rb_erase(&cell->net_node, &net->cells);
David Howellsdca54a72020-10-13 20:51:59 +0100742 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref), 0,
743 afs_cell_trace_unuse_delete);
David Howells1d0e8502020-10-16 13:21:14 +0100744 smp_store_release(&cell->state, AFS_CELL_REMOVED);
David Howells88c853c2019-07-23 11:24:59 +0100745 }
David Howells92e3cc92020-10-09 14:11:58 +0100746 up_write(&net->cells_lock);
David Howells1d0e8502020-10-16 13:21:14 +0100747 if (cell->state == AFS_CELL_REMOVED) {
748 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000749 goto final_destruction;
David Howells1d0e8502020-10-16 13:21:14 +0100750 }
David Howells989782d2017-11-02 15:27:50 +0000751 if (cell->state == AFS_CELL_FAILED)
752 goto done;
David Howellsd5c32c82019-05-07 15:06:36 +0100753 smp_store_release(&cell->state, AFS_CELL_UNSET);
754 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000755 goto again;
756
757 case AFS_CELL_UNSET:
David Howellsd5c32c82019-05-07 15:06:36 +0100758 smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
759 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000760 goto again;
761
762 case AFS_CELL_ACTIVATING:
763 ret = afs_activate_cell(net, cell);
764 if (ret < 0)
765 goto activation_failed;
766
David Howellsd5c32c82019-05-07 15:06:36 +0100767 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
768 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000769 goto again;
770
771 case AFS_CELL_ACTIVE:
David Howells88c853c2019-07-23 11:24:59 +0100772 if (atomic_read(&cell->active) > 1) {
David Howellsd5c32c82019-05-07 15:06:36 +0100773 if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
774 ret = afs_update_cell(cell);
775 if (ret < 0)
776 cell->error = ret;
777 }
David Howells989782d2017-11-02 15:27:50 +0000778 goto done;
779 }
David Howellsd5c32c82019-05-07 15:06:36 +0100780 smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
781 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000782 goto again;
783
784 case AFS_CELL_DEACTIVATING:
David Howells88c853c2019-07-23 11:24:59 +0100785 if (atomic_read(&cell->active) > 1)
David Howells989782d2017-11-02 15:27:50 +0000786 goto reverse_deactivation;
787 afs_deactivate_cell(net, cell);
David Howellsd5c32c82019-05-07 15:06:36 +0100788 smp_store_release(&cell->state, AFS_CELL_INACTIVE);
789 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000790 goto again;
791
David Howells1d0e8502020-10-16 13:21:14 +0100792 case AFS_CELL_REMOVED:
793 goto done;
794
David Howells989782d2017-11-02 15:27:50 +0000795 default:
796 break;
797 }
798 _debug("bad state %u", cell->state);
799 BUG(); /* Unhandled state */
800
801activation_failed:
802 cell->error = ret;
803 afs_deactivate_cell(net, cell);
804
David Howellsd5c32c82019-05-07 15:06:36 +0100805 smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
806 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000807 goto again;
808
809reverse_deactivation:
David Howellsd5c32c82019-05-07 15:06:36 +0100810 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
811 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000812 _leave(" [deact->act]");
813 return;
814
815done:
816 _leave(" [done %u]", cell->state);
817 return;
818
819final_destruction:
David Howells88c853c2019-07-23 11:24:59 +0100820 /* The root volume is pinning the cell */
821 afs_put_volume(cell->net, cell->root_volume, afs_volume_trace_put_cell_root);
822 cell->root_volume = NULL;
David Howellsdca54a72020-10-13 20:51:59 +0100823 afs_put_cell(cell, afs_cell_trace_put_destroy);
David Howells88c853c2019-07-23 11:24:59 +0100824}
825
826static void afs_manage_cell_work(struct work_struct *work)
827{
828 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
829
830 afs_manage_cell(cell);
David Howellsdca54a72020-10-13 20:51:59 +0100831 afs_put_cell(cell, afs_cell_trace_put_queue_work);
David Howells989782d2017-11-02 15:27:50 +0000832}
833
834/*
835 * Manage the records of cells known to a network namespace. This includes
836 * updating the DNS records and garbage collecting unused cells that were
837 * automatically added.
838 *
839 * Note that constructed cell records may only be removed from net->cells by
840 * this work item, so it is safe for this work item to stash a cursor pointing
841 * into the tree and then return to caller (provided it skips cells that are
842 * still under construction).
843 *
844 * Note also that we were given an increment on net->cells_outstanding by
845 * whoever queued us that we need to deal with before returning.
846 */
847void afs_manage_cells(struct work_struct *work)
848{
849 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
850 struct rb_node *cursor;
851 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
852 bool purging = !net->live;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 _enter("");
855
David Howells989782d2017-11-02 15:27:50 +0000856 /* Trawl the cell database looking for cells that have expired from
857 * lack of use and cells whose DNS results have expired and dispatch
858 * their managers.
859 */
David Howells92e3cc92020-10-09 14:11:58 +0100860 down_read(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
David Howells989782d2017-11-02 15:27:50 +0000862 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
863 struct afs_cell *cell =
864 rb_entry(cursor, struct afs_cell, net_node);
David Howells88c853c2019-07-23 11:24:59 +0100865 unsigned active;
David Howells989782d2017-11-02 15:27:50 +0000866 bool sched_cell = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700867
David Howells88c853c2019-07-23 11:24:59 +0100868 active = atomic_read(&cell->active);
David Howellsdca54a72020-10-13 20:51:59 +0100869 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref),
870 active, afs_cell_trace_manage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
David Howells88c853c2019-07-23 11:24:59 +0100872 ASSERTCMP(active, >=, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
David Howells989782d2017-11-02 15:27:50 +0000874 if (purging) {
David Howellsdca54a72020-10-13 20:51:59 +0100875 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags)) {
876 active = atomic_dec_return(&cell->active);
877 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref),
878 active, afs_cell_trace_unuse_pin);
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881
David Howells88c853c2019-07-23 11:24:59 +0100882 if (active == 1) {
David Howellsd5c32c82019-05-07 15:06:36 +0100883 struct afs_vlserver_list *vllist;
David Howells989782d2017-11-02 15:27:50 +0000884 time64_t expire_at = cell->last_inactive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
David Howellsd5c32c82019-05-07 15:06:36 +0100886 read_lock(&cell->vl_servers_lock);
887 vllist = rcu_dereference_protected(
888 cell->vl_servers,
889 lockdep_is_held(&cell->vl_servers_lock));
890 if (vllist->nr_servers > 0)
David Howells989782d2017-11-02 15:27:50 +0000891 expire_at += afs_cell_gc_delay;
David Howellsd5c32c82019-05-07 15:06:36 +0100892 read_unlock(&cell->vl_servers_lock);
David Howells989782d2017-11-02 15:27:50 +0000893 if (purging || expire_at <= now)
894 sched_cell = true;
895 else if (expire_at < next_manage)
896 next_manage = expire_at;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
David Howells989782d2017-11-02 15:27:50 +0000899 if (!purging) {
David Howellsd5c32c82019-05-07 15:06:36 +0100900 if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
David Howells989782d2017-11-02 15:27:50 +0000901 sched_cell = true;
David Howells989782d2017-11-02 15:27:50 +0000902 }
903
904 if (sched_cell)
David Howellsdca54a72020-10-13 20:51:59 +0100905 afs_queue_cell(cell, afs_cell_trace_get_queue_manage);
David Howells989782d2017-11-02 15:27:50 +0000906 }
907
David Howells92e3cc92020-10-09 14:11:58 +0100908 up_read(&net->cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000909
910 /* Update the timer on the way out. We have to pass an increment on
911 * cells_outstanding in the namespace that we are in to the timer or
912 * the work scheduler.
913 */
914 if (!purging && next_manage < TIME64_MAX) {
915 now = ktime_get_real_seconds();
916
917 if (next_manage - now <= 0) {
918 if (queue_work(afs_wq, &net->cells_manager))
919 atomic_inc(&net->cells_outstanding);
920 } else {
921 afs_set_cell_timer(net, next_manage - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923 }
924
David Howells989782d2017-11-02 15:27:50 +0000925 afs_dec_cells_outstanding(net);
926 _leave(" [%d]", atomic_read(&net->cells_outstanding));
927}
928
929/*
930 * Purge in-memory cell database.
931 */
932void afs_cell_purge(struct afs_net *net)
933{
934 struct afs_cell *ws;
935
936 _enter("");
937
David Howells92e3cc92020-10-09 14:11:58 +0100938 down_write(&net->cells_lock);
939 ws = net->ws_cell;
940 net->ws_cell = NULL;
941 up_write(&net->cells_lock);
David Howellsdca54a72020-10-13 20:51:59 +0100942 afs_unuse_cell(net, ws, afs_cell_trace_unuse_ws);
David Howells989782d2017-11-02 15:27:50 +0000943
944 _debug("del timer");
945 if (del_timer_sync(&net->cells_timer))
946 atomic_dec(&net->cells_outstanding);
947
948 _debug("kick mgr");
949 afs_queue_cell_manager(net);
950
951 _debug("wait");
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100952 wait_var_event(&net->cells_outstanding,
953 !atomic_read(&net->cells_outstanding));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700955}