| 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 |  | 
| Robert P. J. Day | 0ae52d6 | 2008-04-29 01:03:20 -0700 | [diff] [blame] | 23 | static LIST_HEAD(afs_cells); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Sven Schnelle | 5214b72 | 2008-03-28 14:15:55 -0700 | [diff] [blame] | 130 | down_write(&afs_cells_sem); | 
|  | 131 | read_lock(&afs_cells_lock); | 
|  | 132 | list_for_each_entry(cell, &afs_cells, link) { | 
|  | 133 | if (strcasecmp(cell->name, name) == 0) | 
|  | 134 | goto duplicate_name; | 
|  | 135 | } | 
|  | 136 | read_unlock(&afs_cells_lock); | 
|  | 137 |  | 
| David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 138 | cell = afs_cell_alloc(name, vllist); | 
|  | 139 | if (IS_ERR(cell)) { | 
|  | 140 | _leave(" = %ld", PTR_ERR(cell)); | 
| Sven Schnelle | a5f37c3 | 2008-04-02 13:17:18 +0100 | [diff] [blame] | 141 | up_write(&afs_cells_sem); | 
| David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 142 | return cell; | 
|  | 143 | } | 
|  | 144 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 145 | /* add a proc directory for this cell */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | ret = afs_proc_cell_setup(cell); | 
|  | 147 | if (ret < 0) | 
|  | 148 | goto error; | 
|  | 149 |  | 
|  | 150 | #ifdef AFS_CACHING_SUPPORT | 
|  | 151 | /* put it up for caching */ | 
|  | 152 | cachefs_acquire_cookie(afs_cache_netfs.primary_index, | 
|  | 153 | &afs_vlocation_cache_index_def, | 
|  | 154 | cell, | 
|  | 155 | &cell->cache); | 
|  | 156 | #endif | 
|  | 157 |  | 
|  | 158 | /* add to the cell lists */ | 
|  | 159 | write_lock(&afs_cells_lock); | 
|  | 160 | list_add_tail(&cell->link, &afs_cells); | 
|  | 161 | write_unlock(&afs_cells_lock); | 
|  | 162 |  | 
|  | 163 | down_write(&afs_proc_cells_sem); | 
|  | 164 | list_add_tail(&cell->proc_link, &afs_proc_cells); | 
|  | 165 | up_write(&afs_proc_cells_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | up_write(&afs_cells_sem); | 
|  | 167 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 168 | _leave(" = %p", cell); | 
|  | 169 | return cell; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 |  | 
| David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 171 | error: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | up_write(&afs_cells_sem); | 
| David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 173 | key_put(cell->anonymous_key); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | kfree(cell); | 
|  | 175 | _leave(" = %d", ret); | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 176 | return ERR_PTR(ret); | 
| Sven Schnelle | 5214b72 | 2008-03-28 14:15:55 -0700 | [diff] [blame] | 177 |  | 
|  | 178 | duplicate_name: | 
|  | 179 | read_unlock(&afs_cells_lock); | 
|  | 180 | up_write(&afs_cells_sem); | 
|  | 181 | return ERR_PTR(-EEXIST); | 
| David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 182 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | /* | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 185 | * set the root cell information | 
|  | 186 | * - can be called with a module parameter string | 
|  | 187 | * - can be called from a write to /proc/fs/afs/rootcell | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | */ | 
|  | 189 | int afs_cell_init(char *rootcell) | 
|  | 190 | { | 
|  | 191 | struct afs_cell *old_root, *new_root; | 
|  | 192 | char *cp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 |  | 
|  | 194 | _enter(""); | 
|  | 195 |  | 
|  | 196 | if (!rootcell) { | 
|  | 197 | /* module is loaded with no parameters, or built statically. | 
|  | 198 | * - in the future we might initialize cell DB here. | 
|  | 199 | */ | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 200 | _leave(" = 0 [no root]"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | return 0; | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | cp = strchr(rootcell, ':'); | 
|  | 205 | if (!cp) { | 
|  | 206 | printk(KERN_ERR "kAFS: no VL server IP addresses specified\n"); | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 207 | _leave(" = -EINVAL"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | return -EINVAL; | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | /* allocate a cell record for the root cell */ | 
|  | 212 | *cp++ = 0; | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 213 | new_root = afs_cell_create(rootcell, cp); | 
|  | 214 | if (IS_ERR(new_root)) { | 
|  | 215 | _leave(" = %ld", PTR_ERR(new_root)); | 
|  | 216 | return PTR_ERR(new_root); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } | 
|  | 218 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 219 | /* install the new cell */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | write_lock(&afs_cells_lock); | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 221 | old_root = afs_cell_root; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | afs_cell_root = new_root; | 
|  | 223 | write_unlock(&afs_cells_lock); | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 224 | afs_put_cell(old_root); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 226 | _leave(" = 0"); | 
|  | 227 | return 0; | 
| David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 228 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | /* | 
|  | 231 | * lookup a cell record | 
|  | 232 | */ | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 233 | struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { | 
|  | 235 | struct afs_cell *cell; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 |  | 
|  | 237 | _enter("\"%*.*s\",", namesz, namesz, name ? name : ""); | 
|  | 238 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 239 | down_read(&afs_cells_sem); | 
|  | 240 | read_lock(&afs_cells_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 |  | 
|  | 242 | if (name) { | 
|  | 243 | /* 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] | 244 | list_for_each_entry(cell, &afs_cells, link) { | 
|  | 245 | if (strncmp(cell->name, name, namesz) == 0) { | 
|  | 246 | afs_get_cell(cell); | 
|  | 247 | goto found; | 
|  | 248 | } | 
|  | 249 | } | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 250 | cell = ERR_PTR(-ENOENT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | found: | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 252 | ; | 
| David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 253 | } else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | cell = afs_cell_root; | 
|  | 255 | if (!cell) { | 
|  | 256 | /* this should not happen unless user tries to mount | 
|  | 257 | * when root cell is not set. Return an impossibly | 
|  | 258 | * bizzare errno to alert the user. Things like | 
|  | 259 | * ENOENT might be "more appropriate" but they happen | 
|  | 260 | * for other reasons. | 
|  | 261 | */ | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 262 | cell = ERR_PTR(-EDESTADDRREQ); | 
| David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 263 | } else { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | afs_get_cell(cell); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } | 
|  | 266 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } | 
|  | 268 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 269 | read_unlock(&afs_cells_lock); | 
|  | 270 | up_read(&afs_cells_sem); | 
|  | 271 | _leave(" = %p", cell); | 
|  | 272 | return cell; | 
| David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 273 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 |  | 
| Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 275 | #if 0 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | /* | 
|  | 277 | * try and get a cell record | 
|  | 278 | */ | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 279 | struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell) | 
| 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 | write_lock(&afs_cells_lock); | 
|  | 282 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | if (cell && !list_empty(&cell->link)) | 
|  | 284 | afs_get_cell(cell); | 
|  | 285 | else | 
|  | 286 | cell = NULL; | 
|  | 287 |  | 
|  | 288 | write_unlock(&afs_cells_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | return cell; | 
| David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 290 | } | 
| Adrian Bunk | c1206a2 | 2007-10-16 23:26:41 -0700 | [diff] [blame] | 291 | #endif  /*  0  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | /* | 
|  | 294 | * destroy a cell record | 
|  | 295 | */ | 
|  | 296 | void afs_put_cell(struct afs_cell *cell) | 
|  | 297 | { | 
|  | 298 | if (!cell) | 
|  | 299 | return; | 
|  | 300 |  | 
|  | 301 | _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name); | 
|  | 302 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 303 | ASSERTCMP(atomic_read(&cell->usage), >, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 |  | 
|  | 305 | /* to prevent a race, the decrement and the dequeue must be effectively | 
|  | 306 | * atomic */ | 
|  | 307 | write_lock(&afs_cells_lock); | 
|  | 308 |  | 
|  | 309 | if (likely(!atomic_dec_and_test(&cell->usage))) { | 
|  | 310 | write_unlock(&afs_cells_lock); | 
|  | 311 | _leave(""); | 
|  | 312 | return; | 
|  | 313 | } | 
|  | 314 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 315 | ASSERT(list_empty(&cell->servers)); | 
|  | 316 | ASSERT(list_empty(&cell->vl_list)); | 
|  | 317 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | write_unlock(&afs_cells_lock); | 
|  | 319 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 320 | wake_up(&afs_cells_freeable_wq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 |  | 
|  | 322 | _leave(" [unused]"); | 
| David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 323 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /* | 
|  | 326 | * destroy a cell record | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 327 | * - must be called with the afs_cells_sem write-locked | 
|  | 328 | * - cell->link should have been broken by the caller | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | */ | 
|  | 330 | static void afs_cell_destroy(struct afs_cell *cell) | 
|  | 331 | { | 
|  | 332 | _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name); | 
|  | 333 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 334 | ASSERTCMP(atomic_read(&cell->usage), >=, 0); | 
|  | 335 | ASSERT(list_empty(&cell->link)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 337 | /* wait for everyone to stop using the cell */ | 
|  | 338 | if (atomic_read(&cell->usage) > 0) { | 
|  | 339 | DECLARE_WAITQUEUE(myself, current); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 341 | _debug("wait for cell %s", cell->name); | 
|  | 342 | set_current_state(TASK_UNINTERRUPTIBLE); | 
|  | 343 | add_wait_queue(&afs_cells_freeable_wq, &myself); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 345 | while (atomic_read(&cell->usage) > 0) { | 
|  | 346 | schedule(); | 
|  | 347 | set_current_state(TASK_UNINTERRUPTIBLE); | 
|  | 348 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 350 | remove_wait_queue(&afs_cells_freeable_wq, &myself); | 
|  | 351 | set_current_state(TASK_RUNNING); | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | _debug("cell dead"); | 
|  | 355 | ASSERTCMP(atomic_read(&cell->usage), ==, 0); | 
|  | 356 | ASSERT(list_empty(&cell->servers)); | 
|  | 357 | ASSERT(list_empty(&cell->vl_list)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 |  | 
|  | 359 | afs_proc_cell_remove(cell); | 
|  | 360 |  | 
|  | 361 | down_write(&afs_proc_cells_sem); | 
|  | 362 | list_del_init(&cell->proc_link); | 
|  | 363 | up_write(&afs_proc_cells_sem); | 
|  | 364 |  | 
|  | 365 | #ifdef AFS_CACHING_SUPPORT | 
|  | 366 | cachefs_relinquish_cookie(cell->cache, 0); | 
|  | 367 | #endif | 
|  | 368 |  | 
| David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 369 | key_put(cell->anonymous_key); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | kfree(cell); | 
|  | 371 |  | 
|  | 372 | _leave(" [destroyed]"); | 
| David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 373 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | * purge in-memory cell database on module unload or afs_init() failure | 
|  | 377 | * - the timeout daemon is stopped before calling this | 
|  | 378 | */ | 
|  | 379 | void afs_cell_purge(void) | 
|  | 380 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | struct afs_cell *cell; | 
|  | 382 |  | 
|  | 383 | _enter(""); | 
|  | 384 |  | 
|  | 385 | afs_put_cell(afs_cell_root); | 
|  | 386 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 387 | down_write(&afs_cells_sem); | 
|  | 388 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | while (!list_empty(&afs_cells)) { | 
|  | 390 | cell = NULL; | 
|  | 391 |  | 
|  | 392 | /* remove the next cell from the front of the list */ | 
|  | 393 | write_lock(&afs_cells_lock); | 
|  | 394 |  | 
|  | 395 | if (!list_empty(&afs_cells)) { | 
|  | 396 | cell = list_entry(afs_cells.next, | 
|  | 397 | struct afs_cell, link); | 
|  | 398 | list_del_init(&cell->link); | 
|  | 399 | } | 
|  | 400 |  | 
|  | 401 | write_unlock(&afs_cells_lock); | 
|  | 402 |  | 
|  | 403 | if (cell) { | 
|  | 404 | _debug("PURGING CELL %s (%d)", | 
|  | 405 | cell->name, atomic_read(&cell->usage)); | 
|  | 406 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | /* now the cell should be left with no references */ | 
|  | 408 | afs_cell_destroy(cell); | 
|  | 409 | } | 
|  | 410 | } | 
|  | 411 |  | 
| David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 412 | up_write(&afs_cells_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | _leave(""); | 
| David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 414 | } |