make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / kernel / module.c
1 #include <linux/config.h>
2 #include <linux/mm.h>
3 #include <linux/module.h>
4 #include <asm/module.h>
5 #include <asm/uaccess.h>
6 #include <linux/vmalloc.h>
7 #include <linux/smp_lock.h>
8 #include <asm/pgalloc.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/kmod.h>
12 #include <linux/seq_file.h>
13
14 /*
15  * Originally by Anonymous (as far as I know...)
16  * Linux version by Bas Laarhoven <bas@vimec.nl>
17  * 0.99.14 version by Jon Tombs <jon@gtex02.us.es>,
18  * Heavily modified by Bjorn Ekwall <bj0rn@blox.se> May 1994 (C)
19  * Rewritten by Richard Henderson <rth@tamu.edu> Dec 1996
20  * Add MOD_INITIALIZING Keith Owens <kaos@ocs.com.au> Nov 1999
21  * Add kallsyms support, Keith Owens <kaos@ocs.com.au> Apr 2000
22  * Add asm/module support, IA64 has special requirements.  Keith Owens <kaos@ocs.com.au> Sep 2000
23  * Fix assorted bugs in module verification.  Keith Owens <kaos@ocs.com.au> Sep 2000
24  * Fix sys_init_module race, Andrew Morton <andrewm@uow.edu.au> Oct 2000
25  *     http://www.uwsg.iu.edu/hypermail/linux/kernel/0008.3/0379.html
26  * Replace xxx_module_symbol with inter_module_xxx.  Keith Owens <kaos@ocs.com.au> Oct 2000
27  * Add a module list lock for kernel fault race fixing. Alan Cox <alan@redhat.com>
28  *
29  * This source is covered by the GNU GPL, the same as all kernel sources.
30  */
31
32 #if defined(CONFIG_MODULES) || defined(CONFIG_KALLSYMS)
33
34 extern struct module_symbol __start___ksymtab[];
35 extern struct module_symbol __stop___ksymtab[];
36
37 extern const struct exception_table_entry __start___ex_table[];
38 extern const struct exception_table_entry __stop___ex_table[];
39
40 extern const char __start___kallsyms[] __attribute__ ((weak));
41 extern const char __stop___kallsyms[] __attribute__ ((weak));
42
43 struct module kernel_module =
44 {
45         size_of_struct:         sizeof(struct module),
46         name:                   "",
47         uc:                     {ATOMIC_INIT(1)},
48         flags:                  MOD_RUNNING,
49         syms:                   __start___ksymtab,
50         ex_table_start:         __start___ex_table,
51         ex_table_end:           __stop___ex_table,
52         kallsyms_start:         __start___kallsyms,
53         kallsyms_end:           __stop___kallsyms,
54 };
55
56 struct module *module_list = &kernel_module;
57
58 #endif  /* defined(CONFIG_MODULES) || defined(CONFIG_KALLSYMS) */
59
60 /* inter_module functions are always available, even when the kernel is
61  * compiled without modules.  Consumers of inter_module_xxx routines
62  * will always work, even when both are built into the kernel, this
63  * approach removes lots of #ifdefs in mainline code.
64  */
65
66 static struct list_head ime_list = LIST_HEAD_INIT(ime_list);
67 static spinlock_t ime_lock = SPIN_LOCK_UNLOCKED;
68 static int kmalloc_failed;
69
70 /*
71  *      This lock prevents modifications that might race the kernel fault
72  *      fixups. It does not prevent reader walks that the modules code
73  *      does. The kernel lock does that.
74  *
75  *      Since vmalloc fault fixups occur in any context this lock is taken
76  *      irqsave at all times.
77  */
78  
79 spinlock_t modlist_lock = SPIN_LOCK_UNLOCKED;
80
81 /**
82  * inter_module_register - register a new set of inter module data.
83  * @im_name: an arbitrary string to identify the data, must be unique
84  * @owner: module that is registering the data, always use THIS_MODULE
85  * @userdata: pointer to arbitrary userdata to be registered
86  *
87  * Description: Check that the im_name has not already been registered,
88  * complain if it has.  For new data, add it to the inter_module_entry
89  * list.
90  */
91 void inter_module_register(const char *im_name, struct module *owner, const void *userdata)
92 {
93         struct list_head *tmp;
94         struct inter_module_entry *ime, *ime_new;
95
96         if (!(ime_new = kmalloc(sizeof(*ime), GFP_KERNEL))) {
97                 /* Overloaded kernel, not fatal */
98                 printk(KERN_ERR
99                         "Aiee, inter_module_register: cannot kmalloc entry for '%s'\n",
100                         im_name);
101                 kmalloc_failed = 1;
102                 return;
103         }
104         memset(ime_new, 0, sizeof(*ime_new));
105         ime_new->im_name = im_name;
106         ime_new->owner = owner;
107         ime_new->userdata = userdata;
108
109         spin_lock(&ime_lock);
110         list_for_each(tmp, &ime_list) {
111                 ime = list_entry(tmp, struct inter_module_entry, list);
112                 if (strcmp(ime->im_name, im_name) == 0) {
113                         spin_unlock(&ime_lock);
114                         kfree(ime_new);
115                         /* Program logic error, fatal */
116                         printk(KERN_ERR "inter_module_register: duplicate im_name '%s'", im_name);
117                         BUG();
118                 }
119         }
120         list_add(&(ime_new->list), &ime_list);
121         spin_unlock(&ime_lock);
122 }
123
124 /**
125  * inter_module_unregister - unregister a set of inter module data.
126  * @im_name: an arbitrary string to identify the data, must be unique
127  *
128  * Description: Check that the im_name has been registered, complain if
129  * it has not.  For existing data, remove it from the
130  * inter_module_entry list.
131  */
132 void inter_module_unregister(const char *im_name)
133 {
134         struct list_head *tmp;
135         struct inter_module_entry *ime;
136
137         spin_lock(&ime_lock);
138         list_for_each(tmp, &ime_list) {
139                 ime = list_entry(tmp, struct inter_module_entry, list);
140                 if (strcmp(ime->im_name, im_name) == 0) {
141                         list_del(&(ime->list));
142                         spin_unlock(&ime_lock);
143                         kfree(ime);
144                         return;
145                 }
146         }
147         spin_unlock(&ime_lock);
148         if (kmalloc_failed) {
149                 printk(KERN_ERR
150                         "inter_module_unregister: no entry for '%s', "
151                         "probably caused by previous kmalloc failure\n",
152                         im_name);
153                 return;
154         }
155         else {
156                 /* Program logic error, fatal */
157                 printk(KERN_ERR "inter_module_unregister: no entry for '%s'", im_name);
158                 BUG();
159         }
160 }
161
162 /**
163  * inter_module_get - return arbitrary userdata from another module.
164  * @im_name: an arbitrary string to identify the data, must be unique
165  *
166  * Description: If the im_name has not been registered, return NULL.
167  * Try to increment the use count on the owning module, if that fails
168  * then return NULL.  Otherwise return the userdata.
169  */
170 const void *inter_module_get(const char *im_name)
171 {
172         struct list_head *tmp;
173         struct inter_module_entry *ime;
174         const void *result = NULL;
175
176         spin_lock(&ime_lock);
177         list_for_each(tmp, &ime_list) {
178                 ime = list_entry(tmp, struct inter_module_entry, list);
179                 if (strcmp(ime->im_name, im_name) == 0) {
180                         if (try_inc_mod_count(ime->owner))
181                                 result = ime->userdata;
182                         break;
183                 }
184         }
185         spin_unlock(&ime_lock);
186         return(result);
187 }
188
189 /**
190  * inter_module_get_request - im get with automatic request_module.
191  * @im_name: an arbitrary string to identify the data, must be unique
192  * @modname: module that is expected to register im_name
193  *
194  * Description: If inter_module_get fails, do request_module then retry.
195  */
196 const void *inter_module_get_request(const char *im_name, const char *modname)
197 {
198         const void *result = inter_module_get(im_name);
199         if (!result) {
200                 request_module(modname);
201                 result = inter_module_get(im_name);
202         }
203         return(result);
204 }
205
206 /**
207  * inter_module_put - release use of data from another module.
208  * @im_name: an arbitrary string to identify the data, must be unique
209  *
210  * Description: If the im_name has not been registered, complain,
211  * otherwise decrement the use count on the owning module.
212  */
213 void inter_module_put(const char *im_name)
214 {
215         struct list_head *tmp;
216         struct inter_module_entry *ime;
217
218         spin_lock(&ime_lock);
219         list_for_each(tmp, &ime_list) {
220                 ime = list_entry(tmp, struct inter_module_entry, list);
221                 if (strcmp(ime->im_name, im_name) == 0) {
222                         if (ime->owner)
223                                 __MOD_DEC_USE_COUNT(ime->owner);
224                         spin_unlock(&ime_lock);
225                         return;
226                 }
227         }
228         spin_unlock(&ime_lock);
229         printk(KERN_ERR "inter_module_put: no entry for '%s'", im_name);
230         BUG();
231 }
232
233
234 #if defined(CONFIG_MODULES)     /* The rest of the source */
235
236 static long get_mod_name(const char *user_name, char **buf);
237 static void put_mod_name(char *buf);
238 struct module *find_module(const char *name);
239 void free_module(struct module *, int tag_freed);
240
241
242 /*
243  * Called at boot time
244  */
245
246 void __init init_modules(void)
247 {
248         kernel_module.nsyms = __stop___ksymtab - __start___ksymtab;
249
250         arch_init_modules(&kernel_module);
251 }
252
253 /*
254  * Copy the name of a module from user space.
255  */
256
257 static inline long
258 get_mod_name(const char *user_name, char **buf)
259 {
260         unsigned long page;
261         long retval;
262
263         page = __get_free_page(GFP_KERNEL);
264         if (!page)
265                 return -ENOMEM;
266
267         retval = strncpy_from_user((char *)page, user_name, PAGE_SIZE);
268         if (retval > 0) {
269                 if (retval < PAGE_SIZE) {
270                         *buf = (char *)page;
271                         return retval;
272                 }
273                 retval = -ENAMETOOLONG;
274         } else if (!retval)
275                 retval = -EINVAL;
276
277         free_page(page);
278         return retval;
279 }
280
281 static inline void
282 put_mod_name(char *buf)
283 {
284         free_page((unsigned long)buf);
285 }
286
287 /*
288  * Allocate space for a module.
289  */
290
291 asmlinkage unsigned long
292 sys_create_module(const char *name_user, size_t size)
293 {
294         char *name;
295         long namelen, error;
296         struct module *mod;
297         unsigned long flags;
298
299         if (!capable(CAP_SYS_MODULE))
300                 return -EPERM;
301         lock_kernel();
302         if ((namelen = get_mod_name(name_user, &name)) < 0) {
303                 error = namelen;
304                 goto err0;
305         }
306         if (size < sizeof(struct module)+namelen+1) {
307                 error = -EINVAL;
308                 goto err1;
309         }
310         if (find_module(name) != NULL) {
311                 error = -EEXIST;
312                 goto err1;
313         }
314         if ((mod = (struct module *)module_map(size)) == NULL) {
315                 error = -ENOMEM;
316                 goto err1;
317         }
318
319         memset(mod, 0, sizeof(*mod));
320         mod->size_of_struct = sizeof(*mod);
321         mod->name = (char *)(mod + 1);
322         mod->size = size;
323         memcpy((char*)(mod+1), name, namelen+1);
324
325         put_mod_name(name);
326
327         spin_lock_irqsave(&modlist_lock, flags);
328         mod->next = module_list;
329         module_list = mod;      /* link it in */
330         spin_unlock_irqrestore(&modlist_lock, flags);
331
332         error = (long) mod;
333         goto err0;
334 err1:
335         put_mod_name(name);
336 err0:
337         unlock_kernel();
338         return error;
339 }
340
341 /*
342  * Initialize a module.
343  */
344
345 asmlinkage long
346 sys_init_module(const char *name_user, struct module *mod_user)
347 {
348         struct module mod_tmp, *mod;
349         char *name, *n_name, *name_tmp = NULL;
350         long namelen, n_namelen, i, error;
351         unsigned long mod_user_size;
352         struct module_ref *dep;
353
354         if (!capable(CAP_SYS_MODULE))
355                 return -EPERM;
356         lock_kernel();
357         if ((namelen = get_mod_name(name_user, &name)) < 0) {
358                 error = namelen;
359                 goto err0;
360         }
361         if ((mod = find_module(name)) == NULL) {
362                 error = -ENOENT;
363                 goto err1;
364         }
365
366         /* Check module header size.  We allow a bit of slop over the
367            size we are familiar with to cope with a version of insmod
368            for a newer kernel.  But don't over do it. */
369         if ((error = get_user(mod_user_size, &mod_user->size_of_struct)) != 0)
370                 goto err1;
371         if (mod_user_size < (unsigned long)&((struct module *)0L)->persist_start
372             || mod_user_size > sizeof(struct module) + 16*sizeof(void*)) {
373                 printk(KERN_ERR "init_module: Invalid module header size.\n"
374                        KERN_ERR "A new version of the modutils is likely "
375                                 "needed.\n");
376                 error = -EINVAL;
377                 goto err1;
378         }
379
380         /* Hold the current contents while we play with the user's idea
381            of righteousness.  */
382         mod_tmp = *mod;
383         name_tmp = kmalloc(strlen(mod->name) + 1, GFP_KERNEL);  /* Where's kstrdup()? */
384         if (name_tmp == NULL) {
385                 error = -ENOMEM;
386                 goto err1;
387         }
388         strcpy(name_tmp, mod->name);
389
390         error = copy_from_user(mod, mod_user, mod_user_size);
391         if (error) {
392                 error = -EFAULT;
393                 goto err2;
394         }
395
396         /* Sanity check the size of the module.  */
397         error = -EINVAL;
398
399         if (mod->size > mod_tmp.size) {
400                 printk(KERN_ERR "init_module: Size of initialized module "
401                                 "exceeds size of created module.\n");
402                 goto err2;
403         }
404
405         /* Make sure all interesting pointers are sane.  */
406
407         if (!mod_bound(mod->name, namelen, mod)) {
408                 printk(KERN_ERR "init_module: mod->name out of bounds.\n");
409                 goto err2;
410         }
411         if (mod->nsyms && !mod_bound(mod->syms, mod->nsyms, mod)) {
412                 printk(KERN_ERR "init_module: mod->syms out of bounds.\n");
413                 goto err2;
414         }
415         if (mod->ndeps && !mod_bound(mod->deps, mod->ndeps, mod)) {
416                 printk(KERN_ERR "init_module: mod->deps out of bounds.\n");
417                 goto err2;
418         }
419         if (mod->init && !mod_bound(mod->init, 0, mod)) {
420                 printk(KERN_ERR "init_module: mod->init out of bounds.\n");
421                 goto err2;
422         }
423         if (mod->cleanup && !mod_bound(mod->cleanup, 0, mod)) {
424                 printk(KERN_ERR "init_module: mod->cleanup out of bounds.\n");
425                 goto err2;
426         }
427         if (mod->ex_table_start > mod->ex_table_end
428             || (mod->ex_table_start &&
429                 !((unsigned long)mod->ex_table_start >= ((unsigned long)mod + mod->size_of_struct)
430                   && ((unsigned long)mod->ex_table_end
431                       < (unsigned long)mod + mod->size)))
432             || (((unsigned long)mod->ex_table_start
433                  - (unsigned long)mod->ex_table_end)
434                 % sizeof(struct exception_table_entry))) {
435                 printk(KERN_ERR "init_module: mod->ex_table_* invalid.\n");
436                 goto err2;
437         }
438         if (mod->flags & ~MOD_AUTOCLEAN) {
439                 printk(KERN_ERR "init_module: mod->flags invalid.\n");
440                 goto err2;
441         }
442         if (mod_member_present(mod, can_unload)
443             && mod->can_unload && !mod_bound(mod->can_unload, 0, mod)) {
444                 printk(KERN_ERR "init_module: mod->can_unload out of bounds.\n");
445                 goto err2;
446         }
447         if (mod_member_present(mod, kallsyms_end)) {
448             if (mod->kallsyms_end &&
449                 (!mod_bound(mod->kallsyms_start, 0, mod) ||
450                  !mod_bound(mod->kallsyms_end, 0, mod))) {
451                 printk(KERN_ERR "init_module: mod->kallsyms out of bounds.\n");
452                 goto err2;
453             }
454             if (mod->kallsyms_start > mod->kallsyms_end) {
455                 printk(KERN_ERR "init_module: mod->kallsyms invalid.\n");
456                 goto err2;
457             }
458         }
459         if (mod_member_present(mod, archdata_end)) {
460             if (mod->archdata_end &&
461                 (!mod_bound(mod->archdata_start, 0, mod) ||
462                  !mod_bound(mod->archdata_end, 0, mod))) {
463                 printk(KERN_ERR "init_module: mod->archdata out of bounds.\n");
464                 goto err2;
465             }
466             if (mod->archdata_start > mod->archdata_end) {
467                 printk(KERN_ERR "init_module: mod->archdata invalid.\n");
468                 goto err2;
469             }
470         }
471         if (mod_member_present(mod, kernel_data) && mod->kernel_data) {
472             printk(KERN_ERR "init_module: mod->kernel_data must be zero.\n");
473             goto err2;
474         }
475
476         /* Check that the user isn't doing something silly with the name.  */
477
478         if ((n_namelen = get_mod_name(mod->name - (unsigned long)mod
479                                       + (unsigned long)mod_user,
480                                       &n_name)) < 0) {
481                 printk(KERN_ERR "init_module: get_mod_name failure.\n");
482                 error = n_namelen;
483                 goto err2;
484         }
485         if (namelen != n_namelen || strcmp(n_name, name_tmp) != 0) {
486                 printk(KERN_ERR "init_module: changed module name to "
487                                 "`%s' from `%s'\n",
488                        n_name, name_tmp);
489                 goto err3;
490         }
491
492         /* Ok, that's about all the sanity we can stomach; copy the rest.  */
493
494         if (copy_from_user((char *)mod+mod_user_size,
495                            (char *)mod_user+mod_user_size,
496                            mod->size-mod_user_size)) {
497                 error = -EFAULT;
498                 goto err3;
499         }
500
501         if (module_arch_init(mod))
502                 goto err3;
503
504         /* On some machines it is necessary to do something here
505            to make the I and D caches consistent.  */
506         flush_icache_range((unsigned long)mod, (unsigned long)mod + mod->size);
507
508         mod->next = mod_tmp.next;
509         mod->refs = NULL;
510
511         /* Sanity check the module's dependents */
512         for (i = 0, dep = mod->deps; i < mod->ndeps; ++i, ++dep) {
513                 struct module *o, *d = dep->dep;
514
515                 /* Make sure the indicated dependencies are really modules.  */
516                 if (d == mod) {
517                         printk(KERN_ERR "init_module: self-referential "
518                                         "dependency in mod->deps.\n");
519                         goto err3;
520                 }
521
522                 /* Scan the current modules for this dependency */
523                 for (o = module_list; o != &kernel_module && o != d; o = o->next)
524                         ;
525
526                 if (o != d) {
527                         printk(KERN_ERR "init_module: found dependency that is "
528                                 "(no longer?) a module.\n");
529                         goto err3;
530                 }
531         }
532
533         /* Update module references.  */
534         for (i = 0, dep = mod->deps; i < mod->ndeps; ++i, ++dep) {
535                 struct module *d = dep->dep;
536
537                 dep->ref = mod;
538                 dep->next_ref = d->refs;
539                 d->refs = dep;
540                 /* Being referenced by a dependent module counts as a
541                    use as far as kmod is concerned.  */
542                 d->flags |= MOD_USED_ONCE;
543         }
544
545         /* Free our temporary memory.  */
546         put_mod_name(n_name);
547         put_mod_name(name);
548
549         /* Initialize the module.  */
550         atomic_set(&mod->uc.usecount,1);
551         mod->flags |= MOD_INITIALIZING;
552         if (mod->init && (error = mod->init()) != 0) {
553                 atomic_set(&mod->uc.usecount,0);
554                 mod->flags &= ~MOD_INITIALIZING;
555                 if (error > 0)  /* Buggy module */
556                         error = -EBUSY;
557                 goto err0;
558         }
559         atomic_dec(&mod->uc.usecount);
560
561         /* And set it running.  */
562         mod->flags = (mod->flags | MOD_RUNNING) & ~MOD_INITIALIZING;
563         error = 0;
564         goto err0;
565
566 err3:
567         put_mod_name(n_name);
568 err2:
569         *mod = mod_tmp;
570         strcpy((char *)mod->name, name_tmp);    /* We know there is room for this */
571 err1:
572         put_mod_name(name);
573 err0:
574         unlock_kernel();
575         kfree(name_tmp);
576         return error;
577 }
578
579 static spinlock_t unload_lock = SPIN_LOCK_UNLOCKED;
580 int try_inc_mod_count(struct module *mod)
581 {
582         int res = 1;
583         if (mod) {
584                 spin_lock(&unload_lock);
585                 if (mod->flags & MOD_DELETED)
586                         res = 0;
587                 else
588                         __MOD_INC_USE_COUNT(mod);
589                 spin_unlock(&unload_lock);
590         }
591         return res;
592 }
593
594 asmlinkage long
595 sys_delete_module(const char *name_user)
596 {
597         struct module *mod, *next;
598         char *name;
599         long error;
600         int something_changed;
601
602         if (!capable(CAP_SYS_MODULE))
603                 return -EPERM;
604
605         lock_kernel();
606         if (name_user) {
607                 if ((error = get_mod_name(name_user, &name)) < 0)
608                         goto out;
609                 error = -ENOENT;
610                 if ((mod = find_module(name)) == NULL) {
611                         put_mod_name(name);
612                         goto out;
613                 }
614                 put_mod_name(name);
615                 error = -EBUSY;
616                 if (mod->refs != NULL)
617                         goto out;
618
619                 spin_lock(&unload_lock);
620                 if (!__MOD_IN_USE(mod)) {
621                         mod->flags |= MOD_DELETED;
622                         spin_unlock(&unload_lock);
623                         free_module(mod, 0);
624                         error = 0;
625                 } else {
626                         spin_unlock(&unload_lock);
627                 }
628                 goto out;
629         }
630
631         /* Do automatic reaping */
632 restart:
633         something_changed = 0;
634         
635         for (mod = module_list; mod != &kernel_module; mod = next) {
636                 next = mod->next;
637                 spin_lock(&unload_lock);
638                 if (mod->refs == NULL
639                     && (mod->flags & MOD_AUTOCLEAN)
640                     && (mod->flags & MOD_RUNNING)
641                     && !(mod->flags & MOD_DELETED)
642                     && (mod->flags & MOD_USED_ONCE)
643                     && !__MOD_IN_USE(mod)) {
644                         if ((mod->flags & MOD_VISITED)
645                             && !(mod->flags & MOD_JUST_FREED)) {
646                                 spin_unlock(&unload_lock);
647                                 mod->flags &= ~MOD_VISITED;
648                         } else {
649                                 mod->flags |= MOD_DELETED;
650                                 spin_unlock(&unload_lock);
651                                 free_module(mod, 1);
652                                 something_changed = 1;
653                         }
654                 } else {
655                         spin_unlock(&unload_lock);
656                 }
657         }
658         
659         if (something_changed)
660                 goto restart;
661                 
662         for (mod = module_list; mod != &kernel_module; mod = mod->next)
663                 mod->flags &= ~MOD_JUST_FREED;
664         
665         error = 0;
666 out:
667         unlock_kernel();
668         return error;
669 }
670
671 /* Query various bits about modules.  */
672
673 static int
674 qm_modules(char *buf, size_t bufsize, size_t *ret)
675 {
676         struct module *mod;
677         size_t nmod, space, len;
678
679         nmod = space = 0;
680
681         for (mod=module_list; mod != &kernel_module; mod=mod->next, ++nmod) {
682                 len = strlen(mod->name)+1;
683                 if (len > bufsize)
684                         goto calc_space_needed;
685                 if (copy_to_user(buf, mod->name, len))
686                         return -EFAULT;
687                 buf += len;
688                 bufsize -= len;
689                 space += len;
690         }
691
692         if (put_user(nmod, ret))
693                 return -EFAULT;
694         else
695                 return 0;
696
697 calc_space_needed:
698         space += len;
699         while ((mod = mod->next) != &kernel_module)
700                 space += strlen(mod->name)+1;
701
702         if (put_user(space, ret))
703                 return -EFAULT;
704         else
705                 return -ENOSPC;
706 }
707
708 static int
709 qm_deps(struct module *mod, char *buf, size_t bufsize, size_t *ret)
710 {
711         size_t i, space, len;
712
713         if (mod == &kernel_module)
714                 return -EINVAL;
715         if (!MOD_CAN_QUERY(mod))
716                 if (put_user(0, ret))
717                         return -EFAULT;
718                 else
719                         return 0;
720
721         space = 0;
722         for (i = 0; i < mod->ndeps; ++i) {
723                 const char *dep_name = mod->deps[i].dep->name;
724
725                 len = strlen(dep_name)+1;
726                 if (len > bufsize)
727                         goto calc_space_needed;
728                 if (copy_to_user(buf, dep_name, len))
729                         return -EFAULT;
730                 buf += len;
731                 bufsize -= len;
732                 space += len;
733         }
734
735         if (put_user(i, ret))
736                 return -EFAULT;
737         else
738                 return 0;
739
740 calc_space_needed:
741         space += len;
742         while (++i < mod->ndeps)
743                 space += strlen(mod->deps[i].dep->name)+1;
744
745         if (put_user(space, ret))
746                 return -EFAULT;
747         else
748                 return -ENOSPC;
749 }
750
751 static int
752 qm_refs(struct module *mod, char *buf, size_t bufsize, size_t *ret)
753 {
754         size_t nrefs, space, len;
755         struct module_ref *ref;
756
757         if (mod == &kernel_module)
758                 return -EINVAL;
759         if (!MOD_CAN_QUERY(mod))
760                 if (put_user(0, ret))
761                         return -EFAULT;
762                 else
763                         return 0;
764
765         space = 0;
766         for (nrefs = 0, ref = mod->refs; ref ; ++nrefs, ref = ref->next_ref) {
767                 const char *ref_name = ref->ref->name;
768
769                 len = strlen(ref_name)+1;
770                 if (len > bufsize)
771                         goto calc_space_needed;
772                 if (copy_to_user(buf, ref_name, len))
773                         return -EFAULT;
774                 buf += len;
775                 bufsize -= len;
776                 space += len;
777         }
778
779         if (put_user(nrefs, ret))
780                 return -EFAULT;
781         else
782                 return 0;
783
784 calc_space_needed:
785         space += len;
786         while ((ref = ref->next_ref) != NULL)
787                 space += strlen(ref->ref->name)+1;
788
789         if (put_user(space, ret))
790                 return -EFAULT;
791         else
792                 return -ENOSPC;
793 }
794
795 static int
796 qm_symbols(struct module *mod, char *buf, size_t bufsize, size_t *ret)
797 {
798         size_t i, space, len;
799         struct module_symbol *s;
800         char *strings;
801         unsigned long *vals;
802
803         if (!MOD_CAN_QUERY(mod))
804                 if (put_user(0, ret))
805                         return -EFAULT;
806                 else
807                         return 0;
808
809         space = mod->nsyms * 2*sizeof(void *);
810
811         i = len = 0;
812         s = mod->syms;
813
814         if (space > bufsize)
815                 goto calc_space_needed;
816
817         if (!access_ok(VERIFY_WRITE, buf, space))
818                 return -EFAULT;
819
820         bufsize -= space;
821         vals = (unsigned long *)buf;
822         strings = buf+space;
823
824         for (; i < mod->nsyms ; ++i, ++s, vals += 2) {
825                 len = strlen(s->name)+1;
826                 if (len > bufsize)
827                         goto calc_space_needed;
828
829                 if (copy_to_user(strings, s->name, len)
830                     || __put_user(s->value, vals+0)
831                     || __put_user(space, vals+1))
832                         return -EFAULT;
833
834                 strings += len;
835                 bufsize -= len;
836                 space += len;
837         }
838         if (put_user(i, ret))
839                 return -EFAULT;
840         else
841                 return 0;
842
843 calc_space_needed:
844         for (; i < mod->nsyms; ++i, ++s)
845                 space += strlen(s->name)+1;
846
847         if (put_user(space, ret))
848                 return -EFAULT;
849         else
850                 return -ENOSPC;
851 }
852
853 static int
854 qm_info(struct module *mod, char *buf, size_t bufsize, size_t *ret)
855 {
856         int error = 0;
857
858         if (mod == &kernel_module)
859                 return -EINVAL;
860
861         if (sizeof(struct module_info) <= bufsize) {
862                 struct module_info info;
863                 info.addr = (unsigned long)mod;
864                 info.size = mod->size;
865                 info.flags = mod->flags;
866                 
867                 /* usecount is one too high here - report appropriately to
868                    compensate for locking */
869                 info.usecount = (mod_member_present(mod, can_unload)
870                                  && mod->can_unload ? -1 : atomic_read(&mod->uc.usecount)-1);
871
872                 if (copy_to_user(buf, &info, sizeof(struct module_info)))
873                         return -EFAULT;
874         } else
875                 error = -ENOSPC;
876
877         if (put_user(sizeof(struct module_info), ret))
878                 return -EFAULT;
879
880         return error;
881 }
882
883 asmlinkage long
884 sys_query_module(const char *name_user, int which, char *buf, size_t bufsize,
885                  size_t *ret)
886 {
887         struct module *mod;
888         int err;
889
890         lock_kernel();
891         if (name_user == NULL)
892                 mod = &kernel_module;
893         else {
894                 long namelen;
895                 char *name;
896
897                 if ((namelen = get_mod_name(name_user, &name)) < 0) {
898                         err = namelen;
899                         goto out;
900                 }
901                 err = -ENOENT;
902                 if ((mod = find_module(name)) == NULL) {
903                         put_mod_name(name);
904                         goto out;
905                 }
906                 put_mod_name(name);
907         }
908
909         /* __MOD_ touches the flags. We must avoid that */
910         
911         atomic_inc(&mod->uc.usecount);
912                 
913         switch (which)
914         {
915         case 0:
916                 err = 0;
917                 break;
918         case QM_MODULES:
919                 err = qm_modules(buf, bufsize, ret);
920                 break;
921         case QM_DEPS:
922                 err = qm_deps(mod, buf, bufsize, ret);
923                 break;
924         case QM_REFS:
925                 err = qm_refs(mod, buf, bufsize, ret);
926                 break;
927         case QM_SYMBOLS:
928                 err = qm_symbols(mod, buf, bufsize, ret);
929                 break;
930         case QM_INFO:
931                 err = qm_info(mod, buf, bufsize, ret);
932                 break;
933         default:
934                 err = -EINVAL;
935                 break;
936         }
937         atomic_dec(&mod->uc.usecount);
938         
939 out:
940         unlock_kernel();
941         return err;
942 }
943
944 /*
945  * Copy the kernel symbol table to user space.  If the argument is
946  * NULL, just return the size of the table.
947  *
948  * This call is obsolete.  New programs should use query_module+QM_SYMBOLS
949  * which does not arbitrarily limit the length of symbols.
950  */
951
952 asmlinkage long
953 sys_get_kernel_syms(struct kernel_sym *table)
954 {
955         struct module *mod;
956         int i;
957         struct kernel_sym ksym;
958
959         lock_kernel();
960         for (mod = module_list, i = 0; mod; mod = mod->next) {
961                 /* include the count for the module name! */
962                 i += mod->nsyms + 1;
963         }
964
965         if (table == NULL)
966                 goto out;
967
968         /* So that we don't give the user our stack content */
969         memset (&ksym, 0, sizeof (ksym));
970
971         for (mod = module_list, i = 0; mod; mod = mod->next) {
972                 struct module_symbol *msym;
973                 unsigned int j;
974
975                 if (!MOD_CAN_QUERY(mod))
976                         continue;
977
978                 /* magic: write module info as a pseudo symbol */
979                 ksym.value = (unsigned long)mod;
980                 ksym.name[0] = '#';
981                 strncpy(ksym.name+1, mod->name, sizeof(ksym.name)-1);
982                 ksym.name[sizeof(ksym.name)-1] = '\0';
983
984                 if (copy_to_user(table, &ksym, sizeof(ksym)) != 0)
985                         goto out;
986                 ++i, ++table;
987
988                 if (mod->nsyms == 0)
989                         continue;
990
991                 for (j = 0, msym = mod->syms; j < mod->nsyms; ++j, ++msym) {
992                         ksym.value = msym->value;
993                         strncpy(ksym.name, msym->name, sizeof(ksym.name));
994                         ksym.name[sizeof(ksym.name)-1] = '\0';
995
996                         if (copy_to_user(table, &ksym, sizeof(ksym)) != 0)
997                                 goto out;
998                         ++i, ++table;
999                 }
1000         }
1001 out:
1002         unlock_kernel();
1003         return i;
1004 }
1005
1006 /*
1007  * Look for a module by name, ignoring modules marked for deletion.
1008  */
1009
1010 struct module *
1011 find_module(const char *name)
1012 {
1013         struct module *mod;
1014
1015         for (mod = module_list; mod ; mod = mod->next) {
1016                 if (mod->flags & MOD_DELETED)
1017                         continue;
1018                 if (!strcmp(mod->name, name))
1019                         break;
1020         }
1021
1022         return mod;
1023 }
1024
1025 /*
1026  * Free the given module.
1027  */
1028
1029 void
1030 free_module(struct module *mod, int tag_freed)
1031 {
1032         struct module_ref *dep;
1033         unsigned i;
1034         unsigned long flags;
1035
1036         /* Let the module clean up.  */
1037
1038         if (mod->flags & MOD_RUNNING)
1039         {
1040                 if(mod->cleanup)
1041                         mod->cleanup();
1042                 mod->flags &= ~MOD_RUNNING;
1043         }
1044
1045         /* Remove the module from the dependency lists.  */
1046
1047         for (i = 0, dep = mod->deps; i < mod->ndeps; ++i, ++dep) {
1048                 struct module_ref **pp;
1049                 for (pp = &dep->dep->refs; *pp != dep; pp = &(*pp)->next_ref)
1050                         continue;
1051                 *pp = dep->next_ref;
1052                 if (tag_freed && dep->dep->refs == NULL)
1053                         dep->dep->flags |= MOD_JUST_FREED;
1054         }
1055
1056         /* And from the main module list.  */
1057
1058         spin_lock_irqsave(&modlist_lock, flags);
1059         if (mod == module_list) {
1060                 module_list = mod->next;
1061         } else {
1062                 struct module *p;
1063                 for (p = module_list; p->next != mod; p = p->next)
1064                         continue;
1065                 p->next = mod->next;
1066         }
1067         spin_unlock_irqrestore(&modlist_lock, flags);
1068
1069         /* And free the memory.  */
1070
1071         module_unmap(mod);
1072 }
1073
1074 /*
1075  * Called by the /proc file system to return a current list of modules.
1076  */
1077
1078 int get_module_list(char *p)
1079 {
1080         size_t left = PAGE_SIZE;
1081         struct module *mod;
1082         char tmpstr[64];
1083         struct module_ref *ref;
1084
1085         for (mod = module_list; mod != &kernel_module; mod = mod->next) {
1086                 long len;
1087                 const char *q;
1088
1089 #define safe_copy_str(str, len)                                         \
1090                 do {                                                    \
1091                         if (left < len)                                 \
1092                                 goto fini;                              \
1093                         memcpy(p, str, len); p += len, left -= len;     \
1094                 } while (0)
1095 #define safe_copy_cstr(str)     safe_copy_str(str, sizeof(str)-1)
1096
1097                 len = strlen(mod->name);
1098                 safe_copy_str(mod->name, len);
1099
1100                 if ((len = 20 - len) > 0) {
1101                         if (left < len)
1102                                 goto fini;
1103                         memset(p, ' ', len);
1104                         p += len;
1105                         left -= len;
1106                 }
1107
1108                 len = sprintf(tmpstr, "%8lu", mod->size);
1109                 safe_copy_str(tmpstr, len);
1110
1111                 if (mod->flags & MOD_RUNNING) {
1112                         len = sprintf(tmpstr, "%4ld",
1113                                       (mod_member_present(mod, can_unload)
1114                                        && mod->can_unload
1115                                        ? -1L : (long)atomic_read(&mod->uc.usecount)));
1116                         safe_copy_str(tmpstr, len);
1117                 }
1118
1119                 if (mod->flags & MOD_DELETED)
1120                         safe_copy_cstr(" (deleted)");
1121                 else if (mod->flags & MOD_RUNNING) {
1122                         if (mod->flags & MOD_AUTOCLEAN)
1123                                 safe_copy_cstr(" (autoclean)");
1124                         if (!(mod->flags & MOD_USED_ONCE))
1125                                 safe_copy_cstr(" (unused)");
1126                 }
1127                 else if (mod->flags & MOD_INITIALIZING)
1128                         safe_copy_cstr(" (initializing)");
1129                 else
1130                         safe_copy_cstr(" (uninitialized)");
1131
1132                 if ((ref = mod->refs) != NULL) {
1133                         safe_copy_cstr(" [");
1134                         while (1) {
1135                                 q = ref->ref->name;
1136                                 len = strlen(q);
1137                                 safe_copy_str(q, len);
1138
1139                                 if ((ref = ref->next_ref) != NULL)
1140                                         safe_copy_cstr(" ");
1141                                 else
1142                                         break;
1143                         }
1144                         safe_copy_cstr("]");
1145                 }
1146                 safe_copy_cstr("\n");
1147
1148 #undef safe_copy_str
1149 #undef safe_copy_cstr
1150         }
1151
1152 fini:
1153         return PAGE_SIZE - left;
1154 }
1155
1156 /*
1157  * Called by the /proc file system to return a current list of ksyms.
1158  */
1159
1160 struct mod_sym {
1161         struct module *mod;
1162         int index;
1163 };
1164
1165 /* iterator */
1166
1167 static void *s_start(struct seq_file *m, loff_t *pos)
1168 {
1169         struct mod_sym *p = kmalloc(sizeof(*p), GFP_KERNEL);
1170         struct module *v;
1171         loff_t n = *pos;
1172
1173         if (!p)
1174                 return ERR_PTR(-ENOMEM);
1175         lock_kernel();
1176         for (v = module_list, n = *pos; v; n -= v->nsyms, v = v->next) {
1177                 if (n < v->nsyms) {
1178                         p->mod = v;
1179                         p->index = n;
1180                         return p;
1181                 }
1182         }
1183         unlock_kernel();
1184         kfree(p);
1185         return NULL;
1186 }
1187
1188 static void *s_next(struct seq_file *m, void *p, loff_t *pos)
1189 {
1190         struct mod_sym *v = p;
1191         (*pos)++;
1192         if (++v->index >= v->mod->nsyms) {
1193                 do {
1194                         v->mod = v->mod->next;
1195                         if (!v->mod) {
1196                                 unlock_kernel();
1197                                 kfree(p);
1198                                 return NULL;
1199                         }
1200                 } while (!v->mod->nsyms);
1201                 v->index = 0;
1202         }
1203         return p;
1204 }
1205
1206 static void s_stop(struct seq_file *m, void *p)
1207 {
1208         if (p && !IS_ERR(p)) {
1209                 unlock_kernel();
1210                 kfree(p);
1211         }
1212 }
1213
1214 static int s_show(struct seq_file *m, void *p)
1215 {
1216         struct mod_sym *v = p;
1217         struct module_symbol *sym;
1218
1219         if (!MOD_CAN_QUERY(v->mod))
1220                 return 0;
1221         sym = &v->mod->syms[v->index];
1222         if (*v->mod->name)
1223                 seq_printf(m, "%0*lx %s\t[%s]\n", (int)(2*sizeof(void*)),
1224                                sym->value, sym->name, v->mod->name);
1225         else
1226                 seq_printf(m, "%0*lx %s\n", (int)(2*sizeof(void*)),
1227                                sym->value, sym->name);
1228         return 0;
1229 }
1230
1231 struct seq_operations ksyms_op = {
1232         start:  s_start,
1233         next:   s_next,
1234         stop:   s_stop,
1235         show:   s_show
1236 };
1237
1238 #else           /* CONFIG_MODULES */
1239
1240 /* Dummy syscalls for people who don't want modules */
1241
1242 asmlinkage unsigned long
1243 sys_create_module(const char *name_user, size_t size)
1244 {
1245         return -ENOSYS;
1246 }
1247
1248 asmlinkage long
1249 sys_init_module(const char *name_user, struct module *mod_user)
1250 {
1251         return -ENOSYS;
1252 }
1253
1254 asmlinkage long
1255 sys_delete_module(const char *name_user)
1256 {
1257         return -ENOSYS;
1258 }
1259
1260 asmlinkage long
1261 sys_query_module(const char *name_user, int which, char *buf, size_t bufsize,
1262                  size_t *ret)
1263 {
1264         /* Let the program know about the new interface.  Not that
1265            it'll do them much good.  */
1266         if (which == 0)
1267                 return 0;
1268
1269         return -ENOSYS;
1270 }
1271
1272 asmlinkage long
1273 sys_get_kernel_syms(struct kernel_sym *table)
1274 {
1275         return -ENOSYS;
1276 }
1277
1278 int try_inc_mod_count(struct module *mod)
1279 {
1280         return 1;
1281 }
1282
1283 #endif  /* CONFIG_MODULES */