blob: ca0a3cf93791879578121b842891e97e5b0ffb1e [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
21DECLARE_RWSEM(afs_proc_cells_sem);
22LIST_HEAD(afs_proc_cells);
23
Robert P. J. Day0ae52d62008-04-29 01:03:20 -070024static LIST_HEAD(afs_cells);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static DEFINE_RWLOCK(afs_cells_lock);
26static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
David Howells08e0e7c2007-04-26 15:55:03 -070027static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static struct afs_cell *afs_cell_root;
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
David Howells00d3b7a2007-04-26 15:57:07 -070031 * allocate a cell record and fill in its name, VL server address list and
32 * allocate an anonymous key
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
wangleibec5eb62010-08-11 09:38:04 +010034static struct afs_cell *afs_cell_alloc(const char *name, unsigned namelen,
35 char *vllist)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
37 struct afs_cell *cell;
David Howells76181c12007-10-16 23:29:46 -070038 struct key *key;
David Howells00d3b7a2007-04-26 15:57:07 -070039 char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next;
Wang Lei07567a52010-08-04 15:16:38 +010040 char *dvllist = NULL, *_vllist = NULL;
41 char delimiter = ':';
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 int ret;
43
wangleibec5eb62010-08-11 09:38:04 +010044 _enter("%*.*s,%s", namelen, namelen, name ?: "", vllist);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46 BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
47
Wang Lei07567a52010-08-04 15:16:38 +010048 if (namelen > AFS_MAXCELLNAME) {
49 _leave(" = -ENAMETOOLONG");
David Howells00d3b7a2007-04-26 15:57:07 -070050 return ERR_PTR(-ENAMETOOLONG);
Wang Lei07567a52010-08-04 15:16:38 +010051 }
David Howells00d3b7a2007-04-26 15:57:07 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 /* allocate and initialise a cell record */
David Howells00d3b7a2007-04-26 15:57:07 -070054 cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (!cell) {
56 _leave(" = -ENOMEM");
David Howells08e0e7c2007-04-26 15:55:03 -070057 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 }
59
David Howells00d3b7a2007-04-26 15:57:07 -070060 memcpy(cell->name, name, namelen);
61 cell->name[namelen] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
David Howells08e0e7c2007-04-26 15:55:03 -070063 atomic_set(&cell->usage, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 INIT_LIST_HEAD(&cell->link);
David Howells08e0e7c2007-04-26 15:55:03 -070065 rwlock_init(&cell->servers_lock);
66 INIT_LIST_HEAD(&cell->servers);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 init_rwsem(&cell->vl_sem);
68 INIT_LIST_HEAD(&cell->vl_list);
David Howells08e0e7c2007-04-26 15:55:03 -070069 spin_lock_init(&cell->vl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Wang Lei07567a52010-08-04 15:16:38 +010071 /* if the ip address is invalid, try dns query */
72 if (!vllist || strlen(vllist) < 7) {
73 ret = dns_query("afsdb", name, namelen, "ipv4", &dvllist, NULL);
74 if (ret < 0) {
Wang Lei4a2d7892010-08-11 09:37:58 +010075 if (ret == -ENODATA || ret == -EAGAIN || ret == -ENOKEY)
76 /* translate these errors into something
77 * userspace might understand */
78 ret = -EDESTADDRREQ;
Wang Lei07567a52010-08-04 15:16:38 +010079 _leave(" = %d", ret);
80 return ERR_PTR(ret);
81 }
82 _vllist = dvllist;
83
84 /* change the delimiter for user-space reply */
85 delimiter = ',';
86
87 } else {
88 _vllist = vllist;
89 }
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 /* fill in the VL server list from the rest of the string */
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 do {
93 unsigned a, b, c, d;
94
Wang Lei07567a52010-08-04 15:16:38 +010095 next = strchr(_vllist, delimiter);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (next)
97 *next++ = 0;
98
Wang Lei07567a52010-08-04 15:16:38 +010099 if (sscanf(_vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
David Howells00d3b7a2007-04-26 15:57:07 -0700100 goto bad_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 if (a > 255 || b > 255 || c > 255 || d > 255)
David Howells00d3b7a2007-04-26 15:57:07 -0700103 goto bad_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 cell->vl_addrs[cell->vl_naddrs++].s_addr =
106 htonl((a << 24) | (b << 16) | (c << 8) | d);
107
Wang Lei07567a52010-08-04 15:16:38 +0100108 } while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (_vllist = next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
David Howells00d3b7a2007-04-26 15:57:07 -0700110 /* create a key to represent an anonymous user */
111 memcpy(keyname, "afs@", 4);
112 dp = keyname + 4;
113 cp = cell->name;
114 do {
115 *dp++ = toupper(*cp);
116 } while (*cp++);
David Howells00d3b7a2007-04-26 15:57:07 -0700117
David Howells76181c12007-10-16 23:29:46 -0700118 key = rxrpc_get_null_key(keyname);
119 if (IS_ERR(key)) {
120 _debug("no key");
121 ret = PTR_ERR(key);
David Howells00d3b7a2007-04-26 15:57:07 -0700122 goto error;
123 }
David Howells76181c12007-10-16 23:29:46 -0700124 cell->anonymous_key = key;
David Howells00d3b7a2007-04-26 15:57:07 -0700125
126 _debug("anon key %p{%x}",
127 cell->anonymous_key, key_serial(cell->anonymous_key));
128
129 _leave(" = %p", cell);
130 return cell;
131
132bad_address:
133 printk(KERN_ERR "kAFS: bad VL server IP address\n");
134 ret = -EINVAL;
135error:
136 key_put(cell->anonymous_key);
Wang Lei07567a52010-08-04 15:16:38 +0100137 kfree(dvllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700138 kfree(cell);
139 _leave(" = %d", ret);
140 return ERR_PTR(ret);
141}
142
143/*
wangleibec5eb62010-08-11 09:38:04 +0100144 * afs_cell_crate() - create a cell record
145 * @name: is the name of the cell.
146 * @namsesz: is the strlen of the cell name.
147 * @vllist: is a colon separated list of IP addresses in "a.b.c.d" format.
148 * @retref: is T to return the cell reference when the cell exists.
David Howells00d3b7a2007-04-26 15:57:07 -0700149 */
wangleibec5eb62010-08-11 09:38:04 +0100150struct afs_cell *afs_cell_create(const char *name, unsigned namesz,
151 char *vllist, bool retref)
David Howells00d3b7a2007-04-26 15:57:07 -0700152{
153 struct afs_cell *cell;
154 int ret;
155
wangleibec5eb62010-08-11 09:38:04 +0100156 _enter("%*.*s,%s", namesz, namesz, name ?: "", vllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700157
Sven Schnelle5214b722008-03-28 14:15:55 -0700158 down_write(&afs_cells_sem);
159 read_lock(&afs_cells_lock);
160 list_for_each_entry(cell, &afs_cells, link) {
wangleibec5eb62010-08-11 09:38:04 +0100161 if (strncasecmp(cell->name, name, namesz) == 0)
Sven Schnelle5214b722008-03-28 14:15:55 -0700162 goto duplicate_name;
163 }
164 read_unlock(&afs_cells_lock);
165
wangleibec5eb62010-08-11 09:38:04 +0100166 cell = afs_cell_alloc(name, namesz, vllist);
David Howells00d3b7a2007-04-26 15:57:07 -0700167 if (IS_ERR(cell)) {
168 _leave(" = %ld", PTR_ERR(cell));
Sven Schnellea5f37c32008-04-02 13:17:18 +0100169 up_write(&afs_cells_sem);
David Howells00d3b7a2007-04-26 15:57:07 -0700170 return cell;
171 }
172
David Howells08e0e7c2007-04-26 15:55:03 -0700173 /* add a proc directory for this cell */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ret = afs_proc_cell_setup(cell);
175 if (ret < 0)
176 goto error;
177
David Howells9b3f26c2009-04-03 16:42:41 +0100178#ifdef CONFIG_AFS_FSCACHE
179 /* put it up for caching (this never returns an error) */
180 cell->cache = fscache_acquire_cookie(afs_cache_netfs.primary_index,
181 &afs_cell_cache_index_def,
David Howells94d30ae2013-09-21 00:09:31 +0100182 cell, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#endif
184
185 /* add to the cell lists */
186 write_lock(&afs_cells_lock);
187 list_add_tail(&cell->link, &afs_cells);
188 write_unlock(&afs_cells_lock);
189
190 down_write(&afs_proc_cells_sem);
191 list_add_tail(&cell->proc_link, &afs_proc_cells);
192 up_write(&afs_proc_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 up_write(&afs_cells_sem);
194
David Howells08e0e7c2007-04-26 15:55:03 -0700195 _leave(" = %p", cell);
196 return cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
David Howellsec268152007-04-26 15:49:28 -0700198error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 up_write(&afs_cells_sem);
David Howells00d3b7a2007-04-26 15:57:07 -0700200 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 kfree(cell);
202 _leave(" = %d", ret);
David Howells08e0e7c2007-04-26 15:55:03 -0700203 return ERR_PTR(ret);
Sven Schnelle5214b722008-03-28 14:15:55 -0700204
205duplicate_name:
wangleibec5eb62010-08-11 09:38:04 +0100206 if (retref && !IS_ERR(cell))
207 afs_get_cell(cell);
208
Sven Schnelle5214b722008-03-28 14:15:55 -0700209 read_unlock(&afs_cells_lock);
210 up_write(&afs_cells_sem);
wangleibec5eb62010-08-11 09:38:04 +0100211
212 if (retref) {
213 _leave(" = %p", cell);
214 return cell;
215 }
216
217 _leave(" = -EEXIST");
Sven Schnelle5214b722008-03-28 14:15:55 -0700218 return ERR_PTR(-EEXIST);
David Howellsec268152007-04-26 15:49:28 -0700219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
David Howells08e0e7c2007-04-26 15:55:03 -0700222 * set the root cell information
223 * - can be called with a module parameter string
224 * - can be called from a write to /proc/fs/afs/rootcell
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 */
226int afs_cell_init(char *rootcell)
227{
228 struct afs_cell *old_root, *new_root;
229 char *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 _enter("");
232
233 if (!rootcell) {
234 /* module is loaded with no parameters, or built statically.
235 * - in the future we might initialize cell DB here.
236 */
David Howells08e0e7c2007-04-26 15:55:03 -0700237 _leave(" = 0 [no root]");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 0;
239 }
240
241 cp = strchr(rootcell, ':');
Wang Lei07567a52010-08-04 15:16:38 +0100242 if (!cp)
243 _debug("kAFS: no VL server IP addresses specified");
244 else
245 *cp++ = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* allocate a cell record for the root cell */
wangleibec5eb62010-08-11 09:38:04 +0100248 new_root = afs_cell_create(rootcell, strlen(rootcell), cp, false);
David Howells08e0e7c2007-04-26 15:55:03 -0700249 if (IS_ERR(new_root)) {
250 _leave(" = %ld", PTR_ERR(new_root));
251 return PTR_ERR(new_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
David Howells08e0e7c2007-04-26 15:55:03 -0700254 /* install the new cell */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 write_lock(&afs_cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700256 old_root = afs_cell_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 afs_cell_root = new_root;
258 write_unlock(&afs_cells_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700259 afs_put_cell(old_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
David Howells08e0e7c2007-04-26 15:55:03 -0700261 _leave(" = 0");
262 return 0;
David Howellsec268152007-04-26 15:49:28 -0700263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/*
266 * lookup a cell record
267 */
wangleibec5eb62010-08-11 09:38:04 +0100268struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz,
269 bool dns_cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct afs_cell *cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
wangleibec5eb62010-08-11 09:38:04 +0100273 _enter("\"%*.*s\",", namesz, namesz, name ?: "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
David Howells08e0e7c2007-04-26 15:55:03 -0700275 down_read(&afs_cells_sem);
276 read_lock(&afs_cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if (name) {
279 /* if the cell was named, look for it in the cell record list */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 list_for_each_entry(cell, &afs_cells, link) {
281 if (strncmp(cell->name, name, namesz) == 0) {
282 afs_get_cell(cell);
283 goto found;
284 }
285 }
David Howells08e0e7c2007-04-26 15:55:03 -0700286 cell = ERR_PTR(-ENOENT);
wangleibec5eb62010-08-11 09:38:04 +0100287 if (dns_cell)
288 goto create_cell;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 found:
David Howells08e0e7c2007-04-26 15:55:03 -0700290 ;
David Howellsec268152007-04-26 15:49:28 -0700291 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 cell = afs_cell_root;
293 if (!cell) {
294 /* this should not happen unless user tries to mount
295 * when root cell is not set. Return an impossibly
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300296 * bizarre errno to alert the user. Things like
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * ENOENT might be "more appropriate" but they happen
298 * for other reasons.
299 */
David Howells08e0e7c2007-04-26 15:55:03 -0700300 cell = ERR_PTR(-EDESTADDRREQ);
David Howellsec268152007-04-26 15:49:28 -0700301 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 afs_get_cell(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
David Howells08e0e7c2007-04-26 15:55:03 -0700307 read_unlock(&afs_cells_lock);
308 up_read(&afs_cells_sem);
309 _leave(" = %p", cell);
310 return cell;
wangleibec5eb62010-08-11 09:38:04 +0100311
312create_cell:
313 read_unlock(&afs_cells_lock);
314 up_read(&afs_cells_sem);
315
316 cell = afs_cell_create(name, namesz, NULL, true);
317
318 _leave(" = %p", cell);
319 return cell;
David Howellsec268152007-04-26 15:49:28 -0700320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Adrian Bunkc1206a22007-10-16 23:26:41 -0700322#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323/*
324 * try and get a cell record
325 */
David Howells08e0e7c2007-04-26 15:55:03 -0700326struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 write_lock(&afs_cells_lock);
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (cell && !list_empty(&cell->link))
331 afs_get_cell(cell);
332 else
333 cell = NULL;
334
335 write_unlock(&afs_cells_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return cell;
David Howellsec268152007-04-26 15:49:28 -0700337}
Adrian Bunkc1206a22007-10-16 23:26:41 -0700338#endif /* 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*
341 * destroy a cell record
342 */
343void afs_put_cell(struct afs_cell *cell)
344{
345 if (!cell)
346 return;
347
348 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
349
David Howells08e0e7c2007-04-26 15:55:03 -0700350 ASSERTCMP(atomic_read(&cell->usage), >, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* to prevent a race, the decrement and the dequeue must be effectively
353 * atomic */
354 write_lock(&afs_cells_lock);
355
356 if (likely(!atomic_dec_and_test(&cell->usage))) {
357 write_unlock(&afs_cells_lock);
358 _leave("");
359 return;
360 }
361
David Howells08e0e7c2007-04-26 15:55:03 -0700362 ASSERT(list_empty(&cell->servers));
363 ASSERT(list_empty(&cell->vl_list));
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 write_unlock(&afs_cells_lock);
366
David Howells08e0e7c2007-04-26 15:55:03 -0700367 wake_up(&afs_cells_freeable_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 _leave(" [unused]");
David Howellsec268152007-04-26 15:49:28 -0700370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/*
373 * destroy a cell record
David Howells08e0e7c2007-04-26 15:55:03 -0700374 * - must be called with the afs_cells_sem write-locked
375 * - cell->link should have been broken by the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
377static void afs_cell_destroy(struct afs_cell *cell)
378{
379 _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
380
David Howells08e0e7c2007-04-26 15:55:03 -0700381 ASSERTCMP(atomic_read(&cell->usage), >=, 0);
382 ASSERT(list_empty(&cell->link));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
David Howells08e0e7c2007-04-26 15:55:03 -0700384 /* wait for everyone to stop using the cell */
385 if (atomic_read(&cell->usage) > 0) {
386 DECLARE_WAITQUEUE(myself, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
David Howells08e0e7c2007-04-26 15:55:03 -0700388 _debug("wait for cell %s", cell->name);
389 set_current_state(TASK_UNINTERRUPTIBLE);
390 add_wait_queue(&afs_cells_freeable_wq, &myself);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
David Howells08e0e7c2007-04-26 15:55:03 -0700392 while (atomic_read(&cell->usage) > 0) {
393 schedule();
394 set_current_state(TASK_UNINTERRUPTIBLE);
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David Howells08e0e7c2007-04-26 15:55:03 -0700397 remove_wait_queue(&afs_cells_freeable_wq, &myself);
398 set_current_state(TASK_RUNNING);
399 }
400
401 _debug("cell dead");
402 ASSERTCMP(atomic_read(&cell->usage), ==, 0);
403 ASSERT(list_empty(&cell->servers));
404 ASSERT(list_empty(&cell->vl_list));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 afs_proc_cell_remove(cell);
407
408 down_write(&afs_proc_cells_sem);
409 list_del_init(&cell->proc_link);
410 up_write(&afs_proc_cells_sem);
411
David Howells9b3f26c2009-04-03 16:42:41 +0100412#ifdef CONFIG_AFS_FSCACHE
413 fscache_relinquish_cookie(cell->cache, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#endif
David Howells00d3b7a2007-04-26 15:57:07 -0700415 key_put(cell->anonymous_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 kfree(cell);
417
418 _leave(" [destroyed]");
David Howellsec268152007-04-26 15:49:28 -0700419}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * purge in-memory cell database on module unload or afs_init() failure
423 * - the timeout daemon is stopped before calling this
424 */
425void afs_cell_purge(void)
426{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct afs_cell *cell;
428
429 _enter("");
430
431 afs_put_cell(afs_cell_root);
432
David Howells08e0e7c2007-04-26 15:55:03 -0700433 down_write(&afs_cells_sem);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 while (!list_empty(&afs_cells)) {
436 cell = NULL;
437
438 /* remove the next cell from the front of the list */
439 write_lock(&afs_cells_lock);
440
441 if (!list_empty(&afs_cells)) {
442 cell = list_entry(afs_cells.next,
443 struct afs_cell, link);
444 list_del_init(&cell->link);
445 }
446
447 write_unlock(&afs_cells_lock);
448
449 if (cell) {
450 _debug("PURGING CELL %s (%d)",
451 cell->name, atomic_read(&cell->usage));
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 /* now the cell should be left with no references */
454 afs_cell_destroy(cell);
455 }
456 }
457
David Howells08e0e7c2007-04-26 15:55:03 -0700458 up_write(&afs_cells_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700460}