ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / afs / main.c
1 /* main.c: AFS client file system
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/init.h>
14 #include <linux/sched.h>
15 #include <linux/completion.h>
16 #include <rxrpc/rxrpc.h>
17 #include <rxrpc/transport.h>
18 #include <rxrpc/call.h>
19 #include <rxrpc/peer.h>
20 #include "cache.h"
21 #include "cell.h"
22 #include "server.h"
23 #include "fsclient.h"
24 #include "cmservice.h"
25 #include "kafstimod.h"
26 #include "kafsasyncd.h"
27 #include "internal.h"
28
29 struct rxrpc_transport *afs_transport;
30
31 static int afs_init(void);
32 static void afs_exit(void);
33 static int afs_adding_peer(struct rxrpc_peer *peer);
34 static void afs_discarding_peer(struct rxrpc_peer *peer);
35
36 /* XXX late_initcall is kludgy, but the only alternative seems to create
37  * a transport upon the first mount, which is worse. Or is it?
38  */
39 /* module_init(afs_init); */
40 late_initcall(afs_init);        /* must be called after net/ to create socket */
41
42 module_exit(afs_exit);
43
44 MODULE_DESCRIPTION("AFS Client File System");
45 MODULE_AUTHOR("Red Hat, Inc.");
46 MODULE_LICENSE("GPL");
47
48 static char *rootcell;
49
50 MODULE_PARM(rootcell, "s");
51 MODULE_PARM_DESC(rootcell, "root AFS cell name and VL server IP addr list");
52
53
54 static struct rxrpc_peer_ops afs_peer_ops = {
55         .adding         = afs_adding_peer,
56         .discarding     = afs_discarding_peer,
57 };
58
59 struct list_head afs_cb_hash_tbl[AFS_CB_HASH_COUNT];
60 spinlock_t afs_cb_hash_lock = SPIN_LOCK_UNLOCKED;
61
62 #ifdef AFS_CACHING_SUPPORT
63 static struct cachefs_netfs_operations afs_cache_ops = {
64         .get_page_cookie        = afs_cache_get_page_cookie,
65 };
66
67 struct cachefs_netfs afs_cache_netfs = {
68         .name                   = "afs",
69         .version                = 0,
70         .ops                    = &afs_cache_ops,
71 };
72 #endif
73
74 /*****************************************************************************/
75 /*
76  * initialise the AFS client FS module
77  */
78 static int afs_init(void)
79 {
80         int loop, ret;
81
82         printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 registering.\n");
83
84         /* initialise the callback hash table */
85         spin_lock_init(&afs_cb_hash_lock);
86         for (loop = AFS_CB_HASH_COUNT - 1; loop >= 0; loop--)
87                 INIT_LIST_HEAD(&afs_cb_hash_tbl[loop]);
88
89         /* register the /proc stuff */
90         ret = afs_proc_init();
91         if (ret < 0)
92                 return ret;
93
94 #ifdef AFS_CACHING_SUPPORT
95         /* we want to be able to cache */
96         ret = cachefs_register_netfs(&afs_cache_netfs,
97                                      &afs_cache_cell_index_def);
98         if (ret < 0)
99                 goto error;
100 #endif
101
102 #ifdef CONFIG_KEYS
103         ret = afs_key_register();
104         if (ret < 0)
105                 goto error_cache;
106 #endif
107
108         /* initialise the cell DB */
109         ret = afs_cell_init(rootcell);
110         if (ret < 0)
111                 goto error_keys;
112
113         /* start the timeout daemon */
114         ret = afs_kafstimod_start();
115         if (ret < 0)
116                 goto error_keys;
117
118         /* start the async operation daemon */
119         ret = afs_kafsasyncd_start();
120         if (ret < 0)
121                 goto error_kafstimod;
122
123         /* create the RxRPC transport */
124         ret = rxrpc_create_transport(7001, &afs_transport);
125         if (ret < 0)
126                 goto error_kafsasyncd;
127
128         afs_transport->peer_ops = &afs_peer_ops;
129
130         /* register the filesystems */
131         ret = afs_fs_init();
132         if (ret < 0)
133                 goto error_transport;
134
135         return ret;
136
137  error_transport:
138         rxrpc_put_transport(afs_transport);
139  error_kafsasyncd:
140         afs_kafsasyncd_stop();
141  error_kafstimod:
142         afs_kafstimod_stop();
143  error_keys:
144 #ifdef CONFIG_KEYS
145         afs_key_unregister();
146  error_cache:
147 #endif
148 #ifdef AFS_CACHING_SUPPORT
149         cachefs_unregister_netfs(&afs_cache_netfs);
150  error:
151 #endif
152         afs_cell_purge();
153         afs_proc_cleanup();
154         printk(KERN_ERR "kAFS: failed to register: %d\n", ret);
155         return ret;
156 } /* end afs_init() */
157
158 /*****************************************************************************/
159 /*
160  * clean up on module removal
161  */
162 static void __exit afs_exit(void)
163 {
164         printk(KERN_INFO "kAFS: Red Hat AFS client v0.1 unregistering.\n");
165
166         afs_fs_exit();
167         rxrpc_put_transport(afs_transport);
168         afs_kafstimod_stop();
169         afs_kafsasyncd_stop();
170         afs_cell_purge();
171 #ifdef CONFIG_KEYS
172         afs_key_unregister();
173 #endif
174 #ifdef AFS_CACHING_SUPPORT
175         cachefs_unregister_netfs(&afs_cache_netfs);
176 #endif
177         afs_proc_cleanup();
178
179 } /* end afs_exit() */
180
181 /*****************************************************************************/
182 /*
183  * notification that new peer record is being added
184  * - called from krxsecd
185  * - return an error to induce an abort
186  * - mustn't sleep (caller holds an rwlock)
187  */
188 static int afs_adding_peer(struct rxrpc_peer *peer)
189 {
190         struct afs_server *server;
191         int ret;
192
193         _debug("kAFS: Adding new peer %08x\n", ntohl(peer->addr.s_addr));
194
195         /* determine which server the peer resides in (if any) */
196         ret = afs_server_find_by_peer(peer, &server);
197         if (ret < 0)
198                 return ret; /* none that we recognise, so abort */
199
200         _debug("Server %p{u=%d}\n", server, atomic_read(&server->usage));
201
202         _debug("Cell %p{u=%d}\n",
203                server->cell, atomic_read(&server->cell->usage));
204
205         /* cross-point the structs under a global lock */
206         spin_lock(&afs_server_peer_lock);
207         peer->user = server;
208         server->peer = peer;
209         spin_unlock(&afs_server_peer_lock);
210
211         afs_put_server(server);
212
213         return 0;
214 } /* end afs_adding_peer() */
215
216 /*****************************************************************************/
217 /*
218  * notification that a peer record is being discarded
219  * - called from krxiod or krxsecd
220  */
221 static void afs_discarding_peer(struct rxrpc_peer *peer)
222 {
223         struct afs_server *server;
224
225         _enter("%p",peer);
226
227         _debug("Discarding peer %08x (rtt=%lu.%lumS)\n",
228                ntohl(peer->addr.s_addr),
229                (long) (peer->rtt / 1000),
230                (long) (peer->rtt % 1000));
231
232         /* uncross-point the structs under a global lock */
233         spin_lock(&afs_server_peer_lock);
234         server = peer->user;
235         if (server) {
236                 peer->user = NULL;
237                 server->peer = NULL;
238         }
239         spin_unlock(&afs_server_peer_lock);
240
241         _leave("");
242
243 } /* end afs_discarding_peer() */
244
245 /*****************************************************************************/
246 /*
247  * clear the dead space between task_struct and kernel stack
248  * - called by supplying -finstrument-functions to gcc
249  */
250 #if 0
251 void __cyg_profile_func_enter (void *this_fn, void *call_site)
252 __attribute__((no_instrument_function));
253
254 void __cyg_profile_func_enter (void *this_fn, void *call_site)
255 {
256        asm volatile("  movl    %%esp,%%edi     \n"
257                     "  andl    %0,%%edi        \n"
258                     "  addl    %1,%%edi        \n"
259                     "  movl    %%esp,%%ecx     \n"
260                     "  subl    %%edi,%%ecx     \n"
261                     "  shrl    $2,%%ecx        \n"
262                     "  movl    $0xedededed,%%eax     \n"
263                     "  rep stosl               \n"
264                     :
265                     : "i"(~(THREAD_SIZE - 1)), "i"(sizeof(struct thread_info))
266                     : "eax", "ecx", "edi", "memory", "cc"
267                     );
268 }
269
270 void __cyg_profile_func_exit(void *this_fn, void *call_site)
271 __attribute__((no_instrument_function));
272
273 void __cyg_profile_func_exit(void *this_fn, void *call_site)
274 {
275        asm volatile("  movl    %%esp,%%edi     \n"
276                     "  andl    %0,%%edi        \n"
277                     "  addl    %1,%%edi        \n"
278                     "  movl    %%esp,%%ecx     \n"
279                     "  subl    %%edi,%%ecx     \n"
280                     "  shrl    $2,%%ecx        \n"
281                     "  movl    $0xdadadada,%%eax     \n"
282                     "  rep stosl               \n"
283                     :
284                     : "i"(~(THREAD_SIZE - 1)), "i"(sizeof(struct thread_info))
285                     : "eax", "ecx", "edi", "memory", "cc"
286                     );
287 }
288 #endif