]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/afs/cell.c
[AF_RXRPC]: Make the in-kernel AFS filesystem use AF_RXRPC.
[linux-2.6-omap-h63xx.git] / fs / afs / cell.c
1 /* AFS cell and server record management
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>
13 #include <linux/slab.h>
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 */
22 static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
23 static struct afs_cell *afs_cell_root;
24
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  */
30 struct afs_cell *afs_cell_create(const char *name, char *vllist)
31 {
32         struct afs_cell *cell;
33         char *next;
34         int ret;
35
36         _enter("%s,%s", name, vllist);
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");
44                 return ERR_PTR(-ENOMEM);
45         }
46
47         down_write(&afs_cells_sem);
48
49         memset(cell, 0, sizeof(struct afs_cell));
50         atomic_set(&cell->usage, 1);
51
52         INIT_LIST_HEAD(&cell->link);
53
54         rwlock_init(&cell->servers_lock);
55         INIT_LIST_HEAD(&cell->servers);
56
57         init_rwsem(&cell->vl_sem);
58         INIT_LIST_HEAD(&cell->vl_list);
59         spin_lock_init(&cell->vl_lock);
60
61         strcpy(cell->name, name);
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
84         } while ((vllist = next));
85
86         /* add a proc directory for this cell */
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);
107         up_write(&afs_cells_sem);
108
109         _leave(" = %p", cell);
110         return cell;
111
112 badaddr:
113         printk(KERN_ERR "kAFS: bad VL server IP address\n");
114 error:
115         up_write(&afs_cells_sem);
116         kfree(cell);
117         _leave(" = %d", ret);
118         return ERR_PTR(ret);
119 }
120
121 /*
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
125  */
126 int afs_cell_init(char *rootcell)
127 {
128         struct afs_cell *old_root, *new_root;
129         char *cp;
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                  */
137                 _leave(" = 0 [no root]");
138                 return 0;
139         }
140
141         cp = strchr(rootcell, ':');
142         if (!cp) {
143                 printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
144                 _leave(" = -EINVAL");
145                 return -EINVAL;
146         }
147
148         /* allocate a cell record for the root cell */
149         *cp++ = 0;
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);
154         }
155
156         /* install the new cell */
157         write_lock(&afs_cells_lock);
158         old_root = afs_cell_root;
159         afs_cell_root = new_root;
160         write_unlock(&afs_cells_lock);
161         afs_put_cell(old_root);
162
163         _leave(" = 0");
164         return 0;
165 }
166
167 /*
168  * lookup a cell record
169  */
170 struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
171 {
172         struct afs_cell *cell;
173
174         _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
175
176         down_read(&afs_cells_sem);
177         read_lock(&afs_cells_lock);
178
179         if (name) {
180                 /* if the cell was named, look for it in the cell record list */
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                 }
187                 cell = ERR_PTR(-ENOENT);
188         found:
189                 ;
190         } else {
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                          */
199                         cell = ERR_PTR(-EDESTADDRREQ);
200                 } else {
201                         afs_get_cell(cell);
202                 }
203
204         }
205
206         read_unlock(&afs_cells_lock);
207         up_read(&afs_cells_sem);
208         _leave(" = %p", cell);
209         return cell;
210 }
211
212 /*
213  * try and get a cell record
214  */
215 struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
216 {
217         write_lock(&afs_cells_lock);
218
219         if (cell && !list_empty(&cell->link))
220                 afs_get_cell(cell);
221         else
222                 cell = NULL;
223
224         write_unlock(&afs_cells_lock);
225         return cell;
226 }
227
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
238         ASSERTCMP(atomic_read(&cell->usage), >, 0);
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
250         ASSERT(list_empty(&cell->servers));
251         ASSERT(list_empty(&cell->vl_list));
252
253         write_unlock(&afs_cells_lock);
254
255         wake_up(&afs_cells_freeable_wq);
256
257         _leave(" [unused]");
258 }
259
260 /*
261  * destroy a cell record
262  * - must be called with the afs_cells_sem write-locked
263  * - cell->link should have been broken by the caller
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
269         ASSERTCMP(atomic_read(&cell->usage), >=, 0);
270         ASSERT(list_empty(&cell->link));
271
272         /* wait for everyone to stop using the cell */
273         if (atomic_read(&cell->usage) > 0) {
274                 DECLARE_WAITQUEUE(myself, current);
275
276                 _debug("wait for cell %s", cell->name);
277                 set_current_state(TASK_UNINTERRUPTIBLE);
278                 add_wait_queue(&afs_cells_freeable_wq, &myself);
279
280                 while (atomic_read(&cell->usage) > 0) {
281                         schedule();
282                         set_current_state(TASK_UNINTERRUPTIBLE);
283                 }
284
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));
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
304         kfree(cell);
305
306         _leave(" [destroyed]");
307 }
308
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 {
315         struct afs_cell *cell;
316
317         _enter("");
318
319         afs_put_cell(afs_cell_root);
320
321         down_write(&afs_cells_sem);
322
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
341                         /* now the cell should be left with no references */
342                         afs_cell_destroy(cell);
343                 }
344         }
345
346         up_write(&afs_cells_sem);
347         _leave("");
348 }