blob: bd570fa539a0eab2fdedeb6fb8f06d29c2b8cf43 [file] [log] [blame]
David Howellsec268152007-04-26 15:49:28 -07001/* AFS cell and server record management
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
David Howells00d3b7a2007-04-26 15:57:07 -070014#include <linux/key.h>
15#include <linux/ctype.h>
Wang Lei07567a52010-08-04 15:16:38 +010016#include <linux/dns_resolver.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040017#include <linux/sched.h>
David Howells00d3b7a2007-04-26 15:57:07 -070018#include <keys/rxrpc-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "internal.h"
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/*
David Howells00d3b7a2007-04-26 15:57:07 -070022 * allocate a cell record and fill in its name, VL server address list and
23 * allocate an anonymous key
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
David Howellsf044c882017-11-02 15:27:45 +000025static struct afs_cell *afs_cell_alloc(struct afs_net *net,
26 const char *name, unsigned namelen,
wangleibec5eb62010-08-11 09:38:04 +010027 char *vllist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
29 struct afs_cell *cell;
David Howells76181c12007-10-16 23:29:46 -070030 struct key *key;
David Howells00d3b7a2007-04-26 15:57:07 -070031 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
Wang Lei07567a52010-08-04 15:16:38 +010032 char *dvllist = NULL, *_vllist = NULL;
33 char delimiter = ':';
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 int ret;
35
wangleibec5eb62010-08-11 09:38:04 +010036 _enter("%*.*s,%s", namelen, namelen, name ?: "", vllist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
39
Wang Lei07567a52010-08-04 15:16:38 +010040 if (namelen > AFS_MAXCELLNAME) {
41 _leave(" = -ENAMETOOLONG");
David Howells00d3b7a2007-04-26 15:57:07 -070042 return ERR_PTR(-ENAMETOOLONG);
Wang Lei07567a52010-08-04 15:16:38 +010043 }
David Howells00d3b7a2007-04-26 15:57:07 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 /* allocate and initialise a cell record */
David Howells00d3b7a2007-04-26 15:57:07 -070046 cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 if (!cell) {
48 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -070049 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 }
51
David Howells00d3b7a2007-04-26 15:57:07 -070052 memcpy(cell->name, name, namelen);
53 cell->name[namelen] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David Howells08e0e7c2007-04-26 15:55:03 -070055 atomic_set(&cell->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 INIT_LIST_HEAD(&cell->link);
David Howellsf044c882017-11-02 15:27:45 +000057 cell->net = net;
David Howells08e0e7c2007-04-26 15:55:03 -070058 rwlock_init(&cell->servers_lock);
59 INIT_LIST_HEAD(&cell->servers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 init_rwsem(&cell->vl_sem);
61 INIT_LIST_HEAD(&cell->vl_list);
David Howells08e0e7c2007-04-26 15:55:03 -070062 spin_lock_init(&cell->vl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Wang Lei07567a52010-08-04 15:16:38 +010064 /* if the ip address is invalid, try dns query */
65 if (!vllist || strlen(vllist) < 7) {
66 ret = dns_query("afsdb", name, namelen, "ipv4", &dvllist, NULL);
67 if (ret < 0) {
Wang Lei4a2d7892010-08-11 09:37:58 +010068 if (ret == -ENODATA || ret == -EAGAIN || ret == -ENOKEY)
69 /* translate these errors into something
70 * userspace might understand */
71 ret = -EDESTADDRREQ;
Wang Lei07567a52010-08-04 15:16:38 +010072 _leave(" = %d", ret);
73 return ERR_PTR(ret);
74 }
75 _vllist = dvllist;
76
77 /* change the delimiter for user-space reply */
78 delimiter = ',';
79
80 } else {
81 _vllist = vllist;
82 }
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /* fill in the VL server list from the rest of the string */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 do {
86 unsigned a, b, c, d;
87
Wang Lei07567a52010-08-04 15:16:38 +010088 next = strchr(_vllist, delimiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (next)
90 *next++ = 0;
91
Wang Lei07567a52010-08-04 15:16:38 +010092 if (sscanf(_vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
David Howells00d3b7a2007-04-26 15:57:07 -070093 goto bad_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if (a > 255 || b > 255 || c > 255 || d > 255)
David Howells00d3b7a2007-04-26 15:57:07 -070096 goto bad_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 cell->vl_addrs[cell->vl_naddrs++].s_addr =
99 htonl((a << 24) | (b << 16) | (c << 8) | d);
100
Wang Lei07567a52010-08-04 15:16:38 +0100101 } while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (_vllist = next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
David Howells00d3b7a2007-04-26 15:57:07 -0700103 /* create a key to represent an anonymous user */
104 memcpy(keyname, "afs@", 4);
105 dp = keyname + 4;
106 cp = cell->name;
107 do {
108 *dp++ = toupper(*cp);
109 } while (*cp++);
David Howells00d3b7a2007-04-26 15:57:07 -0700110
David Howells76181c12007-10-16 23:29:46 -0700111 key = rxrpc_get_null_key(keyname);
112 if (IS_ERR(key)) {
113 _debug("no key");
114 ret = PTR_ERR(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700115 goto error;
116 }
David Howells76181c12007-10-16 23:29:46 -0700117 cell->anonymous_key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700118
119 _debug("anon key %p{%x}",
120 cell->anonymous_key, key_serial(cell->anonymous_key));
121
122 _leave(" = %p", cell);
123 return cell;
124
125bad_address:
126 printk(KERN_ERR "kAFS: bad VL server IP address\n");
127 ret = -EINVAL;
128error:
129 key_put(cell->anonymous_key);
Wang Lei07567a52010-08-04 15:16:38 +0100130 kfree(dvllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700131 kfree(cell);
132 _leave(" = %d", ret);
133 return ERR_PTR(ret);
134}
135
136/*
wangleibec5eb62010-08-11 09:38:04 +0100137 * afs_cell_crate() - create a cell record
David Howellsf044c882017-11-02 15:27:45 +0000138 * @net: The network namespace
wangleibec5eb62010-08-11 09:38:04 +0100139 * @name: is the name of the cell.
140 * @namsesz: is the strlen of the cell name.
141 * @vllist: is a colon separated list of IP addresses in "a.b.c.d" format.
142 * @retref: is T to return the cell reference when the cell exists.
David Howells00d3b7a2007-04-26 15:57:07 -0700143 */
David Howellsf044c882017-11-02 15:27:45 +0000144struct afs_cell *afs_cell_create(struct afs_net *net,
145 const char *name, unsigned namesz,
wangleibec5eb62010-08-11 09:38:04 +0100146 char *vllist, bool retref)
David Howells00d3b7a2007-04-26 15:57:07 -0700147{
148 struct afs_cell *cell;
149 int ret;
150
wangleibec5eb62010-08-11 09:38:04 +0100151 _enter("%*.*s,%s", namesz, namesz, name ?: "", vllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700152
David Howellsf044c882017-11-02 15:27:45 +0000153 down_write(&net->cells_sem);
154 read_lock(&net->cells_lock);
155 list_for_each_entry(cell, &net->cells, link) {
wangleibec5eb62010-08-11 09:38:04 +0100156 if (strncasecmp(cell->name, name, namesz) == 0)
Sven Schnelle5214b722008-03-28 14:15:55 -0700157 goto duplicate_name;
158 }
David Howellsf044c882017-11-02 15:27:45 +0000159 read_unlock(&net->cells_lock);
Sven Schnelle5214b722008-03-28 14:15:55 -0700160
David Howellsf044c882017-11-02 15:27:45 +0000161 cell = afs_cell_alloc(net, name, namesz, vllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700162 if (IS_ERR(cell)) {
163 _leave(" = %ld", PTR_ERR(cell));
David Howellsf044c882017-11-02 15:27:45 +0000164 up_write(&net->cells_sem);
David Howells00d3b7a2007-04-26 15:57:07 -0700165 return cell;
166 }
167
David Howells08e0e7c2007-04-26 15:55:03 -0700168 /* add a proc directory for this cell */
David Howellsf044c882017-11-02 15:27:45 +0000169 ret = afs_proc_cell_setup(net, cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (ret < 0)
171 goto error;
172
David Howells9b3f26c2009-04-03 16:42:41 +0100173#ifdef CONFIG_AFS_FSCACHE
174 /* put it up for caching (this never returns an error) */
175 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
176 &afs_cell_cache_index_def,
David Howells94d30ae2013-09-21 00:09:31 +0100177 cell, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#endif
179
180 /* add to the cell lists */
David Howellsf044c882017-11-02 15:27:45 +0000181 write_lock(&net->cells_lock);
182 list_add_tail(&cell->link, &net->cells);
183 write_unlock(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
David Howellsf044c882017-11-02 15:27:45 +0000185 down_write(&net->proc_cells_sem);
186 list_add_tail(&cell->proc_link, &net->proc_cells);
187 up_write(&net->proc_cells_sem);
188 up_write(&net->cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
David Howells08e0e7c2007-04-26 15:55:03 -0700190 _leave(" = %p", cell);
191 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
David Howellsec268152007-04-26 15:49:28 -0700193error:
David Howellsf044c882017-11-02 15:27:45 +0000194 up_write(&net->cells_sem);
David Howells00d3b7a2007-04-26 15:57:07 -0700195 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 kfree(cell);
197 _leave(" = %d", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700198 return ERR_PTR(ret);
Sven Schnelle5214b722008-03-28 14:15:55 -0700199
200duplicate_name:
wangleibec5eb62010-08-11 09:38:04 +0100201 if (retref && !IS_ERR(cell))
202 afs_get_cell(cell);
203
David Howellsf044c882017-11-02 15:27:45 +0000204 read_unlock(&net->cells_lock);
205 up_write(&net->cells_sem);
wangleibec5eb62010-08-11 09:38:04 +0100206
207 if (retref) {
208 _leave(" = %p", cell);
209 return cell;
210 }
211
212 _leave(" = -EEXIST");
Sven Schnelle5214b722008-03-28 14:15:55 -0700213 return ERR_PTR(-EEXIST);
David Howellsec268152007-04-26 15:49:28 -0700214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
David Howells08e0e7c2007-04-26 15:55:03 -0700217 * set the root cell information
218 * - can be called with a module parameter string
219 * - can be called from a write to /proc/fs/afs/rootcell
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
David Howellsf044c882017-11-02 15:27:45 +0000221int afs_cell_init(struct afs_net *net, char *rootcell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct afs_cell *old_root, *new_root;
224 char *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 _enter("");
227
228 if (!rootcell) {
229 /* module is loaded with no parameters, or built statically.
230 * - in the future we might initialize cell DB here.
231 */
David Howells08e0e7c2007-04-26 15:55:03 -0700232 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return 0;
234 }
235
236 cp = strchr(rootcell, ':');
Wang Lei07567a52010-08-04 15:16:38 +0100237 if (!cp)
238 _debug("kAFS: no VL server IP addresses specified");
239 else
240 *cp++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* allocate a cell record for the root cell */
David Howellsf044c882017-11-02 15:27:45 +0000243 new_root = afs_cell_create(net, rootcell, strlen(rootcell), cp, false);
David Howells08e0e7c2007-04-26 15:55:03 -0700244 if (IS_ERR(new_root)) {
245 _leave(" = %ld", PTR_ERR(new_root));
246 return PTR_ERR(new_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
David Howells08e0e7c2007-04-26 15:55:03 -0700249 /* install the new cell */
David Howellsf044c882017-11-02 15:27:45 +0000250 write_lock(&net->cells_lock);
251 old_root = net->ws_cell;
252 net->ws_cell = new_root;
253 write_unlock(&net->cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700254 afs_put_cell(old_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
David Howells08e0e7c2007-04-26 15:55:03 -0700256 _leave(" = 0");
257 return 0;
David Howellsec268152007-04-26 15:49:28 -0700258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/*
261 * lookup a cell record
262 */
David Howellsf044c882017-11-02 15:27:45 +0000263struct afs_cell *afs_cell_lookup(struct afs_net *net,
264 const char *name, unsigned namesz,
wangleibec5eb62010-08-11 09:38:04 +0100265 bool dns_cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct afs_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
wangleibec5eb62010-08-11 09:38:04 +0100269 _enter("\"%*.*s\",", namesz, namesz, name ?: "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
David Howellsf044c882017-11-02 15:27:45 +0000271 down_read(&net->cells_sem);
272 read_lock(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (name) {
275 /* if the cell was named, look for it in the cell record list */
David Howellsf044c882017-11-02 15:27:45 +0000276 list_for_each_entry(cell, &net->cells, link) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (strncmp(cell->name, name, namesz) == 0) {
278 afs_get_cell(cell);
279 goto found;
280 }
281 }
David Howells08e0e7c2007-04-26 15:55:03 -0700282 cell = ERR_PTR(-ENOENT);
wangleibec5eb62010-08-11 09:38:04 +0100283 if (dns_cell)
284 goto create_cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 found:
David Howells08e0e7c2007-04-26 15:55:03 -0700286 ;
David Howellsec268152007-04-26 15:49:28 -0700287 } else {
David Howellsf044c882017-11-02 15:27:45 +0000288 cell = net->ws_cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (!cell) {
290 /* this should not happen unless user tries to mount
291 * when root cell is not set. Return an impossibly
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300292 * bizarre errno to alert the user. Things like
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * ENOENT might be "more appropriate" but they happen
294 * for other reasons.
295 */
David Howells08e0e7c2007-04-26 15:55:03 -0700296 cell = ERR_PTR(-EDESTADDRREQ);
David Howellsec268152007-04-26 15:49:28 -0700297 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 afs_get_cell(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302
David Howellsf044c882017-11-02 15:27:45 +0000303 read_unlock(&net->cells_lock);
304 up_read(&net->cells_sem);
David Howells08e0e7c2007-04-26 15:55:03 -0700305 _leave(" = %p", cell);
306 return cell;
wangleibec5eb62010-08-11 09:38:04 +0100307
308create_cell:
David Howellsf044c882017-11-02 15:27:45 +0000309 read_unlock(&net->cells_lock);
310 up_read(&net->cells_sem);
wangleibec5eb62010-08-11 09:38:04 +0100311
David Howellsf044c882017-11-02 15:27:45 +0000312 cell = afs_cell_create(net, name, namesz, NULL, true);
wangleibec5eb62010-08-11 09:38:04 +0100313
314 _leave(" = %p", cell);
315 return cell;
David Howellsec268152007-04-26 15:49:28 -0700316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Adrian Bunkc1206a22007-10-16 23:26:41 -0700318#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/*
320 * try and get a cell record
321 */
David Howells08e0e7c2007-04-26 15:55:03 -0700322struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
David Howellsf044c882017-11-02 15:27:45 +0000324 write_lock(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (cell && !list_empty(&cell->link))
327 afs_get_cell(cell);
328 else
329 cell = NULL;
330
David Howellsf044c882017-11-02 15:27:45 +0000331 write_unlock(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return cell;
David Howellsec268152007-04-26 15:49:28 -0700333}
Adrian Bunkc1206a22007-10-16 23:26:41 -0700334#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/*
337 * destroy a cell record
338 */
339void afs_put_cell(struct afs_cell *cell)
340{
341 if (!cell)
342 return;
343
344 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
345
David Howells08e0e7c2007-04-26 15:55:03 -0700346 ASSERTCMP(atomic_read(&cell->usage), >, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* to prevent a race, the decrement and the dequeue must be effectively
349 * atomic */
David Howellsf044c882017-11-02 15:27:45 +0000350 write_lock(&cell->net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (likely(!atomic_dec_and_test(&cell->usage))) {
David Howellsf044c882017-11-02 15:27:45 +0000353 write_unlock(&cell->net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 _leave("");
355 return;
356 }
357
David Howells08e0e7c2007-04-26 15:55:03 -0700358 ASSERT(list_empty(&cell->servers));
359 ASSERT(list_empty(&cell->vl_list));
360
David Howellsf044c882017-11-02 15:27:45 +0000361 wake_up(&cell->net->cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David Howellsf044c882017-11-02 15:27:45 +0000363 write_unlock(&cell->net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 _leave(" [unused]");
David Howellsec268152007-04-26 15:49:28 -0700366}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
369 * destroy a cell record
David Howellsf044c882017-11-02 15:27:45 +0000370 * - must be called with the net->cells_sem write-locked
David Howells08e0e7c2007-04-26 15:55:03 -0700371 * - cell->link should have been broken by the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
David Howellsf044c882017-11-02 15:27:45 +0000373static void afs_cell_destroy(struct afs_net *net, struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
376
David Howells08e0e7c2007-04-26 15:55:03 -0700377 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
378 ASSERT(list_empty(&cell->link));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David Howells08e0e7c2007-04-26 15:55:03 -0700380 /* wait for everyone to stop using the cell */
381 if (atomic_read(&cell->usage) > 0) {
382 DECLARE_WAITQUEUE(myself, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
David Howells08e0e7c2007-04-26 15:55:03 -0700384 _debug("wait for cell %s", cell->name);
385 set_current_state(TASK_UNINTERRUPTIBLE);
David Howellsf044c882017-11-02 15:27:45 +0000386 add_wait_queue(&net->cells_freeable_wq, &myself);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
David Howells08e0e7c2007-04-26 15:55:03 -0700388 while (atomic_read(&cell->usage) > 0) {
389 schedule();
390 set_current_state(TASK_UNINTERRUPTIBLE);
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
David Howellsf044c882017-11-02 15:27:45 +0000393 remove_wait_queue(&net->cells_freeable_wq, &myself);
David Howells08e0e7c2007-04-26 15:55:03 -0700394 set_current_state(TASK_RUNNING);
395 }
396
397 _debug("cell dead");
398 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
399 ASSERT(list_empty(&cell->servers));
400 ASSERT(list_empty(&cell->vl_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David Howellsf044c882017-11-02 15:27:45 +0000402 afs_proc_cell_remove(net, cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
David Howellsf044c882017-11-02 15:27:45 +0000404 down_write(&net->proc_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 list_del_init(&cell->proc_link);
David Howellsf044c882017-11-02 15:27:45 +0000406 up_write(&net->proc_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
David Howells9b3f26c2009-04-03 16:42:41 +0100408#ifdef CONFIG_AFS_FSCACHE
409 fscache_relinquish_cookie(cell->cache, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410#endif
David Howells00d3b7a2007-04-26 15:57:07 -0700411 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 kfree(cell);
413
414 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700415}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * purge in-memory cell database on module unload or afs_init() failure
419 * - the timeout daemon is stopped before calling this
420 */
David Howellsf044c882017-11-02 15:27:45 +0000421void afs_cell_purge(struct afs_net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct afs_cell *cell;
424
425 _enter("");
426
David Howellsf044c882017-11-02 15:27:45 +0000427 afs_put_cell(net->ws_cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
David Howellsf044c882017-11-02 15:27:45 +0000429 down_write(&net->cells_sem);
David Howells08e0e7c2007-04-26 15:55:03 -0700430
David Howellsf044c882017-11-02 15:27:45 +0000431 while (!list_empty(&net->cells)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 cell = NULL;
433
434 /* remove the next cell from the front of the list */
David Howellsf044c882017-11-02 15:27:45 +0000435 write_lock(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
David Howellsf044c882017-11-02 15:27:45 +0000437 if (!list_empty(&net->cells)) {
438 cell = list_entry(net->cells.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct afs_cell, link);
440 list_del_init(&cell->link);
441 }
442
David Howellsf044c882017-11-02 15:27:45 +0000443 write_unlock(&net->cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (cell) {
446 _debug("PURGING CELL %s (%d)",
447 cell->name, atomic_read(&cell->usage));
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* now the cell should be left with no references */
David Howellsf044c882017-11-02 15:27:45 +0000450 afs_cell_destroy(net, cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452 }
453
David Howellsf044c882017-11-02 15:27:45 +0000454 up_write(&net->cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700456}