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