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