]>
Commit | Line | Data |
---|---|---|
ec26815a | 1 | /* AFS cell and server record management |
1da177e4 LT |
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> | |
1da177e4 | 13 | #include <linux/slab.h> |
1da177e4 LT |
14 | #include "internal.h" |
15 | ||
16 | DECLARE_RWSEM(afs_proc_cells_sem); | |
17 | LIST_HEAD(afs_proc_cells); | |
18 | ||
19 | static struct list_head afs_cells = LIST_HEAD_INIT(afs_cells); | |
20 | static DEFINE_RWLOCK(afs_cells_lock); | |
21 | static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */ | |
08e0e7c8 | 22 | static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq); |
1da177e4 LT |
23 | static struct afs_cell *afs_cell_root; |
24 | ||
1da177e4 LT |
25 | /* |
26 | * create a cell record | |
27 | * - "name" is the name of the cell | |
28 | * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format | |
29 | */ | |
08e0e7c8 | 30 | struct afs_cell *afs_cell_create(const char *name, char *vllist) |
1da177e4 LT |
31 | { |
32 | struct afs_cell *cell; | |
33 | char *next; | |
34 | int ret; | |
35 | ||
08e0e7c8 | 36 | _enter("%s,%s", name, vllist); |
1da177e4 LT |
37 | |
38 | BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */ | |
39 | ||
40 | /* allocate and initialise a cell record */ | |
41 | cell = kmalloc(sizeof(struct afs_cell) + strlen(name) + 1, GFP_KERNEL); | |
42 | if (!cell) { | |
43 | _leave(" = -ENOMEM"); | |
08e0e7c8 | 44 | return ERR_PTR(-ENOMEM); |
1da177e4 LT |
45 | } |
46 | ||
47 | down_write(&afs_cells_sem); | |
48 | ||
49 | memset(cell, 0, sizeof(struct afs_cell)); | |
08e0e7c8 | 50 | atomic_set(&cell->usage, 1); |
1da177e4 LT |
51 | |
52 | INIT_LIST_HEAD(&cell->link); | |
53 | ||
08e0e7c8 DH |
54 | rwlock_init(&cell->servers_lock); |
55 | INIT_LIST_HEAD(&cell->servers); | |
1da177e4 LT |
56 | |
57 | init_rwsem(&cell->vl_sem); | |
58 | INIT_LIST_HEAD(&cell->vl_list); | |
08e0e7c8 | 59 | spin_lock_init(&cell->vl_lock); |
1da177e4 | 60 | |
08e0e7c8 | 61 | strcpy(cell->name, name); |
1da177e4 LT |
62 | |
63 | /* fill in the VL server list from the rest of the string */ | |
64 | ret = -EINVAL; | |
65 | do { | |
66 | unsigned a, b, c, d; | |
67 | ||
68 | next = strchr(vllist, ':'); | |
69 | if (next) | |
70 | *next++ = 0; | |
71 | ||
72 | if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) | |
73 | goto badaddr; | |
74 | ||
75 | if (a > 255 || b > 255 || c > 255 || d > 255) | |
76 | goto badaddr; | |
77 | ||
78 | cell->vl_addrs[cell->vl_naddrs++].s_addr = | |
79 | htonl((a << 24) | (b << 16) | (c << 8) | d); | |
80 | ||
81 | if (cell->vl_naddrs >= AFS_CELL_MAX_ADDRS) | |
82 | break; | |
83 | ||
08e0e7c8 | 84 | } while ((vllist = next)); |
1da177e4 | 85 | |
08e0e7c8 | 86 | /* add a proc directory for this cell */ |
1da177e4 LT |
87 | ret = afs_proc_cell_setup(cell); |
88 | if (ret < 0) | |
89 | goto error; | |
90 | ||
91 | #ifdef AFS_CACHING_SUPPORT | |
92 | /* put it up for caching */ | |
93 | cachefs_acquire_cookie(afs_cache_netfs.primary_index, | |
94 | &afs_vlocation_cache_index_def, | |
95 | cell, | |
96 | &cell->cache); | |
97 | #endif | |
98 | ||
99 | /* add to the cell lists */ | |
100 | write_lock(&afs_cells_lock); | |
101 | list_add_tail(&cell->link, &afs_cells); | |
102 | write_unlock(&afs_cells_lock); | |
103 | ||
104 | down_write(&afs_proc_cells_sem); | |
105 | list_add_tail(&cell->proc_link, &afs_proc_cells); | |
106 | up_write(&afs_proc_cells_sem); | |
1da177e4 LT |
107 | up_write(&afs_cells_sem); |
108 | ||
08e0e7c8 DH |
109 | _leave(" = %p", cell); |
110 | return cell; | |
1da177e4 | 111 | |
ec26815a | 112 | badaddr: |
08e0e7c8 | 113 | printk(KERN_ERR "kAFS: bad VL server IP address\n"); |
ec26815a | 114 | error: |
1da177e4 LT |
115 | up_write(&afs_cells_sem); |
116 | kfree(cell); | |
117 | _leave(" = %d", ret); | |
08e0e7c8 | 118 | return ERR_PTR(ret); |
ec26815a | 119 | } |
1da177e4 | 120 | |
1da177e4 | 121 | /* |
08e0e7c8 DH |
122 | * set the root cell information |
123 | * - can be called with a module parameter string | |
124 | * - can be called from a write to /proc/fs/afs/rootcell | |
1da177e4 LT |
125 | */ |
126 | int afs_cell_init(char *rootcell) | |
127 | { | |
128 | struct afs_cell *old_root, *new_root; | |
129 | char *cp; | |
1da177e4 LT |
130 | |
131 | _enter(""); | |
132 | ||
133 | if (!rootcell) { | |
134 | /* module is loaded with no parameters, or built statically. | |
135 | * - in the future we might initialize cell DB here. | |
136 | */ | |
08e0e7c8 | 137 | _leave(" = 0 [no root]"); |
1da177e4 LT |
138 | return 0; |
139 | } | |
140 | ||
141 | cp = strchr(rootcell, ':'); | |
142 | if (!cp) { | |
143 | printk(KERN_ERR "kAFS: no VL server IP addresses specified\n"); | |
08e0e7c8 | 144 | _leave(" = -EINVAL"); |
1da177e4 LT |
145 | return -EINVAL; |
146 | } | |
147 | ||
148 | /* allocate a cell record for the root cell */ | |
149 | *cp++ = 0; | |
08e0e7c8 DH |
150 | new_root = afs_cell_create(rootcell, cp); |
151 | if (IS_ERR(new_root)) { | |
152 | _leave(" = %ld", PTR_ERR(new_root)); | |
153 | return PTR_ERR(new_root); | |
1da177e4 LT |
154 | } |
155 | ||
08e0e7c8 | 156 | /* install the new cell */ |
1da177e4 | 157 | write_lock(&afs_cells_lock); |
08e0e7c8 | 158 | old_root = afs_cell_root; |
1da177e4 LT |
159 | afs_cell_root = new_root; |
160 | write_unlock(&afs_cells_lock); | |
08e0e7c8 | 161 | afs_put_cell(old_root); |
1da177e4 | 162 | |
08e0e7c8 DH |
163 | _leave(" = 0"); |
164 | return 0; | |
ec26815a | 165 | } |
1da177e4 | 166 | |
1da177e4 LT |
167 | /* |
168 | * lookup a cell record | |
169 | */ | |
08e0e7c8 | 170 | struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz) |
1da177e4 LT |
171 | { |
172 | struct afs_cell *cell; | |
1da177e4 LT |
173 | |
174 | _enter("\"%*.*s\",", namesz, namesz, name ? name : ""); | |
175 | ||
08e0e7c8 DH |
176 | down_read(&afs_cells_sem); |
177 | read_lock(&afs_cells_lock); | |
1da177e4 LT |
178 | |
179 | if (name) { | |
180 | /* if the cell was named, look for it in the cell record list */ | |
1da177e4 LT |
181 | list_for_each_entry(cell, &afs_cells, link) { |
182 | if (strncmp(cell->name, name, namesz) == 0) { | |
183 | afs_get_cell(cell); | |
184 | goto found; | |
185 | } | |
186 | } | |
08e0e7c8 | 187 | cell = ERR_PTR(-ENOENT); |
1da177e4 | 188 | found: |
08e0e7c8 | 189 | ; |
ec26815a | 190 | } else { |
1da177e4 LT |
191 | cell = afs_cell_root; |
192 | if (!cell) { | |
193 | /* this should not happen unless user tries to mount | |
194 | * when root cell is not set. Return an impossibly | |
195 | * bizzare errno to alert the user. Things like | |
196 | * ENOENT might be "more appropriate" but they happen | |
197 | * for other reasons. | |
198 | */ | |
08e0e7c8 | 199 | cell = ERR_PTR(-EDESTADDRREQ); |
ec26815a | 200 | } else { |
1da177e4 | 201 | afs_get_cell(cell); |
1da177e4 LT |
202 | } |
203 | ||
1da177e4 LT |
204 | } |
205 | ||
08e0e7c8 DH |
206 | read_unlock(&afs_cells_lock); |
207 | up_read(&afs_cells_sem); | |
208 | _leave(" = %p", cell); | |
209 | return cell; | |
ec26815a | 210 | } |
1da177e4 | 211 | |
1da177e4 LT |
212 | /* |
213 | * try and get a cell record | |
214 | */ | |
08e0e7c8 | 215 | struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell) |
1da177e4 | 216 | { |
1da177e4 LT |
217 | write_lock(&afs_cells_lock); |
218 | ||
1da177e4 LT |
219 | if (cell && !list_empty(&cell->link)) |
220 | afs_get_cell(cell); | |
221 | else | |
222 | cell = NULL; | |
223 | ||
224 | write_unlock(&afs_cells_lock); | |
1da177e4 | 225 | return cell; |
ec26815a | 226 | } |
1da177e4 | 227 | |
1da177e4 LT |
228 | /* |
229 | * destroy a cell record | |
230 | */ | |
231 | void afs_put_cell(struct afs_cell *cell) | |
232 | { | |
233 | if (!cell) | |
234 | return; | |
235 | ||
236 | _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name); | |
237 | ||
08e0e7c8 | 238 | ASSERTCMP(atomic_read(&cell->usage), >, 0); |
1da177e4 LT |
239 | |
240 | /* to prevent a race, the decrement and the dequeue must be effectively | |
241 | * atomic */ | |
242 | write_lock(&afs_cells_lock); | |
243 | ||
244 | if (likely(!atomic_dec_and_test(&cell->usage))) { | |
245 | write_unlock(&afs_cells_lock); | |
246 | _leave(""); | |
247 | return; | |
248 | } | |
249 | ||
08e0e7c8 DH |
250 | ASSERT(list_empty(&cell->servers)); |
251 | ASSERT(list_empty(&cell->vl_list)); | |
252 | ||
1da177e4 LT |
253 | write_unlock(&afs_cells_lock); |
254 | ||
08e0e7c8 | 255 | wake_up(&afs_cells_freeable_wq); |
1da177e4 LT |
256 | |
257 | _leave(" [unused]"); | |
ec26815a | 258 | } |
1da177e4 | 259 | |
1da177e4 LT |
260 | /* |
261 | * destroy a cell record | |
08e0e7c8 DH |
262 | * - must be called with the afs_cells_sem write-locked |
263 | * - cell->link should have been broken by the caller | |
1da177e4 LT |
264 | */ |
265 | static void afs_cell_destroy(struct afs_cell *cell) | |
266 | { | |
267 | _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name); | |
268 | ||
08e0e7c8 DH |
269 | ASSERTCMP(atomic_read(&cell->usage), >=, 0); |
270 | ASSERT(list_empty(&cell->link)); | |
1da177e4 | 271 | |
08e0e7c8 DH |
272 | /* wait for everyone to stop using the cell */ |
273 | if (atomic_read(&cell->usage) > 0) { | |
274 | DECLARE_WAITQUEUE(myself, current); | |
1da177e4 | 275 | |
08e0e7c8 DH |
276 | _debug("wait for cell %s", cell->name); |
277 | set_current_state(TASK_UNINTERRUPTIBLE); | |
278 | add_wait_queue(&afs_cells_freeable_wq, &myself); | |
1da177e4 | 279 | |
08e0e7c8 DH |
280 | while (atomic_read(&cell->usage) > 0) { |
281 | schedule(); | |
282 | set_current_state(TASK_UNINTERRUPTIBLE); | |
283 | } | |
1da177e4 | 284 | |
08e0e7c8 DH |
285 | remove_wait_queue(&afs_cells_freeable_wq, &myself); |
286 | set_current_state(TASK_RUNNING); | |
287 | } | |
288 | ||
289 | _debug("cell dead"); | |
290 | ASSERTCMP(atomic_read(&cell->usage), ==, 0); | |
291 | ASSERT(list_empty(&cell->servers)); | |
292 | ASSERT(list_empty(&cell->vl_list)); | |
1da177e4 LT |
293 | |
294 | afs_proc_cell_remove(cell); | |
295 | ||
296 | down_write(&afs_proc_cells_sem); | |
297 | list_del_init(&cell->proc_link); | |
298 | up_write(&afs_proc_cells_sem); | |
299 | ||
300 | #ifdef AFS_CACHING_SUPPORT | |
301 | cachefs_relinquish_cookie(cell->cache, 0); | |
302 | #endif | |
303 | ||
1da177e4 LT |
304 | kfree(cell); |
305 | ||
306 | _leave(" [destroyed]"); | |
ec26815a | 307 | } |
1da177e4 | 308 | |
1da177e4 LT |
309 | /* |
310 | * purge in-memory cell database on module unload or afs_init() failure | |
311 | * - the timeout daemon is stopped before calling this | |
312 | */ | |
313 | void afs_cell_purge(void) | |
314 | { | |
1da177e4 LT |
315 | struct afs_cell *cell; |
316 | ||
317 | _enter(""); | |
318 | ||
319 | afs_put_cell(afs_cell_root); | |
320 | ||
08e0e7c8 DH |
321 | down_write(&afs_cells_sem); |
322 | ||
1da177e4 LT |
323 | while (!list_empty(&afs_cells)) { |
324 | cell = NULL; | |
325 | ||
326 | /* remove the next cell from the front of the list */ | |
327 | write_lock(&afs_cells_lock); | |
328 | ||
329 | if (!list_empty(&afs_cells)) { | |
330 | cell = list_entry(afs_cells.next, | |
331 | struct afs_cell, link); | |
332 | list_del_init(&cell->link); | |
333 | } | |
334 | ||
335 | write_unlock(&afs_cells_lock); | |
336 | ||
337 | if (cell) { | |
338 | _debug("PURGING CELL %s (%d)", | |
339 | cell->name, atomic_read(&cell->usage)); | |
340 | ||
1da177e4 LT |
341 | /* now the cell should be left with no references */ |
342 | afs_cell_destroy(cell); | |
343 | } | |
344 | } | |
345 | ||
08e0e7c8 | 346 | up_write(&afs_cells_sem); |
1da177e4 | 347 | _leave(""); |
ec26815a | 348 | } |