blob: 887b673f6223022f2b9699503ef80796736f52be [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 Howells0a5143f2018-10-20 00:57:57 +0100169 rwlock_init(&cell->vl_servers_lock);
David Howells8a070a92020-04-25 10:26:02 +0100170 cell->flags = (1 << AFS_CELL_FL_CHECK_ALIAS);
David Howells4d9df982017-11-02 15:27:47 +0000171
David Howellsca1cbbd2019-05-07 15:30:34 +0100172 /* Provide a VL server list, filling it in if we were given a list of
173 * addresses to use.
David Howells989782d2017-11-02 15:27:50 +0000174 */
David Howells0a5143f2018-10-20 00:57:57 +0100175 if (addresses) {
David Howells0a5143f2018-10-20 00:57:57 +0100176 vllist = afs_parse_text_addrs(net,
177 addresses, strlen(addresses), ':',
178 VL_SERVICE, AFS_VL_PORT);
179 if (IS_ERR(vllist)) {
180 ret = PTR_ERR(vllist);
David Howells8b2a4642017-11-02 15:27:50 +0000181 goto parse_failed;
182 }
David Howells989782d2017-11-02 15:27:50 +0000183
David Howellsd5c32c82019-05-07 15:06:36 +0100184 vllist->source = DNS_RECORD_FROM_CONFIG;
185 vllist->status = DNS_LOOKUP_NOT_DONE;
David Howells989782d2017-11-02 15:27:50 +0000186 cell->dns_expiry = TIME64_MAX;
David Howellsded2f4c2018-10-20 00:57:57 +0100187 } else {
David Howellsca1cbbd2019-05-07 15:30:34 +0100188 ret = -ENOMEM;
189 vllist = afs_alloc_vlserver_list(0);
190 if (!vllist)
191 goto error;
David Howellsd5c32c82019-05-07 15:06:36 +0100192 vllist->source = DNS_RECORD_UNAVAILABLE;
193 vllist->status = DNS_LOOKUP_NOT_DONE;
David Howellsded2f4c2018-10-20 00:57:57 +0100194 cell->dns_expiry = ktime_get_real_seconds();
Wang Lei07567a52010-08-04 15:16:38 +0100195 }
196
David Howellsca1cbbd2019-05-07 15:30:34 +0100197 rcu_assign_pointer(cell->vl_servers, vllist);
198
David Howellsd5c32c82019-05-07 15:06:36 +0100199 cell->dns_source = vllist->source;
200 cell->dns_status = vllist->status;
201 smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
David Howells88c853c2019-07-23 11:24:59 +0100202 atomic_inc(&net->cells_outstanding);
David Howellsdca54a72020-10-13 20:51:59 +0100203 cell->debug_id = atomic_inc_return(&cell_debug_id);
204 trace_afs_cell(cell->debug_id, 1, 0, afs_cell_trace_alloc);
David Howellsd5c32c82019-05-07 15:06:36 +0100205
David Howells00d3b7a2007-04-26 15:57:07 -0700206 _leave(" = %p", cell);
207 return cell;
208
David Howells8b2a4642017-11-02 15:27:50 +0000209parse_failed:
210 if (ret == -EINVAL)
211 printk(KERN_ERR "kAFS: bad VL server IP address\n");
David Howellsca1cbbd2019-05-07 15:30:34 +0100212error:
David Howells719fdd32020-06-24 17:00:24 +0100213 kfree(cell->name);
David Howells00d3b7a2007-04-26 15:57:07 -0700214 kfree(cell);
215 _leave(" = %d", ret);
216 return ERR_PTR(ret);
217}
218
219/*
David Howells989782d2017-11-02 15:27:50 +0000220 * afs_lookup_cell - Look up or create a cell record.
David Howellsf044c882017-11-02 15:27:45 +0000221 * @net: The network namespace
David Howells989782d2017-11-02 15:27:50 +0000222 * @name: The name of the cell.
223 * @namesz: The strlen of the cell name.
224 * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
225 * @excl: T if an error should be given if the cell name already exists.
226 *
227 * Look up a cell record by name and query the DNS for VL server addresses if
228 * needed. Note that that actual DNS query is punted off to the manager thread
229 * so that this function can return immediately if interrupted whilst allowing
230 * cell records to be shared even if not yet fully constructed.
David Howells00d3b7a2007-04-26 15:57:07 -0700231 */
David Howells989782d2017-11-02 15:27:50 +0000232struct afs_cell *afs_lookup_cell(struct afs_net *net,
233 const char *name, unsigned int namesz,
234 const char *vllist, bool excl)
David Howells00d3b7a2007-04-26 15:57:07 -0700235{
David Howells989782d2017-11-02 15:27:50 +0000236 struct afs_cell *cell, *candidate, *cursor;
237 struct rb_node *parent, **pp;
David Howellsd5c32c82019-05-07 15:06:36 +0100238 enum afs_cell_state state;
David Howells989782d2017-11-02 15:27:50 +0000239 int ret, n;
David Howells00d3b7a2007-04-26 15:57:07 -0700240
David Howells989782d2017-11-02 15:27:50 +0000241 _enter("%s,%s", name, vllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700242
David Howells989782d2017-11-02 15:27:50 +0000243 if (!excl) {
David Howellsdca54a72020-10-13 20:51:59 +0100244 cell = afs_find_cell(net, name, namesz, afs_cell_trace_use_lookup);
Gustavo A. R. Silva68327952017-11-17 16:40:32 -0600245 if (!IS_ERR(cell))
David Howells989782d2017-11-02 15:27:50 +0000246 goto wait_for_cell;
David Howells00d3b7a2007-04-26 15:57:07 -0700247 }
248
David Howells989782d2017-11-02 15:27:50 +0000249 /* Assume we're probably going to create a cell and preallocate and
250 * mostly set up a candidate record. We can then use this to stash the
251 * name, the net namespace and VL server addresses.
252 *
253 * We also want to do this before we hold any locks as it may involve
254 * upcalling to userspace to make DNS queries.
255 */
256 candidate = afs_alloc_cell(net, name, namesz, vllist);
257 if (IS_ERR(candidate)) {
258 _leave(" = %ld", PTR_ERR(candidate));
259 return candidate;
260 }
261
262 /* Find the insertion point and check to see if someone else added a
263 * cell whilst we were allocating.
264 */
David Howells92e3cc92020-10-09 14:11:58 +0100265 down_write(&net->cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000266
267 pp = &net->cells.rb_node;
268 parent = NULL;
269 while (*pp) {
270 parent = *pp;
271 cursor = rb_entry(parent, struct afs_cell, net_node);
272
273 n = strncasecmp(cursor->name, name,
274 min_t(size_t, cursor->name_len, namesz));
275 if (n == 0)
276 n = cursor->name_len - namesz;
277 if (n < 0)
278 pp = &(*pp)->rb_left;
279 else if (n > 0)
280 pp = &(*pp)->rb_right;
281 else
282 goto cell_already_exists;
283 }
284
285 cell = candidate;
286 candidate = NULL;
David Howells88c853c2019-07-23 11:24:59 +0100287 atomic_set(&cell->active, 2);
David Howellsdca54a72020-10-13 20:51:59 +0100288 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref), 2, afs_cell_trace_insert);
David Howells989782d2017-11-02 15:27:50 +0000289 rb_link_node_rcu(&cell->net_node, parent, pp);
290 rb_insert_color(&cell->net_node, &net->cells);
David Howells92e3cc92020-10-09 14:11:58 +0100291 up_write(&net->cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000292
David Howellsdca54a72020-10-13 20:51:59 +0100293 afs_queue_cell(cell, afs_cell_trace_get_queue_new);
David Howells989782d2017-11-02 15:27:50 +0000294
295wait_for_cell:
David Howellsdca54a72020-10-13 20:51:59 +0100296 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref), atomic_read(&cell->active),
297 afs_cell_trace_wait);
David Howells989782d2017-11-02 15:27:50 +0000298 _debug("wait_for_cell");
David Howellsd5c32c82019-05-07 15:06:36 +0100299 wait_var_event(&cell->state,
300 ({
301 state = smp_load_acquire(&cell->state); /* vs error */
David Howells1d0e8502020-10-16 13:21:14 +0100302 state == AFS_CELL_ACTIVE || state == AFS_CELL_REMOVED;
David Howellsd5c32c82019-05-07 15:06:36 +0100303 }));
David Howells989782d2017-11-02 15:27:50 +0000304
David Howellsd5c32c82019-05-07 15:06:36 +0100305 /* Check the state obtained from the wait check. */
David Howells1d0e8502020-10-16 13:21:14 +0100306 if (state == AFS_CELL_REMOVED) {
David Howells989782d2017-11-02 15:27:50 +0000307 ret = cell->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 goto error;
David Howells989782d2017-11-02 15:27:50 +0000309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
David Howells989782d2017-11-02 15:27:50 +0000311 _leave(" = %p [cell]", cell);
David Howells08e0e7c2007-04-26 15:55:03 -0700312 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
David Howells989782d2017-11-02 15:27:50 +0000314cell_already_exists:
315 _debug("cell exists");
316 cell = cursor;
317 if (excl) {
318 ret = -EEXIST;
319 } else {
David Howellsdca54a72020-10-13 20:51:59 +0100320 afs_use_cell(cursor, afs_cell_trace_use_lookup);
David Howells989782d2017-11-02 15:27:50 +0000321 ret = 0;
wangleibec5eb62010-08-11 09:38:04 +0100322 }
David Howells92e3cc92020-10-09 14:11:58 +0100323 up_write(&net->cells_lock);
David Howells88c853c2019-07-23 11:24:59 +0100324 if (candidate)
David Howellsdca54a72020-10-13 20:51:59 +0100325 afs_put_cell(candidate, afs_cell_trace_put_candidate);
David Howells989782d2017-11-02 15:27:50 +0000326 if (ret == 0)
327 goto wait_for_cell;
David Howells8b2a4642017-11-02 15:27:50 +0000328 goto error_noput;
David Howells989782d2017-11-02 15:27:50 +0000329error:
David Howellsdca54a72020-10-13 20:51:59 +0100330 afs_unuse_cell(net, cell, afs_cell_trace_unuse_lookup);
David Howells8b2a4642017-11-02 15:27:50 +0000331error_noput:
David Howells989782d2017-11-02 15:27:50 +0000332 _leave(" = %d [error]", ret);
333 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700334}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/*
David Howells08e0e7c2007-04-26 15:55:03 -0700337 * set the root cell information
338 * - can be called with a module parameter string
339 * - can be called from a write to /proc/fs/afs/rootcell
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
David Howells989782d2017-11-02 15:27:50 +0000341int afs_cell_init(struct afs_net *net, const char *rootcell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 struct afs_cell *old_root, *new_root;
David Howells989782d2017-11-02 15:27:50 +0000344 const char *cp, *vllist;
345 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 _enter("");
348
349 if (!rootcell) {
350 /* module is loaded with no parameters, or built statically.
351 * - in the future we might initialize cell DB here.
352 */
David Howells08e0e7c2007-04-26 15:55:03 -0700353 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return 0;
355 }
356
357 cp = strchr(rootcell, ':');
David Howells989782d2017-11-02 15:27:50 +0000358 if (!cp) {
Wang Lei07567a52010-08-04 15:16:38 +0100359 _debug("kAFS: no VL server IP addresses specified");
David Howells989782d2017-11-02 15:27:50 +0000360 vllist = NULL;
361 len = strlen(rootcell);
362 } else {
363 vllist = cp + 1;
364 len = cp - rootcell;
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* allocate a cell record for the root cell */
David Howells989782d2017-11-02 15:27:50 +0000368 new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
David Howells08e0e7c2007-04-26 15:55:03 -0700369 if (IS_ERR(new_root)) {
370 _leave(" = %ld", PTR_ERR(new_root));
371 return PTR_ERR(new_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
David Howells17814ae2018-04-09 21:12:31 +0100374 if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
David Howellsdca54a72020-10-13 20:51:59 +0100375 afs_use_cell(new_root, afs_cell_trace_use_pin);
David Howells989782d2017-11-02 15:27:50 +0000376
David Howells08e0e7c2007-04-26 15:55:03 -0700377 /* install the new cell */
David Howells92e3cc92020-10-09 14:11:58 +0100378 down_write(&net->cells_lock);
David Howellsdca54a72020-10-13 20:51:59 +0100379 afs_see_cell(new_root, afs_cell_trace_see_ws);
David Howells92e3cc92020-10-09 14:11:58 +0100380 old_root = net->ws_cell;
381 net->ws_cell = new_root;
382 up_write(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
David Howellsdca54a72020-10-13 20:51:59 +0100384 afs_unuse_cell(net, old_root, afs_cell_trace_unuse_ws);
David Howells08e0e7c2007-04-26 15:55:03 -0700385 _leave(" = 0");
386 return 0;
David Howellsec268152007-04-26 15:49:28 -0700387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
David Howells989782d2017-11-02 15:27:50 +0000390 * Update a cell's VL server address list from the DNS.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
David Howellsd5c32c82019-05-07 15:06:36 +0100392static int afs_update_cell(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
David Howellsd5c32c82019-05-07 15:06:36 +0100394 struct afs_vlserver_list *vllist, *old = NULL, *p;
David Howellsded2f4c2018-10-20 00:57:57 +0100395 unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
396 unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
397 time64_t now, expiry = 0;
David Howellsd5c32c82019-05-07 15:06:36 +0100398 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
David Howells989782d2017-11-02 15:27:50 +0000400 _enter("%s", cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David Howells0a5143f2018-10-20 00:57:57 +0100402 vllist = afs_dns_query(cell, &expiry);
David Howellsd5c32c82019-05-07 15:06:36 +0100403 if (IS_ERR(vllist)) {
404 ret = PTR_ERR(vllist);
405
406 _debug("%s: fail %d", cell->name, ret);
407 if (ret == -ENOMEM)
408 goto out_wake;
409
410 ret = -ENOMEM;
411 vllist = afs_alloc_vlserver_list(0);
412 if (!vllist)
413 goto out_wake;
414
415 switch (ret) {
416 case -ENODATA:
417 case -EDESTADDRREQ:
418 vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
419 break;
420 case -EAGAIN:
421 case -ECONNREFUSED:
422 vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
423 break;
424 default:
425 vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
426 break;
427 }
428 }
429
430 _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
431 cell->dns_status = vllist->status;
David Howellsded2f4c2018-10-20 00:57:57 +0100432
433 now = ktime_get_real_seconds();
434 if (min_ttl > max_ttl)
435 max_ttl = min_ttl;
436 if (expiry < now + min_ttl)
437 expiry = now + min_ttl;
438 else if (expiry > now + max_ttl)
439 expiry = now + max_ttl;
440
David Howellsd5c32c82019-05-07 15:06:36 +0100441 _debug("%s: status %d", cell->name, vllist->status);
442 if (vllist->source == DNS_RECORD_UNAVAILABLE) {
443 switch (vllist->status) {
444 case DNS_LOOKUP_GOT_NOT_FOUND:
David Howellsded2f4c2018-10-20 00:57:57 +0100445 /* The DNS said that the cell does not exist or there
446 * weren't any addresses to be had.
447 */
David Howellsded2f4c2018-10-20 00:57:57 +0100448 cell->dns_expiry = expiry;
David Howells8b2a4642017-11-02 15:27:50 +0000449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
David Howellsd5c32c82019-05-07 15:06:36 +0100451 case DNS_LOOKUP_BAD:
452 case DNS_LOOKUP_GOT_LOCAL_FAILURE:
453 case DNS_LOOKUP_GOT_TEMP_FAILURE:
454 case DNS_LOOKUP_GOT_NS_FAILURE:
David Howells8b2a4642017-11-02 15:27:50 +0000455 default:
David Howellsded2f4c2018-10-20 00:57:57 +0100456 cell->dns_expiry = now + 10;
David Howells8b2a4642017-11-02 15:27:50 +0000457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
David Howells8b2a4642017-11-02 15:27:50 +0000459 } else {
David Howells8b2a4642017-11-02 15:27:50 +0000460 cell->dns_expiry = expiry;
David Howells8b2a4642017-11-02 15:27:50 +0000461 }
462
David Howellsd5c32c82019-05-07 15:06:36 +0100463 /* Replace the VL server list if the new record has servers or the old
464 * record doesn't.
465 */
466 write_lock(&cell->vl_servers_lock);
467 p = rcu_dereference_protected(cell->vl_servers, true);
468 if (vllist->nr_servers > 0 || p->nr_servers == 0) {
469 rcu_assign_pointer(cell->vl_servers, vllist);
470 cell->dns_source = vllist->source;
471 old = p;
472 }
473 write_unlock(&cell->vl_servers_lock);
474 afs_put_vlserverlist(cell->net, old);
wangleibec5eb62010-08-11 09:38:04 +0100475
David Howellsd5c32c82019-05-07 15:06:36 +0100476out_wake:
477 smp_store_release(&cell->dns_lookup_count,
478 cell->dns_lookup_count + 1); /* vs source/status */
479 wake_up_var(&cell->dns_lookup_count);
480 _leave(" = %d", ret);
481 return ret;
David Howellsec268152007-04-26 15:49:28 -0700482}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
David Howells989782d2017-11-02 15:27:50 +0000485 * Destroy a cell record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 */
David Howells989782d2017-11-02 15:27:50 +0000487static void afs_cell_destroy(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
David Howells989782d2017-11-02 15:27:50 +0000489 struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
David Howells88c853c2019-07-23 11:24:59 +0100490 struct afs_net *net = cell->net;
491 int u;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
David Howells989782d2017-11-02 15:27:50 +0000493 _enter("%p{%s}", cell, cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David Howells88c853c2019-07-23 11:24:59 +0100495 u = atomic_read(&cell->ref);
496 ASSERTCMP(u, ==, 0);
David Howellsdca54a72020-10-13 20:51:59 +0100497 trace_afs_cell(cell->debug_id, u, atomic_read(&cell->active), afs_cell_trace_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
David Howells88c853c2019-07-23 11:24:59 +0100499 afs_put_vlserverlist(net, rcu_access_pointer(cell->vl_servers));
David Howellsdca54a72020-10-13 20:51:59 +0100500 afs_unuse_cell(net, cell->alias_of, afs_cell_trace_unuse_alias);
David Howells00d3b7a2007-04-26 15:57:07 -0700501 key_put(cell->anonymous_key);
David Howells719fdd32020-06-24 17:00:24 +0100502 kfree(cell->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 kfree(cell);
504
David Howells88c853c2019-07-23 11:24:59 +0100505 afs_dec_cells_outstanding(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700507}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/*
David Howells989782d2017-11-02 15:27:50 +0000510 * Queue the cell manager.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
David Howells989782d2017-11-02 15:27:50 +0000512static void afs_queue_cell_manager(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
David Howells989782d2017-11-02 15:27:50 +0000514 int outstanding = atomic_inc_return(&net->cells_outstanding);
515
516 _enter("%d", outstanding);
517
518 if (!queue_work(afs_wq, &net->cells_manager))
519 afs_dec_cells_outstanding(net);
520}
521
522/*
523 * Cell management timer. We have an increment on cells_outstanding that we
524 * need to pass along to the work item.
525 */
526void afs_cells_timer(struct timer_list *timer)
527{
528 struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
529
530 _enter("");
531 if (!queue_work(afs_wq, &net->cells_manager))
532 afs_dec_cells_outstanding(net);
533}
534
535/*
David Howells8b2a4642017-11-02 15:27:50 +0000536 * Get a reference on a cell record.
537 */
David Howellsdca54a72020-10-13 20:51:59 +0100538struct afs_cell *afs_get_cell(struct afs_cell *cell, enum afs_cell_trace reason)
David Howells8b2a4642017-11-02 15:27:50 +0000539{
David Howellsdca54a72020-10-13 20:51:59 +0100540 int u;
541
David Howells88c853c2019-07-23 11:24:59 +0100542 if (atomic_read(&cell->ref) <= 0)
543 BUG();
544
David Howellsdca54a72020-10-13 20:51:59 +0100545 u = atomic_inc_return(&cell->ref);
546 trace_afs_cell(cell->debug_id, u, atomic_read(&cell->active), reason);
David Howells8b2a4642017-11-02 15:27:50 +0000547 return cell;
548}
549
550/*
David Howells989782d2017-11-02 15:27:50 +0000551 * Drop a reference on a cell record.
552 */
David Howellsdca54a72020-10-13 20:51:59 +0100553void afs_put_cell(struct afs_cell *cell, enum afs_cell_trace reason)
David Howells88c853c2019-07-23 11:24:59 +0100554{
555 if (cell) {
David Howellsdca54a72020-10-13 20:51:59 +0100556 unsigned int debug_id = cell->debug_id;
David Howells88c853c2019-07-23 11:24:59 +0100557 unsigned int u, a;
558
David Howellsdca54a72020-10-13 20:51:59 +0100559 a = atomic_read(&cell->active);
David Howells88c853c2019-07-23 11:24:59 +0100560 u = atomic_dec_return(&cell->ref);
David Howellsdca54a72020-10-13 20:51:59 +0100561 trace_afs_cell(debug_id, u, a, reason);
David Howells88c853c2019-07-23 11:24:59 +0100562 if (u == 0) {
563 a = atomic_read(&cell->active);
564 WARN(a != 0, "Cell active count %u > 0\n", a);
565 call_rcu(&cell->rcu, afs_cell_destroy);
566 }
567 }
568}
569
570/*
571 * Note a cell becoming more active.
572 */
David Howellsdca54a72020-10-13 20:51:59 +0100573struct afs_cell *afs_use_cell(struct afs_cell *cell, enum afs_cell_trace reason)
David Howells88c853c2019-07-23 11:24:59 +0100574{
David Howellsdca54a72020-10-13 20:51:59 +0100575 int u, a;
576
David Howells88c853c2019-07-23 11:24:59 +0100577 if (atomic_read(&cell->ref) <= 0)
578 BUG();
579
David Howellsdca54a72020-10-13 20:51:59 +0100580 u = atomic_read(&cell->ref);
581 a = atomic_inc_return(&cell->active);
582 trace_afs_cell(cell->debug_id, u, a, reason);
David Howells88c853c2019-07-23 11:24:59 +0100583 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 */
David Howellsdca54a72020-10-13 20:51:59 +0100590void afs_unuse_cell(struct afs_net *net, struct afs_cell *cell, enum afs_cell_trace reason)
David Howells989782d2017-11-02 15:27:50 +0000591{
David Howellsacc080d2020-10-27 10:42:56 +0000592 unsigned int debug_id;
David Howells989782d2017-11-02 15:27:50 +0000593 time64_t now, expire_delay;
David Howellsdca54a72020-10-13 20:51:59 +0100594 int u, a;
David Howells989782d2017-11-02 15:27:50 +0000595
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;
David Howellsd5c32c82019-05-07 15:06:36 +0100604 if (cell->vl_servers->nr_servers)
David Howells989782d2017-11-02 15:27:50 +0000605 expire_delay = afs_cell_gc_delay;
606
David Howellsacc080d2020-10-27 10:42:56 +0000607 debug_id = cell->debug_id;
David Howellsdca54a72020-10-13 20:51:59 +0100608 u = atomic_read(&cell->ref);
David Howells88c853c2019-07-23 11:24:59 +0100609 a = atomic_dec_return(&cell->active);
David Howellsdca54a72020-10-13 20:51:59 +0100610 trace_afs_cell(debug_id, u, a, reason);
David Howells88c853c2019-07-23 11:24:59 +0100611 WARN_ON(a == 0);
612 if (a == 1)
613 /* 'cell' may now be garbage collected. */
614 afs_set_cell_timer(net, expire_delay);
615}
David Howells989782d2017-11-02 15:27:50 +0000616
David Howells88c853c2019-07-23 11:24:59 +0100617/*
David Howellsdca54a72020-10-13 20:51:59 +0100618 * Note that a cell has been seen.
619 */
620void afs_see_cell(struct afs_cell *cell, enum afs_cell_trace reason)
621{
622 int u, a;
623
624 u = atomic_read(&cell->ref);
625 a = atomic_read(&cell->active);
626 trace_afs_cell(cell->debug_id, u, a, reason);
627}
628
629/*
David Howells88c853c2019-07-23 11:24:59 +0100630 * Queue a cell for management, giving the workqueue a ref to hold.
631 */
David Howellsdca54a72020-10-13 20:51:59 +0100632void afs_queue_cell(struct afs_cell *cell, enum afs_cell_trace reason)
David Howells88c853c2019-07-23 11:24:59 +0100633{
David Howellsdca54a72020-10-13 20:51:59 +0100634 afs_get_cell(cell, reason);
David Howells88c853c2019-07-23 11:24:59 +0100635 if (!queue_work(afs_wq, &cell->manager))
David Howellsdca54a72020-10-13 20:51:59 +0100636 afs_put_cell(cell, afs_cell_trace_put_queue_fail);
David Howells989782d2017-11-02 15:27:50 +0000637}
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{
David Howells6b3944e2018-10-11 22:45:49 +0100671 struct hlist_node **p;
672 struct afs_cell *pcell;
David Howells989782d2017-11-02 15:27:50 +0000673 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#ifdef CONFIG_AFS_FSCACHE
682 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
683 &afs_cell_cache_index_def,
David Howells402cb8d2018-04-04 13:41:28 +0100684 cell->name, strlen(cell->name),
685 NULL, 0,
David Howellsee1235a2018-04-04 13:41:28 +0100686 cell, 0, true);
David Howells989782d2017-11-02 15:27:50 +0000687#endif
David Howells5b86d4f2018-05-18 11:46:15 +0100688 ret = afs_proc_cell_setup(cell);
David Howells989782d2017-11-02 15:27:50 +0000689 if (ret < 0)
690 return ret;
David Howells0da0b7f2018-06-15 15:19:22 +0100691
692 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100693 for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
694 pcell = hlist_entry(*p, struct afs_cell, proc_link);
695 if (strcmp(cell->name, pcell->name) < 0)
696 break;
697 }
698
699 cell->proc_link.pprev = p;
700 cell->proc_link.next = *p;
701 rcu_assign_pointer(*p, &cell->proc_link.next);
702 if (cell->proc_link.next)
703 cell->proc_link.next->pprev = &cell->proc_link.next;
704
David Howells0da0b7f2018-06-15 15:19:22 +0100705 afs_dynroot_mkdir(net, cell);
706 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000707 return 0;
708}
709
710/*
711 * Deactivate a cell.
712 */
713static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
714{
715 _enter("%s", cell->name);
716
David Howells5b86d4f2018-05-18 11:46:15 +0100717 afs_proc_cell_remove(cell);
David Howells989782d2017-11-02 15:27:50 +0000718
David Howells0da0b7f2018-06-15 15:19:22 +0100719 mutex_lock(&net->proc_cells_lock);
David Howells6b3944e2018-10-11 22:45:49 +0100720 hlist_del_rcu(&cell->proc_link);
David Howells0da0b7f2018-06-15 15:19:22 +0100721 afs_dynroot_rmdir(net, cell);
722 mutex_unlock(&net->proc_cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000723
724#ifdef CONFIG_AFS_FSCACHE
David Howells402cb8d2018-04-04 13:41:28 +0100725 fscache_relinquish_cookie(cell->cache, NULL, false);
David Howells989782d2017-11-02 15:27:50 +0000726 cell->cache = NULL;
727#endif
728
729 _leave("");
730}
731
732/*
733 * Manage a cell record, initialising and destroying it, maintaining its DNS
734 * records.
735 */
David Howells88c853c2019-07-23 11:24:59 +0100736static void afs_manage_cell(struct afs_cell *cell)
David Howells989782d2017-11-02 15:27:50 +0000737{
David Howells989782d2017-11-02 15:27:50 +0000738 struct afs_net *net = cell->net;
David Howells88c853c2019-07-23 11:24:59 +0100739 int ret, active;
David Howells989782d2017-11-02 15:27:50 +0000740
741 _enter("%s", cell->name);
742
743again:
744 _debug("state %u", cell->state);
745 switch (cell->state) {
746 case AFS_CELL_INACTIVE:
747 case AFS_CELL_FAILED:
David Howells92e3cc92020-10-09 14:11:58 +0100748 down_write(&net->cells_lock);
David Howells88c853c2019-07-23 11:24:59 +0100749 active = 1;
David Howells1d0e8502020-10-16 13:21:14 +0100750 if (atomic_try_cmpxchg_relaxed(&cell->active, &active, 0)) {
David Howells989782d2017-11-02 15:27:50 +0000751 rb_erase(&cell->net_node, &net->cells);
David Howellsdca54a72020-10-13 20:51:59 +0100752 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref), 0,
753 afs_cell_trace_unuse_delete);
David Howells1d0e8502020-10-16 13:21:14 +0100754 smp_store_release(&cell->state, AFS_CELL_REMOVED);
David Howells88c853c2019-07-23 11:24:59 +0100755 }
David Howells92e3cc92020-10-09 14:11:58 +0100756 up_write(&net->cells_lock);
David Howells1d0e8502020-10-16 13:21:14 +0100757 if (cell->state == AFS_CELL_REMOVED) {
758 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000759 goto final_destruction;
David Howells1d0e8502020-10-16 13:21:14 +0100760 }
David Howells989782d2017-11-02 15:27:50 +0000761 if (cell->state == AFS_CELL_FAILED)
762 goto done;
David Howellsd5c32c82019-05-07 15:06:36 +0100763 smp_store_release(&cell->state, AFS_CELL_UNSET);
764 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000765 goto again;
766
767 case AFS_CELL_UNSET:
David Howellsd5c32c82019-05-07 15:06:36 +0100768 smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
769 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000770 goto again;
771
772 case AFS_CELL_ACTIVATING:
773 ret = afs_activate_cell(net, cell);
774 if (ret < 0)
775 goto activation_failed;
776
David Howellsd5c32c82019-05-07 15:06:36 +0100777 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
778 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000779 goto again;
780
781 case AFS_CELL_ACTIVE:
David Howells88c853c2019-07-23 11:24:59 +0100782 if (atomic_read(&cell->active) > 1) {
David Howellsd5c32c82019-05-07 15:06:36 +0100783 if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
784 ret = afs_update_cell(cell);
785 if (ret < 0)
786 cell->error = ret;
787 }
David Howells989782d2017-11-02 15:27:50 +0000788 goto done;
789 }
David Howellsd5c32c82019-05-07 15:06:36 +0100790 smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
791 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000792 goto again;
793
794 case AFS_CELL_DEACTIVATING:
David Howells88c853c2019-07-23 11:24:59 +0100795 if (atomic_read(&cell->active) > 1)
David Howells989782d2017-11-02 15:27:50 +0000796 goto reverse_deactivation;
797 afs_deactivate_cell(net, cell);
David Howellsd5c32c82019-05-07 15:06:36 +0100798 smp_store_release(&cell->state, AFS_CELL_INACTIVE);
799 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000800 goto again;
801
David Howells1d0e8502020-10-16 13:21:14 +0100802 case AFS_CELL_REMOVED:
803 goto done;
804
David Howells989782d2017-11-02 15:27:50 +0000805 default:
806 break;
807 }
808 _debug("bad state %u", cell->state);
809 BUG(); /* Unhandled state */
810
811activation_failed:
812 cell->error = ret;
813 afs_deactivate_cell(net, cell);
814
David Howellsd5c32c82019-05-07 15:06:36 +0100815 smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
816 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000817 goto again;
818
819reverse_deactivation:
David Howellsd5c32c82019-05-07 15:06:36 +0100820 smp_store_release(&cell->state, AFS_CELL_ACTIVE);
821 wake_up_var(&cell->state);
David Howells989782d2017-11-02 15:27:50 +0000822 _leave(" [deact->act]");
823 return;
824
825done:
826 _leave(" [done %u]", cell->state);
827 return;
828
829final_destruction:
David Howells88c853c2019-07-23 11:24:59 +0100830 /* The root volume is pinning the cell */
831 afs_put_volume(cell->net, cell->root_volume, afs_volume_trace_put_cell_root);
832 cell->root_volume = NULL;
David Howellsdca54a72020-10-13 20:51:59 +0100833 afs_put_cell(cell, afs_cell_trace_put_destroy);
David Howells88c853c2019-07-23 11:24:59 +0100834}
835
836static void afs_manage_cell_work(struct work_struct *work)
837{
838 struct afs_cell *cell = container_of(work, struct afs_cell, manager);
839
840 afs_manage_cell(cell);
David Howellsdca54a72020-10-13 20:51:59 +0100841 afs_put_cell(cell, afs_cell_trace_put_queue_work);
David Howells989782d2017-11-02 15:27:50 +0000842}
843
844/*
845 * Manage the records of cells known to a network namespace. This includes
846 * updating the DNS records and garbage collecting unused cells that were
847 * automatically added.
848 *
849 * Note that constructed cell records may only be removed from net->cells by
850 * this work item, so it is safe for this work item to stash a cursor pointing
851 * into the tree and then return to caller (provided it skips cells that are
852 * still under construction).
853 *
854 * Note also that we were given an increment on net->cells_outstanding by
855 * whoever queued us that we need to deal with before returning.
856 */
857void afs_manage_cells(struct work_struct *work)
858{
859 struct afs_net *net = container_of(work, struct afs_net, cells_manager);
860 struct rb_node *cursor;
861 time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
862 bool purging = !net->live;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 _enter("");
865
David Howells989782d2017-11-02 15:27:50 +0000866 /* Trawl the cell database looking for cells that have expired from
867 * lack of use and cells whose DNS results have expired and dispatch
868 * their managers.
869 */
David Howells92e3cc92020-10-09 14:11:58 +0100870 down_read(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
David Howells989782d2017-11-02 15:27:50 +0000872 for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
873 struct afs_cell *cell =
874 rb_entry(cursor, struct afs_cell, net_node);
David Howells88c853c2019-07-23 11:24:59 +0100875 unsigned active;
David Howells989782d2017-11-02 15:27:50 +0000876 bool sched_cell = false;
David Howells08e0e7c2007-04-26 15:55:03 -0700877
David Howells88c853c2019-07-23 11:24:59 +0100878 active = atomic_read(&cell->active);
David Howellsdca54a72020-10-13 20:51:59 +0100879 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref),
880 active, afs_cell_trace_manage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
David Howells88c853c2019-07-23 11:24:59 +0100882 ASSERTCMP(active, >=, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
David Howells989782d2017-11-02 15:27:50 +0000884 if (purging) {
David Howellsdca54a72020-10-13 20:51:59 +0100885 if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags)) {
886 active = atomic_dec_return(&cell->active);
887 trace_afs_cell(cell->debug_id, atomic_read(&cell->ref),
888 active, afs_cell_trace_unuse_pin);
889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
David Howells88c853c2019-07-23 11:24:59 +0100892 if (active == 1) {
David Howellsd5c32c82019-05-07 15:06:36 +0100893 struct afs_vlserver_list *vllist;
David Howells989782d2017-11-02 15:27:50 +0000894 time64_t expire_at = cell->last_inactive;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
David Howellsd5c32c82019-05-07 15:06:36 +0100896 read_lock(&cell->vl_servers_lock);
897 vllist = rcu_dereference_protected(
898 cell->vl_servers,
899 lockdep_is_held(&cell->vl_servers_lock));
900 if (vllist->nr_servers > 0)
David Howells989782d2017-11-02 15:27:50 +0000901 expire_at += afs_cell_gc_delay;
David Howellsd5c32c82019-05-07 15:06:36 +0100902 read_unlock(&cell->vl_servers_lock);
David Howells989782d2017-11-02 15:27:50 +0000903 if (purging || expire_at <= now)
904 sched_cell = true;
905 else if (expire_at < next_manage)
906 next_manage = expire_at;
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
David Howells989782d2017-11-02 15:27:50 +0000909 if (!purging) {
David Howellsd5c32c82019-05-07 15:06:36 +0100910 if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
David Howells989782d2017-11-02 15:27:50 +0000911 sched_cell = true;
David Howells989782d2017-11-02 15:27:50 +0000912 }
913
914 if (sched_cell)
David Howellsdca54a72020-10-13 20:51:59 +0100915 afs_queue_cell(cell, afs_cell_trace_get_queue_manage);
David Howells989782d2017-11-02 15:27:50 +0000916 }
917
David Howells92e3cc92020-10-09 14:11:58 +0100918 up_read(&net->cells_lock);
David Howells989782d2017-11-02 15:27:50 +0000919
920 /* Update the timer on the way out. We have to pass an increment on
921 * cells_outstanding in the namespace that we are in to the timer or
922 * the work scheduler.
923 */
924 if (!purging && next_manage < TIME64_MAX) {
925 now = ktime_get_real_seconds();
926
927 if (next_manage - now <= 0) {
928 if (queue_work(afs_wq, &net->cells_manager))
929 atomic_inc(&net->cells_outstanding);
930 } else {
931 afs_set_cell_timer(net, next_manage - now);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933 }
934
David Howells989782d2017-11-02 15:27:50 +0000935 afs_dec_cells_outstanding(net);
936 _leave(" [%d]", atomic_read(&net->cells_outstanding));
937}
938
939/*
940 * Purge in-memory cell database.
941 */
942void afs_cell_purge(struct afs_net *net)
943{
944 struct afs_cell *ws;
945
946 _enter("");
947
David Howells92e3cc92020-10-09 14:11:58 +0100948 down_write(&net->cells_lock);
949 ws = net->ws_cell;
950 net->ws_cell = NULL;
951 up_write(&net->cells_lock);
David Howellsdca54a72020-10-13 20:51:59 +0100952 afs_unuse_cell(net, ws, afs_cell_trace_unuse_ws);
David Howells989782d2017-11-02 15:27:50 +0000953
954 _debug("del timer");
955 if (del_timer_sync(&net->cells_timer))
956 atomic_dec(&net->cells_outstanding);
957
958 _debug("kick mgr");
959 afs_queue_cell_manager(net);
960
961 _debug("wait");
Peter Zijlstraab1fbe32018-03-15 11:42:28 +0100962 wait_var_event(&net->cells_outstanding,
963 !atomic_read(&net->cells_outstanding));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700965}