David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1 | /* AFS cell and server record management |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/slab.h> |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 14 | #include <linux/key.h> |
| 15 | #include <linux/ctype.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 16 | #include <linux/sched.h> |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 17 | #include <keys/rxrpc-type.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "internal.h" |
| 19 | |
| 20 | DECLARE_RWSEM(afs_proc_cells_sem); |
| 21 | LIST_HEAD(afs_proc_cells); |
| 22 | |
| 23 | static struct list_head afs_cells = LIST_HEAD_INIT(afs_cells); |
| 24 | static DEFINE_RWLOCK(afs_cells_lock); |
| 25 | static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 26 | static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | static struct afs_cell *afs_cell_root; |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 30 | * allocate a cell record and fill in its name, VL server address list and |
| 31 | * allocate an anonymous key |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 33 | static struct afs_cell *afs_cell_alloc(const char *name, char *vllist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
| 35 | struct afs_cell *cell; |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 36 | struct key *key; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 37 | size_t namelen; |
| 38 | char keyname[4 + AFS_MAXCELLNAME + 1], *cp, *dp, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | int ret; |
| 40 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 41 | _enter("%s,%s", name, vllist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */ |
| 44 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 45 | namelen = strlen(name); |
| 46 | if (namelen > AFS_MAXCELLNAME) |
| 47 | return ERR_PTR(-ENAMETOOLONG); |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /* allocate and initialise a cell record */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 50 | cell = kzalloc(sizeof(struct afs_cell) + namelen + 1, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | if (!cell) { |
| 52 | _leave(" = -ENOMEM"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 53 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 56 | memcpy(cell->name, name, namelen); |
| 57 | cell->name[namelen] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 59 | atomic_set(&cell->usage, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | INIT_LIST_HEAD(&cell->link); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 61 | rwlock_init(&cell->servers_lock); |
| 62 | INIT_LIST_HEAD(&cell->servers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | init_rwsem(&cell->vl_sem); |
| 64 | INIT_LIST_HEAD(&cell->vl_list); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 65 | spin_lock_init(&cell->vl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* fill in the VL server list from the rest of the string */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | do { |
| 69 | unsigned a, b, c, d; |
| 70 | |
| 71 | next = strchr(vllist, ':'); |
| 72 | if (next) |
| 73 | *next++ = 0; |
| 74 | |
| 75 | if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 76 | goto bad_address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | if (a > 255 || b > 255 || c > 255 || d > 255) |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 79 | goto bad_address; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | cell->vl_addrs[cell->vl_naddrs++].s_addr = |
| 82 | htonl((a << 24) | (b << 16) | (c << 8) | d); |
| 83 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 84 | } while (cell->vl_naddrs < AFS_CELL_MAX_ADDRS && (vllist = next)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 86 | /* create a key to represent an anonymous user */ |
| 87 | memcpy(keyname, "afs@", 4); |
| 88 | dp = keyname + 4; |
| 89 | cp = cell->name; |
| 90 | do { |
| 91 | *dp++ = toupper(*cp); |
| 92 | } while (*cp++); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 93 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 94 | key = rxrpc_get_null_key(keyname); |
| 95 | if (IS_ERR(key)) { |
| 96 | _debug("no key"); |
| 97 | ret = PTR_ERR(key); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 98 | goto error; |
| 99 | } |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 100 | cell->anonymous_key = key; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 101 | |
| 102 | _debug("anon key %p{%x}", |
| 103 | cell->anonymous_key, key_serial(cell->anonymous_key)); |
| 104 | |
| 105 | _leave(" = %p", cell); |
| 106 | return cell; |
| 107 | |
| 108 | bad_address: |
| 109 | printk(KERN_ERR "kAFS: bad VL server IP address\n"); |
| 110 | ret = -EINVAL; |
| 111 | error: |
| 112 | key_put(cell->anonymous_key); |
| 113 | kfree(cell); |
| 114 | _leave(" = %d", ret); |
| 115 | return ERR_PTR(ret); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * create a cell record |
| 120 | * - "name" is the name of the cell |
| 121 | * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format |
| 122 | */ |
| 123 | struct afs_cell *afs_cell_create(const char *name, char *vllist) |
| 124 | { |
| 125 | struct afs_cell *cell; |
| 126 | int ret; |
| 127 | |
| 128 | _enter("%s,%s", name, vllist); |
| 129 | |
| 130 | cell = afs_cell_alloc(name, vllist); |
| 131 | if (IS_ERR(cell)) { |
| 132 | _leave(" = %ld", PTR_ERR(cell)); |
| 133 | return cell; |
| 134 | } |
| 135 | |
| 136 | down_write(&afs_cells_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 138 | /* add a proc directory for this cell */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | ret = afs_proc_cell_setup(cell); |
| 140 | if (ret < 0) |
| 141 | goto error; |
| 142 | |
| 143 | #ifdef AFS_CACHING_SUPPORT |
| 144 | /* put it up for caching */ |
| 145 | cachefs_acquire_cookie(afs_cache_netfs.primary_index, |
| 146 | &afs_vlocation_cache_index_def, |
| 147 | cell, |
| 148 | &cell->cache); |
| 149 | #endif |
| 150 | |
| 151 | /* add to the cell lists */ |
| 152 | write_lock(&afs_cells_lock); |
| 153 | list_add_tail(&cell->link, &afs_cells); |
| 154 | write_unlock(&afs_cells_lock); |
| 155 | |
| 156 | down_write(&afs_proc_cells_sem); |
| 157 | list_add_tail(&cell->proc_link, &afs_proc_cells); |
| 158 | up_write(&afs_proc_cells_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | up_write(&afs_cells_sem); |
| 160 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 161 | _leave(" = %p", cell); |
| 162 | return cell; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 164 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | up_write(&afs_cells_sem); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 166 | key_put(cell->anonymous_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | kfree(cell); |
| 168 | _leave(" = %d", ret); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 169 | return ERR_PTR(ret); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 173 | * set the root cell information |
| 174 | * - can be called with a module parameter string |
| 175 | * - can be called from a write to /proc/fs/afs/rootcell |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | */ |
| 177 | int afs_cell_init(char *rootcell) |
| 178 | { |
| 179 | struct afs_cell *old_root, *new_root; |
| 180 | char *cp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
| 182 | _enter(""); |
| 183 | |
| 184 | if (!rootcell) { |
| 185 | /* module is loaded with no parameters, or built statically. |
| 186 | * - in the future we might initialize cell DB here. |
| 187 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 188 | _leave(" = 0 [no root]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | cp = strchr(rootcell, ':'); |
| 193 | if (!cp) { |
| 194 | printk(KERN_ERR "kAFS: no VL server IP addresses specified\n"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 195 | _leave(" = -EINVAL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | return -EINVAL; |
| 197 | } |
| 198 | |
| 199 | /* allocate a cell record for the root cell */ |
| 200 | *cp++ = 0; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 201 | new_root = afs_cell_create(rootcell, cp); |
| 202 | if (IS_ERR(new_root)) { |
| 203 | _leave(" = %ld", PTR_ERR(new_root)); |
| 204 | return PTR_ERR(new_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 207 | /* install the new cell */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | write_lock(&afs_cells_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 209 | old_root = afs_cell_root; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | afs_cell_root = new_root; |
| 211 | write_unlock(&afs_cells_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 212 | afs_put_cell(old_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 214 | _leave(" = 0"); |
| 215 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | /* |
| 219 | * lookup a cell record |
| 220 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 221 | struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
| 223 | struct afs_cell *cell; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | _enter("\"%*.*s\",", namesz, namesz, name ? name : ""); |
| 226 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 227 | down_read(&afs_cells_sem); |
| 228 | read_lock(&afs_cells_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | if (name) { |
| 231 | /* if the cell was named, look for it in the cell record list */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | list_for_each_entry(cell, &afs_cells, link) { |
| 233 | if (strncmp(cell->name, name, namesz) == 0) { |
| 234 | afs_get_cell(cell); |
| 235 | goto found; |
| 236 | } |
| 237 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 238 | cell = ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | found: |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 240 | ; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 241 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | cell = afs_cell_root; |
| 243 | if (!cell) { |
| 244 | /* this should not happen unless user tries to mount |
| 245 | * when root cell is not set. Return an impossibly |
| 246 | * bizzare errno to alert the user. Things like |
| 247 | * ENOENT might be "more appropriate" but they happen |
| 248 | * for other reasons. |
| 249 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 250 | cell = ERR_PTR(-EDESTADDRREQ); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 251 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | afs_get_cell(cell); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | } |
| 256 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 257 | read_unlock(&afs_cells_lock); |
| 258 | up_read(&afs_cells_sem); |
| 259 | _leave(" = %p", cell); |
| 260 | return cell; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 263 | #if 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | /* |
| 265 | * try and get a cell record |
| 266 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 267 | struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | write_lock(&afs_cells_lock); |
| 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (cell && !list_empty(&cell->link)) |
| 272 | afs_get_cell(cell); |
| 273 | else |
| 274 | cell = NULL; |
| 275 | |
| 276 | write_unlock(&afs_cells_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | return cell; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 278 | } |
Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 279 | #endif /* 0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | /* |
| 282 | * destroy a cell record |
| 283 | */ |
| 284 | void afs_put_cell(struct afs_cell *cell) |
| 285 | { |
| 286 | if (!cell) |
| 287 | return; |
| 288 | |
| 289 | _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name); |
| 290 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 291 | ASSERTCMP(atomic_read(&cell->usage), >, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | /* to prevent a race, the decrement and the dequeue must be effectively |
| 294 | * atomic */ |
| 295 | write_lock(&afs_cells_lock); |
| 296 | |
| 297 | if (likely(!atomic_dec_and_test(&cell->usage))) { |
| 298 | write_unlock(&afs_cells_lock); |
| 299 | _leave(""); |
| 300 | return; |
| 301 | } |
| 302 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 303 | ASSERT(list_empty(&cell->servers)); |
| 304 | ASSERT(list_empty(&cell->vl_list)); |
| 305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | write_unlock(&afs_cells_lock); |
| 307 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 308 | wake_up(&afs_cells_freeable_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
| 310 | _leave(" [unused]"); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | /* |
| 314 | * destroy a cell record |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 315 | * - must be called with the afs_cells_sem write-locked |
| 316 | * - cell->link should have been broken by the caller |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | */ |
| 318 | static void afs_cell_destroy(struct afs_cell *cell) |
| 319 | { |
| 320 | _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name); |
| 321 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 322 | ASSERTCMP(atomic_read(&cell->usage), >=, 0); |
| 323 | ASSERT(list_empty(&cell->link)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 325 | /* wait for everyone to stop using the cell */ |
| 326 | if (atomic_read(&cell->usage) > 0) { |
| 327 | DECLARE_WAITQUEUE(myself, current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 329 | _debug("wait for cell %s", cell->name); |
| 330 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 331 | add_wait_queue(&afs_cells_freeable_wq, &myself); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 333 | while (atomic_read(&cell->usage) > 0) { |
| 334 | schedule(); |
| 335 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 336 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 338 | remove_wait_queue(&afs_cells_freeable_wq, &myself); |
| 339 | set_current_state(TASK_RUNNING); |
| 340 | } |
| 341 | |
| 342 | _debug("cell dead"); |
| 343 | ASSERTCMP(atomic_read(&cell->usage), ==, 0); |
| 344 | ASSERT(list_empty(&cell->servers)); |
| 345 | ASSERT(list_empty(&cell->vl_list)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | afs_proc_cell_remove(cell); |
| 348 | |
| 349 | down_write(&afs_proc_cells_sem); |
| 350 | list_del_init(&cell->proc_link); |
| 351 | up_write(&afs_proc_cells_sem); |
| 352 | |
| 353 | #ifdef AFS_CACHING_SUPPORT |
| 354 | cachefs_relinquish_cookie(cell->cache, 0); |
| 355 | #endif |
| 356 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 357 | key_put(cell->anonymous_key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | kfree(cell); |
| 359 | |
| 360 | _leave(" [destroyed]"); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | * purge in-memory cell database on module unload or afs_init() failure |
| 365 | * - the timeout daemon is stopped before calling this |
| 366 | */ |
| 367 | void afs_cell_purge(void) |
| 368 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | struct afs_cell *cell; |
| 370 | |
| 371 | _enter(""); |
| 372 | |
| 373 | afs_put_cell(afs_cell_root); |
| 374 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 375 | down_write(&afs_cells_sem); |
| 376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | while (!list_empty(&afs_cells)) { |
| 378 | cell = NULL; |
| 379 | |
| 380 | /* remove the next cell from the front of the list */ |
| 381 | write_lock(&afs_cells_lock); |
| 382 | |
| 383 | if (!list_empty(&afs_cells)) { |
| 384 | cell = list_entry(afs_cells.next, |
| 385 | struct afs_cell, link); |
| 386 | list_del_init(&cell->link); |
| 387 | } |
| 388 | |
| 389 | write_unlock(&afs_cells_lock); |
| 390 | |
| 391 | if (cell) { |
| 392 | _debug("PURGING CELL %s (%d)", |
| 393 | cell->name, atomic_read(&cell->usage)); |
| 394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | /* now the cell should be left with no references */ |
| 396 | afs_cell_destroy(cell); |
| 397 | } |
| 398 | } |
| 399 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 400 | up_write(&afs_cells_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | _leave(""); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 402 | } |