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