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