12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ec26815aSDavid Howells /* AFS cell and server record management
31da177e4SLinus Torvalds *
4989782dcSDavid Howells * Copyright (C) 2002, 2017 Red Hat, Inc. All Rights Reserved.
51da177e4SLinus Torvalds * Written by David Howells (dhowells@redhat.com)
61da177e4SLinus Torvalds */
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds #include <linux/slab.h>
900d3b7a4SDavid Howells #include <linux/key.h>
1000d3b7a4SDavid Howells #include <linux/ctype.h>
1107567a55SWang Lei #include <linux/dns_resolver.h>
12e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
133838d3ecSDavid Howells #include <linux/inet.h>
140da0b7fdSDavid Howells #include <linux/namei.h>
1500d3b7a4SDavid Howells #include <keys/rxrpc-type.h>
161da177e4SLinus Torvalds #include "internal.h"
171da177e4SLinus Torvalds
18fe342cf7SDavid Howells static unsigned __read_mostly afs_cell_gc_delay = 10;
19ded2f4c5SDavid Howells static unsigned __read_mostly afs_cell_min_ttl = 10 * 60;
20ded2f4c5SDavid Howells static unsigned __read_mostly afs_cell_max_ttl = 24 * 60 * 60;
21dca54a7bSDavid Howells static atomic_t cell_debug_id;
22989782dcSDavid Howells
23286377f6SDavid Howells static void afs_queue_cell_manager(struct afs_net *);
2488c853c3SDavid Howells static void afs_manage_cell_work(struct work_struct *);
25989782dcSDavid Howells
afs_dec_cells_outstanding(struct afs_net * net)26989782dcSDavid Howells static void afs_dec_cells_outstanding(struct afs_net *net)
27989782dcSDavid Howells {
28989782dcSDavid Howells if (atomic_dec_and_test(&net->cells_outstanding))
29ab1fbe32SPeter Zijlstra wake_up_var(&net->cells_outstanding);
30989782dcSDavid Howells }
31989782dcSDavid Howells
321da177e4SLinus Torvalds /*
33989782dcSDavid Howells * Set the cell timer to fire after a given delay, assuming it's not already
34989782dcSDavid Howells * set for an earlier time.
35989782dcSDavid Howells */
afs_set_cell_timer(struct afs_net * net,time64_t delay)36989782dcSDavid Howells static void afs_set_cell_timer(struct afs_net *net, time64_t delay)
37989782dcSDavid Howells {
38989782dcSDavid Howells if (net->live) {
39989782dcSDavid Howells atomic_inc(&net->cells_outstanding);
40989782dcSDavid Howells if (timer_reduce(&net->cells_timer, jiffies + delay * HZ))
41989782dcSDavid Howells afs_dec_cells_outstanding(net);
42286377f6SDavid Howells } else {
43286377f6SDavid Howells afs_queue_cell_manager(net);
44989782dcSDavid Howells }
45989782dcSDavid Howells }
46989782dcSDavid Howells
47989782dcSDavid Howells /*
4892e3cc91SDavid Howells * Look up and get an activation reference on a cell record. The caller must
4992e3cc91SDavid Howells * hold net->cells_lock at least read-locked.
50989782dcSDavid Howells */
afs_find_cell_locked(struct afs_net * net,const char * name,unsigned int namesz,enum afs_cell_trace reason)5192e3cc91SDavid Howells static struct afs_cell *afs_find_cell_locked(struct afs_net *net,
52dca54a7bSDavid Howells const char *name, unsigned int namesz,
53dca54a7bSDavid Howells enum afs_cell_trace reason)
54989782dcSDavid Howells {
55989782dcSDavid Howells struct afs_cell *cell = NULL;
56989782dcSDavid Howells struct rb_node *p;
5792e3cc91SDavid Howells int n;
58989782dcSDavid Howells
59989782dcSDavid Howells _enter("%*.*s", namesz, namesz, name);
60989782dcSDavid Howells
61989782dcSDavid Howells if (name && namesz == 0)
62989782dcSDavid Howells return ERR_PTR(-EINVAL);
63989782dcSDavid Howells if (namesz > AFS_MAXCELLNAME)
64989782dcSDavid Howells return ERR_PTR(-ENAMETOOLONG);
65989782dcSDavid Howells
66989782dcSDavid Howells if (!name) {
6792e3cc91SDavid Howells cell = net->ws_cell;
6892e3cc91SDavid Howells if (!cell)
6992e3cc91SDavid Howells return ERR_PTR(-EDESTADDRREQ);
7088c853c3SDavid Howells goto found;
71989782dcSDavid Howells }
72989782dcSDavid Howells
7392e3cc91SDavid Howells p = net->cells.rb_node;
74989782dcSDavid Howells while (p) {
75989782dcSDavid Howells cell = rb_entry(p, struct afs_cell, net_node);
76989782dcSDavid Howells
77989782dcSDavid Howells n = strncasecmp(cell->name, name,
78989782dcSDavid Howells min_t(size_t, cell->name_len, namesz));
79989782dcSDavid Howells if (n == 0)
80989782dcSDavid Howells n = cell->name_len - namesz;
8192e3cc91SDavid Howells if (n < 0)
8292e3cc91SDavid Howells p = p->rb_left;
8392e3cc91SDavid Howells else if (n > 0)
8492e3cc91SDavid Howells p = p->rb_right;
8592e3cc91SDavid Howells else
8692e3cc91SDavid Howells goto found;
87989782dcSDavid Howells }
88989782dcSDavid Howells
8992e3cc91SDavid Howells return ERR_PTR(-ENOENT);
90989782dcSDavid Howells
9192e3cc91SDavid Howells found:
92dca54a7bSDavid Howells return afs_use_cell(cell, reason);
9392e3cc91SDavid Howells }
94a5fb8e6cSDavid Howells
9588c853c3SDavid Howells /*
9688c853c3SDavid Howells * Look up and get an activation reference on a cell record.
9788c853c3SDavid Howells */
afs_find_cell(struct afs_net * net,const char * name,unsigned int namesz,enum afs_cell_trace reason)9892e3cc91SDavid Howells struct afs_cell *afs_find_cell(struct afs_net *net,
99dca54a7bSDavid Howells const char *name, unsigned int namesz,
100dca54a7bSDavid Howells enum afs_cell_trace reason)
10192e3cc91SDavid Howells {
10292e3cc91SDavid Howells struct afs_cell *cell;
10392e3cc91SDavid Howells
10492e3cc91SDavid Howells down_read(&net->cells_lock);
105dca54a7bSDavid Howells cell = afs_find_cell_locked(net, name, namesz, reason);
10692e3cc91SDavid Howells up_read(&net->cells_lock);
10792e3cc91SDavid Howells return cell;
108989782dcSDavid Howells }
109989782dcSDavid Howells
110989782dcSDavid Howells /*
111989782dcSDavid Howells * Set up a cell record and fill in its name, VL server address list and
11200d3b7a4SDavid Howells * allocate an anonymous key
1131da177e4SLinus Torvalds */
afs_alloc_cell(struct afs_net * net,const char * name,unsigned int namelen,const char * addresses)114989782dcSDavid Howells static struct afs_cell *afs_alloc_cell(struct afs_net *net,
115989782dcSDavid Howells const char *name, unsigned int namelen,
1160a5143f2SDavid Howells const char *addresses)
1171da177e4SLinus Torvalds {
118ca1cbbdcSDavid Howells struct afs_vlserver_list *vllist;
1191da177e4SLinus Torvalds struct afs_cell *cell;
120989782dcSDavid Howells int i, ret;
1211da177e4SLinus Torvalds
122989782dcSDavid Howells ASSERT(name);
123989782dcSDavid Howells if (namelen == 0)
124989782dcSDavid Howells return ERR_PTR(-EINVAL);
12507567a55SWang Lei if (namelen > AFS_MAXCELLNAME) {
12607567a55SWang Lei _leave(" = -ENAMETOOLONG");
12700d3b7a4SDavid Howells return ERR_PTR(-ENAMETOOLONG);
12807567a55SWang Lei }
129a45ea48eSDavid Howells
130a45ea48eSDavid Howells /* Prohibit cell names that contain unprintable chars, '/' and '@' or
131a45ea48eSDavid Howells * that begin with a dot. This also precludes "@cell".
132a45ea48eSDavid Howells */
133a45ea48eSDavid Howells if (name[0] == '.')
13437ab6368SDavid Howells return ERR_PTR(-EINVAL);
135a45ea48eSDavid Howells for (i = 0; i < namelen; i++) {
136a45ea48eSDavid Howells char ch = name[i];
137a45ea48eSDavid Howells if (!isprint(ch) || ch == '/' || ch == '@')
138a45ea48eSDavid Howells return ERR_PTR(-EINVAL);
139a45ea48eSDavid Howells }
14000d3b7a4SDavid Howells
1410a5143f2SDavid Howells _enter("%*.*s,%s", namelen, namelen, name, addresses);
142989782dcSDavid Howells
143989782dcSDavid Howells cell = kzalloc(sizeof(struct afs_cell), GFP_KERNEL);
1441da177e4SLinus Torvalds if (!cell) {
1451da177e4SLinus Torvalds _leave(" = -ENOMEM");
14608e0e7c8SDavid Howells return ERR_PTR(-ENOMEM);
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds
149719fdd32SDavid Howells cell->name = kmalloc(namelen + 1, GFP_KERNEL);
150719fdd32SDavid Howells if (!cell->name) {
151719fdd32SDavid Howells kfree(cell);
152719fdd32SDavid Howells return ERR_PTR(-ENOMEM);
153719fdd32SDavid Howells }
154719fdd32SDavid Howells
155f044c884SDavid Howells cell->net = net;
156989782dcSDavid Howells cell->name_len = namelen;
157989782dcSDavid Howells for (i = 0; i < namelen; i++)
158989782dcSDavid Howells cell->name[i] = tolower(name[i]);
159719fdd32SDavid Howells cell->name[i] = 0;
160989782dcSDavid Howells
161c56f9ec8SDavid Howells refcount_set(&cell->ref, 1);
16288c853c3SDavid Howells atomic_set(&cell->active, 0);
16388c853c3SDavid Howells INIT_WORK(&cell->manager, afs_manage_cell_work);
164*0b2bf6fbSDavid Howells spin_lock_init(&cell->vs_lock);
16520325960SDavid Howells cell->volumes = RB_ROOT;
16620325960SDavid Howells INIT_HLIST_HEAD(&cell->proc_volumes);
16720325960SDavid Howells seqlock_init(&cell->volume_lock);
16820325960SDavid Howells cell->fs_servers = RB_ROOT;
16920325960SDavid Howells seqlock_init(&cell->fs_lock);
1706e0e99d5SDavid Howells INIT_LIST_HEAD(&cell->fs_open_mmaps);
1716e0e99d5SDavid Howells init_rwsem(&cell->fs_open_mmaps_lock);
1720a5143f2SDavid Howells rwlock_init(&cell->vl_servers_lock);
1738a070a96SDavid Howells cell->flags = (1 << AFS_CELL_FL_CHECK_ALIAS);
1744d9df986SDavid Howells
175ca1cbbdcSDavid Howells /* Provide a VL server list, filling it in if we were given a list of
176ca1cbbdcSDavid Howells * addresses to use.
177989782dcSDavid Howells */
1780a5143f2SDavid Howells if (addresses) {
1790a5143f2SDavid Howells vllist = afs_parse_text_addrs(net,
1800a5143f2SDavid Howells addresses, strlen(addresses), ':',
1818b2a464cSDavid Howells VL_SERVICE, AFS_VL_PORT);
1820a5143f2SDavid Howells if (IS_ERR(vllist)) {
1830a5143f2SDavid Howells ret = PTR_ERR(vllist);
1848b2a464cSDavid Howells goto parse_failed;
1853838d3ecSDavid Howells }
1861da177e4SLinus Torvalds
187d5c32c89SDavid Howells vllist->source = DNS_RECORD_FROM_CONFIG;
188d5c32c89SDavid Howells vllist->status = DNS_LOOKUP_NOT_DONE;
189989782dcSDavid Howells cell->dns_expiry = TIME64_MAX;
190ded2f4c5SDavid Howells } else {
191ca1cbbdcSDavid Howells ret = -ENOMEM;
192ca1cbbdcSDavid Howells vllist = afs_alloc_vlserver_list(0);
193ca1cbbdcSDavid Howells if (!vllist)
194ca1cbbdcSDavid Howells goto error;
195d5c32c89SDavid Howells vllist->source = DNS_RECORD_UNAVAILABLE;
196d5c32c89SDavid Howells vllist->status = DNS_LOOKUP_NOT_DONE;
197ded2f4c5SDavid Howells cell->dns_expiry = ktime_get_real_seconds();
19800d3b7a4SDavid Howells }
19900d3b7a4SDavid Howells
200ca1cbbdcSDavid Howells rcu_assign_pointer(cell->vl_servers, vllist);
201ca1cbbdcSDavid Howells
202d5c32c89SDavid Howells cell->dns_source = vllist->source;
203d5c32c89SDavid Howells cell->dns_status = vllist->status;
204d5c32c89SDavid Howells smp_store_release(&cell->dns_lookup_count, 1); /* vs source/status */
20588c853c3SDavid Howells atomic_inc(&net->cells_outstanding);
206dca54a7bSDavid Howells cell->debug_id = atomic_inc_return(&cell_debug_id);
207dca54a7bSDavid Howells trace_afs_cell(cell->debug_id, 1, 0, afs_cell_trace_alloc);
208d5c32c89SDavid Howells
20900d3b7a4SDavid Howells _leave(" = %p", cell);
21000d3b7a4SDavid Howells return cell;
21100d3b7a4SDavid Howells
2128b2a464cSDavid Howells parse_failed:
2138b2a464cSDavid Howells if (ret == -EINVAL)
21400d3b7a4SDavid Howells printk(KERN_ERR "kAFS: bad VL server IP address\n");
215ca1cbbdcSDavid Howells error:
216719fdd32SDavid Howells kfree(cell->name);
21700d3b7a4SDavid Howells kfree(cell);
21800d3b7a4SDavid Howells _leave(" = %d", ret);
21900d3b7a4SDavid Howells return ERR_PTR(ret);
22000d3b7a4SDavid Howells }
22100d3b7a4SDavid Howells
22200d3b7a4SDavid Howells /*
223989782dcSDavid Howells * afs_lookup_cell - Look up or create a cell record.
224f044c884SDavid Howells * @net: The network namespace
225989782dcSDavid Howells * @name: The name of the cell.
226989782dcSDavid Howells * @namesz: The strlen of the cell name.
227989782dcSDavid Howells * @vllist: A colon/comma separated list of numeric IP addresses or NULL.
228989782dcSDavid Howells * @excl: T if an error should be given if the cell name already exists.
229989782dcSDavid Howells *
230989782dcSDavid Howells * Look up a cell record by name and query the DNS for VL server addresses if
231989782dcSDavid Howells * needed. Note that that actual DNS query is punted off to the manager thread
232989782dcSDavid Howells * so that this function can return immediately if interrupted whilst allowing
233989782dcSDavid Howells * cell records to be shared even if not yet fully constructed.
23400d3b7a4SDavid Howells */
afs_lookup_cell(struct afs_net * net,const char * name,unsigned int namesz,const char * vllist,bool excl)235989782dcSDavid Howells struct afs_cell *afs_lookup_cell(struct afs_net *net,
236989782dcSDavid Howells const char *name, unsigned int namesz,
237989782dcSDavid Howells const char *vllist, bool excl)
23800d3b7a4SDavid Howells {
239989782dcSDavid Howells struct afs_cell *cell, *candidate, *cursor;
240989782dcSDavid Howells struct rb_node *parent, **pp;
241d5c32c89SDavid Howells enum afs_cell_state state;
242989782dcSDavid Howells int ret, n;
24300d3b7a4SDavid Howells
244989782dcSDavid Howells _enter("%s,%s", name, vllist);
24500d3b7a4SDavid Howells
246989782dcSDavid Howells if (!excl) {
247dca54a7bSDavid Howells cell = afs_find_cell(net, name, namesz, afs_cell_trace_use_lookup);
24868327951SGustavo A. R. Silva if (!IS_ERR(cell))
249989782dcSDavid Howells goto wait_for_cell;
250989782dcSDavid Howells }
251989782dcSDavid Howells
252989782dcSDavid Howells /* Assume we're probably going to create a cell and preallocate and
253989782dcSDavid Howells * mostly set up a candidate record. We can then use this to stash the
254989782dcSDavid Howells * name, the net namespace and VL server addresses.
255989782dcSDavid Howells *
256989782dcSDavid Howells * We also want to do this before we hold any locks as it may involve
257989782dcSDavid Howells * upcalling to userspace to make DNS queries.
258989782dcSDavid Howells */
259989782dcSDavid Howells candidate = afs_alloc_cell(net, name, namesz, vllist);
260989782dcSDavid Howells if (IS_ERR(candidate)) {
261989782dcSDavid Howells _leave(" = %ld", PTR_ERR(candidate));
262989782dcSDavid Howells return candidate;
263989782dcSDavid Howells }
264989782dcSDavid Howells
265989782dcSDavid Howells /* Find the insertion point and check to see if someone else added a
266989782dcSDavid Howells * cell whilst we were allocating.
267989782dcSDavid Howells */
26892e3cc91SDavid Howells down_write(&net->cells_lock);
269989782dcSDavid Howells
270989782dcSDavid Howells pp = &net->cells.rb_node;
271989782dcSDavid Howells parent = NULL;
272989782dcSDavid Howells while (*pp) {
273989782dcSDavid Howells parent = *pp;
274989782dcSDavid Howells cursor = rb_entry(parent, struct afs_cell, net_node);
275989782dcSDavid Howells
276989782dcSDavid Howells n = strncasecmp(cursor->name, name,
277989782dcSDavid Howells min_t(size_t, cursor->name_len, namesz));
278989782dcSDavid Howells if (n == 0)
279989782dcSDavid Howells n = cursor->name_len - namesz;
280989782dcSDavid Howells if (n < 0)
281989782dcSDavid Howells pp = &(*pp)->rb_left;
282989782dcSDavid Howells else if (n > 0)
283989782dcSDavid Howells pp = &(*pp)->rb_right;
284989782dcSDavid Howells else
285989782dcSDavid Howells goto cell_already_exists;
286989782dcSDavid Howells }
287989782dcSDavid Howells
288989782dcSDavid Howells cell = candidate;
289989782dcSDavid Howells candidate = NULL;
29088c853c3SDavid Howells atomic_set(&cell->active, 2);
291c56f9ec8SDavid Howells trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), 2, afs_cell_trace_insert);
292989782dcSDavid Howells rb_link_node_rcu(&cell->net_node, parent, pp);
293989782dcSDavid Howells rb_insert_color(&cell->net_node, &net->cells);
29492e3cc91SDavid Howells up_write(&net->cells_lock);
295989782dcSDavid Howells
296dca54a7bSDavid Howells afs_queue_cell(cell, afs_cell_trace_get_queue_new);
297989782dcSDavid Howells
298989782dcSDavid Howells wait_for_cell:
299c56f9ec8SDavid Howells trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), atomic_read(&cell->active),
300dca54a7bSDavid Howells afs_cell_trace_wait);
301989782dcSDavid Howells _debug("wait_for_cell");
302d5c32c89SDavid Howells wait_var_event(&cell->state,
303d5c32c89SDavid Howells ({
304d5c32c89SDavid Howells state = smp_load_acquire(&cell->state); /* vs error */
3051d0e850aSDavid Howells state == AFS_CELL_ACTIVE || state == AFS_CELL_REMOVED;
306d5c32c89SDavid Howells }));
307989782dcSDavid Howells
308d5c32c89SDavid Howells /* Check the state obtained from the wait check. */
3091d0e850aSDavid Howells if (state == AFS_CELL_REMOVED) {
310989782dcSDavid Howells ret = cell->error;
311989782dcSDavid Howells goto error;
312989782dcSDavid Howells }
313989782dcSDavid Howells
314989782dcSDavid Howells _leave(" = %p [cell]", cell);
315989782dcSDavid Howells return cell;
316989782dcSDavid Howells
317989782dcSDavid Howells cell_already_exists:
318989782dcSDavid Howells _debug("cell exists");
319989782dcSDavid Howells cell = cursor;
320989782dcSDavid Howells if (excl) {
321989782dcSDavid Howells ret = -EEXIST;
322989782dcSDavid Howells } else {
323dca54a7bSDavid Howells afs_use_cell(cursor, afs_cell_trace_use_lookup);
324989782dcSDavid Howells ret = 0;
325989782dcSDavid Howells }
32692e3cc91SDavid Howells up_write(&net->cells_lock);
32788c853c3SDavid Howells if (candidate)
328dca54a7bSDavid Howells afs_put_cell(candidate, afs_cell_trace_put_candidate);
329989782dcSDavid Howells if (ret == 0)
330989782dcSDavid Howells goto wait_for_cell;
3318b2a464cSDavid Howells goto error_noput;
332989782dcSDavid Howells error:
333dca54a7bSDavid Howells afs_unuse_cell(net, cell, afs_cell_trace_unuse_lookup);
3348b2a464cSDavid Howells error_noput:
335989782dcSDavid Howells _leave(" = %d [error]", ret);
336989782dcSDavid Howells return ERR_PTR(ret);
337989782dcSDavid Howells }
3381da177e4SLinus Torvalds
3391da177e4SLinus Torvalds /*
34008e0e7c8SDavid Howells * set the root cell information
34108e0e7c8SDavid Howells * - can be called with a module parameter string
34208e0e7c8SDavid Howells * - can be called from a write to /proc/fs/afs/rootcell
3431da177e4SLinus Torvalds */
afs_cell_init(struct afs_net * net,const char * rootcell)344989782dcSDavid Howells int afs_cell_init(struct afs_net *net, const char *rootcell)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds struct afs_cell *old_root, *new_root;
347989782dcSDavid Howells const char *cp, *vllist;
348989782dcSDavid Howells size_t len;
3491da177e4SLinus Torvalds
3501da177e4SLinus Torvalds _enter("");
3511da177e4SLinus Torvalds
3521da177e4SLinus Torvalds if (!rootcell) {
3531da177e4SLinus Torvalds /* module is loaded with no parameters, or built statically.
3541da177e4SLinus Torvalds * - in the future we might initialize cell DB here.
3551da177e4SLinus Torvalds */
35608e0e7c8SDavid Howells _leave(" = 0 [no root]");
3571da177e4SLinus Torvalds return 0;
3581da177e4SLinus Torvalds }
3591da177e4SLinus Torvalds
3601da177e4SLinus Torvalds cp = strchr(rootcell, ':');
361989782dcSDavid Howells if (!cp) {
36207567a55SWang Lei _debug("kAFS: no VL server IP addresses specified");
363989782dcSDavid Howells vllist = NULL;
364989782dcSDavid Howells len = strlen(rootcell);
365989782dcSDavid Howells } else {
366989782dcSDavid Howells vllist = cp + 1;
367989782dcSDavid Howells len = cp - rootcell;
368989782dcSDavid Howells }
3691da177e4SLinus Torvalds
3701da177e4SLinus Torvalds /* allocate a cell record for the root cell */
371989782dcSDavid Howells new_root = afs_lookup_cell(net, rootcell, len, vllist, false);
37208e0e7c8SDavid Howells if (IS_ERR(new_root)) {
37308e0e7c8SDavid Howells _leave(" = %ld", PTR_ERR(new_root));
37408e0e7c8SDavid Howells return PTR_ERR(new_root);
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds
37717814aefSDavid Howells if (!test_and_set_bit(AFS_CELL_FL_NO_GC, &new_root->flags))
378dca54a7bSDavid Howells afs_use_cell(new_root, afs_cell_trace_use_pin);
379989782dcSDavid Howells
38008e0e7c8SDavid Howells /* install the new cell */
38192e3cc91SDavid Howells down_write(&net->cells_lock);
382dca54a7bSDavid Howells afs_see_cell(new_root, afs_cell_trace_see_ws);
38392e3cc91SDavid Howells old_root = net->ws_cell;
38492e3cc91SDavid Howells net->ws_cell = new_root;
38592e3cc91SDavid Howells up_write(&net->cells_lock);
3861da177e4SLinus Torvalds
387dca54a7bSDavid Howells afs_unuse_cell(net, old_root, afs_cell_trace_unuse_ws);
38808e0e7c8SDavid Howells _leave(" = 0");
38908e0e7c8SDavid Howells return 0;
390ec26815aSDavid Howells }
3911da177e4SLinus Torvalds
3921da177e4SLinus Torvalds /*
393989782dcSDavid Howells * Update a cell's VL server address list from the DNS.
3941da177e4SLinus Torvalds */
afs_update_cell(struct afs_cell * cell)395d5c32c89SDavid Howells static int afs_update_cell(struct afs_cell *cell)
3961da177e4SLinus Torvalds {
397d5c32c89SDavid Howells struct afs_vlserver_list *vllist, *old = NULL, *p;
398ded2f4c5SDavid Howells unsigned int min_ttl = READ_ONCE(afs_cell_min_ttl);
399ded2f4c5SDavid Howells unsigned int max_ttl = READ_ONCE(afs_cell_max_ttl);
400ded2f4c5SDavid Howells time64_t now, expiry = 0;
401d5c32c89SDavid Howells int ret = 0;
4021da177e4SLinus Torvalds
403989782dcSDavid Howells _enter("%s", cell->name);
4041da177e4SLinus Torvalds
4050a5143f2SDavid Howells vllist = afs_dns_query(cell, &expiry);
406d5c32c89SDavid Howells if (IS_ERR(vllist)) {
407d5c32c89SDavid Howells ret = PTR_ERR(vllist);
408d5c32c89SDavid Howells
409d5c32c89SDavid Howells _debug("%s: fail %d", cell->name, ret);
410d5c32c89SDavid Howells if (ret == -ENOMEM)
411d5c32c89SDavid Howells goto out_wake;
412d5c32c89SDavid Howells
413d5c32c89SDavid Howells vllist = afs_alloc_vlserver_list(0);
41481fc8dceSDavid Howells if (!vllist) {
41581fc8dceSDavid Howells if (ret >= 0)
41681fc8dceSDavid Howells ret = -ENOMEM;
417d5c32c89SDavid Howells goto out_wake;
41881fc8dceSDavid Howells }
419d5c32c89SDavid Howells
420d5c32c89SDavid Howells switch (ret) {
421d5c32c89SDavid Howells case -ENODATA:
422d5c32c89SDavid Howells case -EDESTADDRREQ:
423d5c32c89SDavid Howells vllist->status = DNS_LOOKUP_GOT_NOT_FOUND;
424d5c32c89SDavid Howells break;
425d5c32c89SDavid Howells case -EAGAIN:
426d5c32c89SDavid Howells case -ECONNREFUSED:
427d5c32c89SDavid Howells vllist->status = DNS_LOOKUP_GOT_TEMP_FAILURE;
428d5c32c89SDavid Howells break;
429d5c32c89SDavid Howells default:
430d5c32c89SDavid Howells vllist->status = DNS_LOOKUP_GOT_LOCAL_FAILURE;
431d5c32c89SDavid Howells break;
432d5c32c89SDavid Howells }
433d5c32c89SDavid Howells }
434d5c32c89SDavid Howells
435d5c32c89SDavid Howells _debug("%s: got list %d %d", cell->name, vllist->source, vllist->status);
436d5c32c89SDavid Howells cell->dns_status = vllist->status;
437ded2f4c5SDavid Howells
438ded2f4c5SDavid Howells now = ktime_get_real_seconds();
439ded2f4c5SDavid Howells if (min_ttl > max_ttl)
440ded2f4c5SDavid Howells max_ttl = min_ttl;
441ded2f4c5SDavid Howells if (expiry < now + min_ttl)
442ded2f4c5SDavid Howells expiry = now + min_ttl;
443ded2f4c5SDavid Howells else if (expiry > now + max_ttl)
444ded2f4c5SDavid Howells expiry = now + max_ttl;
445ded2f4c5SDavid Howells
446d5c32c89SDavid Howells _debug("%s: status %d", cell->name, vllist->status);
447d5c32c89SDavid Howells if (vllist->source == DNS_RECORD_UNAVAILABLE) {
448d5c32c89SDavid Howells switch (vllist->status) {
449d5c32c89SDavid Howells case DNS_LOOKUP_GOT_NOT_FOUND:
450ded2f4c5SDavid Howells /* The DNS said that the cell does not exist or there
451ded2f4c5SDavid Howells * weren't any addresses to be had.
452ded2f4c5SDavid Howells */
453ded2f4c5SDavid Howells cell->dns_expiry = expiry;
4548b2a464cSDavid Howells break;
455989782dcSDavid Howells
456d5c32c89SDavid Howells case DNS_LOOKUP_BAD:
457d5c32c89SDavid Howells case DNS_LOOKUP_GOT_LOCAL_FAILURE:
458d5c32c89SDavid Howells case DNS_LOOKUP_GOT_TEMP_FAILURE:
459d5c32c89SDavid Howells case DNS_LOOKUP_GOT_NS_FAILURE:
460989782dcSDavid Howells default:
461ded2f4c5SDavid Howells cell->dns_expiry = now + 10;
462989782dcSDavid Howells break;
4638b2a464cSDavid Howells }
4648b2a464cSDavid Howells } else {
4658b2a464cSDavid Howells cell->dns_expiry = expiry;
4668b2a464cSDavid Howells }
4678b2a464cSDavid Howells
468d5c32c89SDavid Howells /* Replace the VL server list if the new record has servers or the old
469d5c32c89SDavid Howells * record doesn't.
470d5c32c89SDavid Howells */
471d5c32c89SDavid Howells write_lock(&cell->vl_servers_lock);
472d5c32c89SDavid Howells p = rcu_dereference_protected(cell->vl_servers, true);
473d5c32c89SDavid Howells if (vllist->nr_servers > 0 || p->nr_servers == 0) {
474d5c32c89SDavid Howells rcu_assign_pointer(cell->vl_servers, vllist);
475d5c32c89SDavid Howells cell->dns_source = vllist->source;
476d5c32c89SDavid Howells old = p;
477d5c32c89SDavid Howells }
478d5c32c89SDavid Howells write_unlock(&cell->vl_servers_lock);
479d5c32c89SDavid Howells afs_put_vlserverlist(cell->net, old);
480bec5eb61Swanglei
481d5c32c89SDavid Howells out_wake:
482d5c32c89SDavid Howells smp_store_release(&cell->dns_lookup_count,
483d5c32c89SDavid Howells cell->dns_lookup_count + 1); /* vs source/status */
484d5c32c89SDavid Howells wake_up_var(&cell->dns_lookup_count);
485d5c32c89SDavid Howells _leave(" = %d", ret);
486d5c32c89SDavid Howells return ret;
487ec26815aSDavid Howells }
4881da177e4SLinus Torvalds
4891da177e4SLinus Torvalds /*
490989782dcSDavid Howells * Destroy a cell record
4911da177e4SLinus Torvalds */
afs_cell_destroy(struct rcu_head * rcu)492989782dcSDavid Howells static void afs_cell_destroy(struct rcu_head *rcu)
4931da177e4SLinus Torvalds {
494989782dcSDavid Howells struct afs_cell *cell = container_of(rcu, struct afs_cell, rcu);
49588c853c3SDavid Howells struct afs_net *net = cell->net;
496c56f9ec8SDavid Howells int r;
4971da177e4SLinus Torvalds
498989782dcSDavid Howells _enter("%p{%s}", cell, cell->name);
4991da177e4SLinus Torvalds
500c56f9ec8SDavid Howells r = refcount_read(&cell->ref);
501c56f9ec8SDavid Howells ASSERTCMP(r, ==, 0);
502c56f9ec8SDavid Howells trace_afs_cell(cell->debug_id, r, atomic_read(&cell->active), afs_cell_trace_free);
5031da177e4SLinus Torvalds
50488c853c3SDavid Howells afs_put_vlserverlist(net, rcu_access_pointer(cell->vl_servers));
505dca54a7bSDavid Howells afs_unuse_cell(net, cell->alias_of, afs_cell_trace_unuse_alias);
50600d3b7a4SDavid Howells key_put(cell->anonymous_key);
507719fdd32SDavid Howells kfree(cell->name);
5081da177e4SLinus Torvalds kfree(cell);
5091da177e4SLinus Torvalds
51088c853c3SDavid Howells afs_dec_cells_outstanding(net);
5111da177e4SLinus Torvalds _leave(" [destroyed]");
512ec26815aSDavid Howells }
5131da177e4SLinus Torvalds
5141da177e4SLinus Torvalds /*
515989782dcSDavid Howells * Queue the cell manager.
5161da177e4SLinus Torvalds */
afs_queue_cell_manager(struct afs_net * net)517989782dcSDavid Howells static void afs_queue_cell_manager(struct afs_net *net)
5181da177e4SLinus Torvalds {
519989782dcSDavid Howells int outstanding = atomic_inc_return(&net->cells_outstanding);
520989782dcSDavid Howells
521989782dcSDavid Howells _enter("%d", outstanding);
522989782dcSDavid Howells
523989782dcSDavid Howells if (!queue_work(afs_wq, &net->cells_manager))
524989782dcSDavid Howells afs_dec_cells_outstanding(net);
525989782dcSDavid Howells }
526989782dcSDavid Howells
527989782dcSDavid Howells /*
528989782dcSDavid Howells * Cell management timer. We have an increment on cells_outstanding that we
529989782dcSDavid Howells * need to pass along to the work item.
530989782dcSDavid Howells */
afs_cells_timer(struct timer_list * timer)531989782dcSDavid Howells void afs_cells_timer(struct timer_list *timer)
532989782dcSDavid Howells {
533989782dcSDavid Howells struct afs_net *net = container_of(timer, struct afs_net, cells_timer);
534989782dcSDavid Howells
535989782dcSDavid Howells _enter("");
536989782dcSDavid Howells if (!queue_work(afs_wq, &net->cells_manager))
537989782dcSDavid Howells afs_dec_cells_outstanding(net);
538989782dcSDavid Howells }
539989782dcSDavid Howells
540989782dcSDavid Howells /*
5418b2a464cSDavid Howells * Get a reference on a cell record.
5428b2a464cSDavid Howells */
afs_get_cell(struct afs_cell * cell,enum afs_cell_trace reason)543dca54a7bSDavid Howells struct afs_cell *afs_get_cell(struct afs_cell *cell, enum afs_cell_trace reason)
5448b2a464cSDavid Howells {
545c56f9ec8SDavid Howells int r;
546dca54a7bSDavid Howells
547c56f9ec8SDavid Howells __refcount_inc(&cell->ref, &r);
548c56f9ec8SDavid Howells trace_afs_cell(cell->debug_id, r + 1, atomic_read(&cell->active), reason);
5498b2a464cSDavid Howells return cell;
5508b2a464cSDavid Howells }
5518b2a464cSDavid Howells
5528b2a464cSDavid Howells /*
553989782dcSDavid Howells * Drop a reference on a cell record.
554989782dcSDavid Howells */
afs_put_cell(struct afs_cell * cell,enum afs_cell_trace reason)555dca54a7bSDavid Howells void afs_put_cell(struct afs_cell *cell, enum afs_cell_trace reason)
55688c853c3SDavid Howells {
55788c853c3SDavid Howells if (cell) {
558dca54a7bSDavid Howells unsigned int debug_id = cell->debug_id;
559c56f9ec8SDavid Howells unsigned int a;
560c56f9ec8SDavid Howells bool zero;
561c56f9ec8SDavid Howells int r;
56288c853c3SDavid Howells
563dca54a7bSDavid Howells a = atomic_read(&cell->active);
564c56f9ec8SDavid Howells zero = __refcount_dec_and_test(&cell->ref, &r);
565c56f9ec8SDavid Howells trace_afs_cell(debug_id, r - 1, a, reason);
566c56f9ec8SDavid Howells if (zero) {
56788c853c3SDavid Howells a = atomic_read(&cell->active);
56888c853c3SDavid Howells WARN(a != 0, "Cell active count %u > 0\n", a);
56988c853c3SDavid Howells call_rcu(&cell->rcu, afs_cell_destroy);
57088c853c3SDavid Howells }
57188c853c3SDavid Howells }
57288c853c3SDavid Howells }
57388c853c3SDavid Howells
57488c853c3SDavid Howells /*
57588c853c3SDavid Howells * Note a cell becoming more active.
57688c853c3SDavid Howells */
afs_use_cell(struct afs_cell * cell,enum afs_cell_trace reason)577dca54a7bSDavid Howells struct afs_cell *afs_use_cell(struct afs_cell *cell, enum afs_cell_trace reason)
57888c853c3SDavid Howells {
579c56f9ec8SDavid Howells int r, a;
580dca54a7bSDavid Howells
581c56f9ec8SDavid Howells r = refcount_read(&cell->ref);
582c56f9ec8SDavid Howells WARN_ON(r == 0);
583dca54a7bSDavid Howells a = atomic_inc_return(&cell->active);
584c56f9ec8SDavid Howells trace_afs_cell(cell->debug_id, r, a, reason);
58588c853c3SDavid Howells return cell;
58688c853c3SDavid Howells }
58788c853c3SDavid Howells
58888c853c3SDavid Howells /*
58988c853c3SDavid Howells * Record a cell becoming less active. When the active counter reaches 1, it
59088c853c3SDavid Howells * is scheduled for destruction, but may get reactivated.
59188c853c3SDavid Howells */
afs_unuse_cell(struct afs_net * net,struct afs_cell * cell,enum afs_cell_trace reason)592dca54a7bSDavid Howells void afs_unuse_cell(struct afs_net *net, struct afs_cell *cell, enum afs_cell_trace reason)
593989782dcSDavid Howells {
594acc080d1SDavid Howells unsigned int debug_id;
595989782dcSDavid Howells time64_t now, expire_delay;
596c56f9ec8SDavid Howells int r, a;
597989782dcSDavid Howells
598989782dcSDavid Howells if (!cell)
599989782dcSDavid Howells return;
600989782dcSDavid Howells
601989782dcSDavid Howells _enter("%s", cell->name);
602989782dcSDavid Howells
603989782dcSDavid Howells now = ktime_get_real_seconds();
604989782dcSDavid Howells cell->last_inactive = now;
605989782dcSDavid Howells expire_delay = 0;
606d5c32c89SDavid Howells if (cell->vl_servers->nr_servers)
607989782dcSDavid Howells expire_delay = afs_cell_gc_delay;
608989782dcSDavid Howells
609acc080d1SDavid Howells debug_id = cell->debug_id;
610c56f9ec8SDavid Howells r = refcount_read(&cell->ref);
61188c853c3SDavid Howells a = atomic_dec_return(&cell->active);
612c56f9ec8SDavid Howells trace_afs_cell(debug_id, r, a, reason);
61388c853c3SDavid Howells WARN_ON(a == 0);
61488c853c3SDavid Howells if (a == 1)
615989782dcSDavid Howells /* 'cell' may now be garbage collected. */
616989782dcSDavid Howells afs_set_cell_timer(net, expire_delay);
617989782dcSDavid Howells }
618989782dcSDavid Howells
619989782dcSDavid Howells /*
620dca54a7bSDavid Howells * Note that a cell has been seen.
621dca54a7bSDavid Howells */
afs_see_cell(struct afs_cell * cell,enum afs_cell_trace reason)622dca54a7bSDavid Howells void afs_see_cell(struct afs_cell *cell, enum afs_cell_trace reason)
623dca54a7bSDavid Howells {
624c56f9ec8SDavid Howells int r, a;
625dca54a7bSDavid Howells
626c56f9ec8SDavid Howells r = refcount_read(&cell->ref);
627dca54a7bSDavid Howells a = atomic_read(&cell->active);
628c56f9ec8SDavid Howells trace_afs_cell(cell->debug_id, r, a, reason);
629dca54a7bSDavid Howells }
630dca54a7bSDavid Howells
631dca54a7bSDavid Howells /*
63288c853c3SDavid Howells * Queue a cell for management, giving the workqueue a ref to hold.
63388c853c3SDavid Howells */
afs_queue_cell(struct afs_cell * cell,enum afs_cell_trace reason)634dca54a7bSDavid Howells void afs_queue_cell(struct afs_cell *cell, enum afs_cell_trace reason)
63588c853c3SDavid Howells {
636dca54a7bSDavid Howells afs_get_cell(cell, reason);
63788c853c3SDavid Howells if (!queue_work(afs_wq, &cell->manager))
638dca54a7bSDavid Howells afs_put_cell(cell, afs_cell_trace_put_queue_fail);
63988c853c3SDavid Howells }
64088c853c3SDavid Howells
64188c853c3SDavid Howells /*
642989782dcSDavid Howells * Allocate a key to use as a placeholder for anonymous user security.
643989782dcSDavid Howells */
afs_alloc_anon_key(struct afs_cell * cell)644989782dcSDavid Howells static int afs_alloc_anon_key(struct afs_cell *cell)
645989782dcSDavid Howells {
646989782dcSDavid Howells struct key *key;
647989782dcSDavid Howells char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp;
648989782dcSDavid Howells
649989782dcSDavid Howells /* Create a key to represent an anonymous user. */
650989782dcSDavid Howells memcpy(keyname, "afs@", 4);
651989782dcSDavid Howells dp = keyname + 4;
652989782dcSDavid Howells cp = cell->name;
653989782dcSDavid Howells do {
654989782dcSDavid Howells *dp++ = tolower(*cp);
655989782dcSDavid Howells } while (*cp++);
656989782dcSDavid Howells
657989782dcSDavid Howells key = rxrpc_get_null_key(keyname);
658989782dcSDavid Howells if (IS_ERR(key))
659989782dcSDavid Howells return PTR_ERR(key);
660989782dcSDavid Howells
661989782dcSDavid Howells cell->anonymous_key = key;
662989782dcSDavid Howells
663989782dcSDavid Howells _debug("anon key %p{%x}",
664989782dcSDavid Howells cell->anonymous_key, key_serial(cell->anonymous_key));
665989782dcSDavid Howells return 0;
666989782dcSDavid Howells }
667989782dcSDavid Howells
668989782dcSDavid Howells /*
669989782dcSDavid Howells * Activate a cell.
670989782dcSDavid Howells */
afs_activate_cell(struct afs_net * net,struct afs_cell * cell)671989782dcSDavid Howells static int afs_activate_cell(struct afs_net *net, struct afs_cell *cell)
672989782dcSDavid Howells {
6736b3944e4SDavid Howells struct hlist_node **p;
6746b3944e4SDavid Howells struct afs_cell *pcell;
675989782dcSDavid Howells int ret;
676989782dcSDavid Howells
677989782dcSDavid Howells if (!cell->anonymous_key) {
678989782dcSDavid Howells ret = afs_alloc_anon_key(cell);
679989782dcSDavid Howells if (ret < 0)
680989782dcSDavid Howells return ret;
681989782dcSDavid Howells }
682989782dcSDavid Howells
6835b86d4ffSDavid Howells ret = afs_proc_cell_setup(cell);
684989782dcSDavid Howells if (ret < 0)
685989782dcSDavid Howells return ret;
6860da0b7fdSDavid Howells
6870da0b7fdSDavid Howells mutex_lock(&net->proc_cells_lock);
6886b3944e4SDavid Howells for (p = &net->proc_cells.first; *p; p = &(*p)->next) {
6896b3944e4SDavid Howells pcell = hlist_entry(*p, struct afs_cell, proc_link);
6906b3944e4SDavid Howells if (strcmp(cell->name, pcell->name) < 0)
6916b3944e4SDavid Howells break;
6926b3944e4SDavid Howells }
6936b3944e4SDavid Howells
6946b3944e4SDavid Howells cell->proc_link.pprev = p;
6956b3944e4SDavid Howells cell->proc_link.next = *p;
6966b3944e4SDavid Howells rcu_assign_pointer(*p, &cell->proc_link.next);
6976b3944e4SDavid Howells if (cell->proc_link.next)
6986b3944e4SDavid Howells cell->proc_link.next->pprev = &cell->proc_link.next;
6996b3944e4SDavid Howells
7000da0b7fdSDavid Howells afs_dynroot_mkdir(net, cell);
7010da0b7fdSDavid Howells mutex_unlock(&net->proc_cells_lock);
702989782dcSDavid Howells return 0;
703989782dcSDavid Howells }
704989782dcSDavid Howells
705989782dcSDavid Howells /*
706989782dcSDavid Howells * Deactivate a cell.
707989782dcSDavid Howells */
afs_deactivate_cell(struct afs_net * net,struct afs_cell * cell)708989782dcSDavid Howells static void afs_deactivate_cell(struct afs_net *net, struct afs_cell *cell)
709989782dcSDavid Howells {
710989782dcSDavid Howells _enter("%s", cell->name);
711989782dcSDavid Howells
7125b86d4ffSDavid Howells afs_proc_cell_remove(cell);
713989782dcSDavid Howells
7140da0b7fdSDavid Howells mutex_lock(&net->proc_cells_lock);
7156b3944e4SDavid Howells hlist_del_rcu(&cell->proc_link);
7160da0b7fdSDavid Howells afs_dynroot_rmdir(net, cell);
7170da0b7fdSDavid Howells mutex_unlock(&net->proc_cells_lock);
718989782dcSDavid Howells
719989782dcSDavid Howells _leave("");
720989782dcSDavid Howells }
721989782dcSDavid Howells
722989782dcSDavid Howells /*
723989782dcSDavid Howells * Manage a cell record, initialising and destroying it, maintaining its DNS
724989782dcSDavid Howells * records.
725989782dcSDavid Howells */
afs_manage_cell(struct afs_cell * cell)72688c853c3SDavid Howells static void afs_manage_cell(struct afs_cell *cell)
727989782dcSDavid Howells {
728989782dcSDavid Howells struct afs_net *net = cell->net;
72988c853c3SDavid Howells int ret, active;
730989782dcSDavid Howells
731989782dcSDavid Howells _enter("%s", cell->name);
732989782dcSDavid Howells
733989782dcSDavid Howells again:
734989782dcSDavid Howells _debug("state %u", cell->state);
735989782dcSDavid Howells switch (cell->state) {
736989782dcSDavid Howells case AFS_CELL_INACTIVE:
737989782dcSDavid Howells case AFS_CELL_FAILED:
73892e3cc91SDavid Howells down_write(&net->cells_lock);
73988c853c3SDavid Howells active = 1;
7401d0e850aSDavid Howells if (atomic_try_cmpxchg_relaxed(&cell->active, &active, 0)) {
741989782dcSDavid Howells rb_erase(&cell->net_node, &net->cells);
742c56f9ec8SDavid Howells trace_afs_cell(cell->debug_id, refcount_read(&cell->ref), 0,
743dca54a7bSDavid Howells afs_cell_trace_unuse_delete);
7441d0e850aSDavid Howells smp_store_release(&cell->state, AFS_CELL_REMOVED);
74588c853c3SDavid Howells }
74692e3cc91SDavid Howells up_write(&net->cells_lock);
7471d0e850aSDavid Howells if (cell->state == AFS_CELL_REMOVED) {
7481d0e850aSDavid Howells wake_up_var(&cell->state);
749989782dcSDavid Howells goto final_destruction;
7501d0e850aSDavid Howells }
751989782dcSDavid Howells if (cell->state == AFS_CELL_FAILED)
752989782dcSDavid Howells goto done;
753d5c32c89SDavid Howells smp_store_release(&cell->state, AFS_CELL_UNSET);
754d5c32c89SDavid Howells wake_up_var(&cell->state);
755989782dcSDavid Howells goto again;
756989782dcSDavid Howells
757989782dcSDavid Howells case AFS_CELL_UNSET:
758d5c32c89SDavid Howells smp_store_release(&cell->state, AFS_CELL_ACTIVATING);
759d5c32c89SDavid Howells wake_up_var(&cell->state);
760989782dcSDavid Howells goto again;
761989782dcSDavid Howells
762989782dcSDavid Howells case AFS_CELL_ACTIVATING:
763989782dcSDavid Howells ret = afs_activate_cell(net, cell);
764989782dcSDavid Howells if (ret < 0)
765989782dcSDavid Howells goto activation_failed;
766989782dcSDavid Howells
767d5c32c89SDavid Howells smp_store_release(&cell->state, AFS_CELL_ACTIVE);
768d5c32c89SDavid Howells wake_up_var(&cell->state);
769989782dcSDavid Howells goto again;
770989782dcSDavid Howells
771989782dcSDavid Howells case AFS_CELL_ACTIVE:
77288c853c3SDavid Howells if (atomic_read(&cell->active) > 1) {
773d5c32c89SDavid Howells if (test_and_clear_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags)) {
774d5c32c89SDavid Howells ret = afs_update_cell(cell);
775d5c32c89SDavid Howells if (ret < 0)
776d5c32c89SDavid Howells cell->error = ret;
777d5c32c89SDavid Howells }
778989782dcSDavid Howells goto done;
779989782dcSDavid Howells }
780d5c32c89SDavid Howells smp_store_release(&cell->state, AFS_CELL_DEACTIVATING);
781d5c32c89SDavid Howells wake_up_var(&cell->state);
782989782dcSDavid Howells goto again;
783989782dcSDavid Howells
784989782dcSDavid Howells case AFS_CELL_DEACTIVATING:
78588c853c3SDavid Howells if (atomic_read(&cell->active) > 1)
786989782dcSDavid Howells goto reverse_deactivation;
787989782dcSDavid Howells afs_deactivate_cell(net, cell);
788d5c32c89SDavid Howells smp_store_release(&cell->state, AFS_CELL_INACTIVE);
789d5c32c89SDavid Howells wake_up_var(&cell->state);
790989782dcSDavid Howells goto again;
791989782dcSDavid Howells
7921d0e850aSDavid Howells case AFS_CELL_REMOVED:
7931d0e850aSDavid Howells goto done;
7941d0e850aSDavid Howells
795989782dcSDavid Howells default:
796989782dcSDavid Howells break;
797989782dcSDavid Howells }
798989782dcSDavid Howells _debug("bad state %u", cell->state);
799989782dcSDavid Howells BUG(); /* Unhandled state */
800989782dcSDavid Howells
801989782dcSDavid Howells activation_failed:
802989782dcSDavid Howells cell->error = ret;
803989782dcSDavid Howells afs_deactivate_cell(net, cell);
804989782dcSDavid Howells
805d5c32c89SDavid Howells smp_store_release(&cell->state, AFS_CELL_FAILED); /* vs error */
806d5c32c89SDavid Howells wake_up_var(&cell->state);
807989782dcSDavid Howells goto again;
808989782dcSDavid Howells
809989782dcSDavid Howells reverse_deactivation:
810d5c32c89SDavid Howells smp_store_release(&cell->state, AFS_CELL_ACTIVE);
811d5c32c89SDavid Howells wake_up_var(&cell->state);
812989782dcSDavid Howells _leave(" [deact->act]");
813989782dcSDavid Howells return;
814989782dcSDavid Howells
815989782dcSDavid Howells done:
816989782dcSDavid Howells _leave(" [done %u]", cell->state);
817989782dcSDavid Howells return;
818989782dcSDavid Howells
819989782dcSDavid Howells final_destruction:
82088c853c3SDavid Howells /* The root volume is pinning the cell */
82188c853c3SDavid Howells afs_put_volume(cell->net, cell->root_volume, afs_volume_trace_put_cell_root);
82288c853c3SDavid Howells cell->root_volume = NULL;
823dca54a7bSDavid Howells afs_put_cell(cell, afs_cell_trace_put_destroy);
82488c853c3SDavid Howells }
82588c853c3SDavid Howells
afs_manage_cell_work(struct work_struct * work)82688c853c3SDavid Howells static void afs_manage_cell_work(struct work_struct *work)
82788c853c3SDavid Howells {
82888c853c3SDavid Howells struct afs_cell *cell = container_of(work, struct afs_cell, manager);
82988c853c3SDavid Howells
83088c853c3SDavid Howells afs_manage_cell(cell);
831dca54a7bSDavid Howells afs_put_cell(cell, afs_cell_trace_put_queue_work);
832989782dcSDavid Howells }
833989782dcSDavid Howells
834989782dcSDavid Howells /*
835989782dcSDavid Howells * Manage the records of cells known to a network namespace. This includes
836989782dcSDavid Howells * updating the DNS records and garbage collecting unused cells that were
837989782dcSDavid Howells * automatically added.
838989782dcSDavid Howells *
839989782dcSDavid Howells * Note that constructed cell records may only be removed from net->cells by
840989782dcSDavid Howells * this work item, so it is safe for this work item to stash a cursor pointing
841989782dcSDavid Howells * into the tree and then return to caller (provided it skips cells that are
842989782dcSDavid Howells * still under construction).
843989782dcSDavid Howells *
844989782dcSDavid Howells * Note also that we were given an increment on net->cells_outstanding by
845989782dcSDavid Howells * whoever queued us that we need to deal with before returning.
846989782dcSDavid Howells */
afs_manage_cells(struct work_struct * work)847989782dcSDavid Howells void afs_manage_cells(struct work_struct *work)
848989782dcSDavid Howells {
849989782dcSDavid Howells struct afs_net *net = container_of(work, struct afs_net, cells_manager);
850989782dcSDavid Howells struct rb_node *cursor;
851989782dcSDavid Howells time64_t now = ktime_get_real_seconds(), next_manage = TIME64_MAX;
852989782dcSDavid Howells bool purging = !net->live;
8531da177e4SLinus Torvalds
8541da177e4SLinus Torvalds _enter("");
8551da177e4SLinus Torvalds
856989782dcSDavid Howells /* Trawl the cell database looking for cells that have expired from
857989782dcSDavid Howells * lack of use and cells whose DNS results have expired and dispatch
858989782dcSDavid Howells * their managers.
859989782dcSDavid Howells */
86092e3cc91SDavid Howells down_read(&net->cells_lock);
8611da177e4SLinus Torvalds
862989782dcSDavid Howells for (cursor = rb_first(&net->cells); cursor; cursor = rb_next(cursor)) {
863989782dcSDavid Howells struct afs_cell *cell =
864989782dcSDavid Howells rb_entry(cursor, struct afs_cell, net_node);
86588c853c3SDavid Howells unsigned active;
866989782dcSDavid Howells bool sched_cell = false;
86708e0e7c8SDavid Howells
86888c853c3SDavid Howells active = atomic_read(&cell->active);
869c56f9ec8SDavid Howells trace_afs_cell(cell->debug_id, refcount_read(&cell->ref),
870dca54a7bSDavid Howells active, afs_cell_trace_manage);
8711da177e4SLinus Torvalds
87288c853c3SDavid Howells ASSERTCMP(active, >=, 1);
8731da177e4SLinus Torvalds
874989782dcSDavid Howells if (purging) {
875dca54a7bSDavid Howells if (test_and_clear_bit(AFS_CELL_FL_NO_GC, &cell->flags)) {
876dca54a7bSDavid Howells active = atomic_dec_return(&cell->active);
877c56f9ec8SDavid Howells trace_afs_cell(cell->debug_id, refcount_read(&cell->ref),
878dca54a7bSDavid Howells active, afs_cell_trace_unuse_pin);
879dca54a7bSDavid Howells }
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds
88288c853c3SDavid Howells if (active == 1) {
883d5c32c89SDavid Howells struct afs_vlserver_list *vllist;
884989782dcSDavid Howells time64_t expire_at = cell->last_inactive;
8851da177e4SLinus Torvalds
886d5c32c89SDavid Howells read_lock(&cell->vl_servers_lock);
887d5c32c89SDavid Howells vllist = rcu_dereference_protected(
888d5c32c89SDavid Howells cell->vl_servers,
889d5c32c89SDavid Howells lockdep_is_held(&cell->vl_servers_lock));
890d5c32c89SDavid Howells if (vllist->nr_servers > 0)
891989782dcSDavid Howells expire_at += afs_cell_gc_delay;
892d5c32c89SDavid Howells read_unlock(&cell->vl_servers_lock);
893989782dcSDavid Howells if (purging || expire_at <= now)
894989782dcSDavid Howells sched_cell = true;
895989782dcSDavid Howells else if (expire_at < next_manage)
896989782dcSDavid Howells next_manage = expire_at;
897989782dcSDavid Howells }
8981da177e4SLinus Torvalds
899989782dcSDavid Howells if (!purging) {
900d5c32c89SDavid Howells if (test_bit(AFS_CELL_FL_DO_LOOKUP, &cell->flags))
901989782dcSDavid Howells sched_cell = true;
902989782dcSDavid Howells }
903989782dcSDavid Howells
904989782dcSDavid Howells if (sched_cell)
905dca54a7bSDavid Howells afs_queue_cell(cell, afs_cell_trace_get_queue_manage);
906989782dcSDavid Howells }
907989782dcSDavid Howells
90892e3cc91SDavid Howells up_read(&net->cells_lock);
909989782dcSDavid Howells
910989782dcSDavid Howells /* Update the timer on the way out. We have to pass an increment on
911989782dcSDavid Howells * cells_outstanding in the namespace that we are in to the timer or
912989782dcSDavid Howells * the work scheduler.
913989782dcSDavid Howells */
914989782dcSDavid Howells if (!purging && next_manage < TIME64_MAX) {
915989782dcSDavid Howells now = ktime_get_real_seconds();
916989782dcSDavid Howells
917989782dcSDavid Howells if (next_manage - now <= 0) {
918989782dcSDavid Howells if (queue_work(afs_wq, &net->cells_manager))
919989782dcSDavid Howells atomic_inc(&net->cells_outstanding);
920989782dcSDavid Howells } else {
921989782dcSDavid Howells afs_set_cell_timer(net, next_manage - now);
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds }
9241da177e4SLinus Torvalds
925989782dcSDavid Howells afs_dec_cells_outstanding(net);
926989782dcSDavid Howells _leave(" [%d]", atomic_read(&net->cells_outstanding));
927989782dcSDavid Howells }
928989782dcSDavid Howells
929989782dcSDavid Howells /*
930989782dcSDavid Howells * Purge in-memory cell database.
931989782dcSDavid Howells */
afs_cell_purge(struct afs_net * net)932989782dcSDavid Howells void afs_cell_purge(struct afs_net *net)
933989782dcSDavid Howells {
934989782dcSDavid Howells struct afs_cell *ws;
935989782dcSDavid Howells
936989782dcSDavid Howells _enter("");
937989782dcSDavid Howells
93892e3cc91SDavid Howells down_write(&net->cells_lock);
93992e3cc91SDavid Howells ws = net->ws_cell;
94092e3cc91SDavid Howells net->ws_cell = NULL;
94192e3cc91SDavid Howells up_write(&net->cells_lock);
942dca54a7bSDavid Howells afs_unuse_cell(net, ws, afs_cell_trace_unuse_ws);
943989782dcSDavid Howells
944989782dcSDavid Howells _debug("del timer");
945989782dcSDavid Howells if (del_timer_sync(&net->cells_timer))
946989782dcSDavid Howells atomic_dec(&net->cells_outstanding);
947989782dcSDavid Howells
948989782dcSDavid Howells _debug("kick mgr");
949989782dcSDavid Howells afs_queue_cell_manager(net);
950989782dcSDavid Howells
951989782dcSDavid Howells _debug("wait");
952ab1fbe32SPeter Zijlstra wait_var_event(&net->cells_outstanding,
953ab1fbe32SPeter Zijlstra !atomic_read(&net->cells_outstanding));
9541da177e4SLinus Torvalds _leave("");
955ec26815aSDavid Howells }
956