Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / libcfs / libcfs / module.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef EXPORT_SYMTAB
38 # define EXPORT_SYMTAB
39 #endif
40 #define DEBUG_SUBSYSTEM S_LNET
41
42 #include <libcfs/libcfs.h>
43 #include <lnet/lib-lnet.h>
44 #include <lnet/lnet.h>
45 #include "tracefile.h"
46
47 void
48 kportal_memhog_free (struct libcfs_device_userstate *ldu)
49 {
50         cfs_page_t **level0p = &ldu->ldu_memhog_root_page;
51         cfs_page_t **level1p;
52         cfs_page_t **level2p;
53         int           count1;
54         int           count2;
55
56         if (*level0p != NULL) {
57
58                 level1p = (cfs_page_t **)cfs_page_address(*level0p);
59                 count1 = 0;
60
61                 while (count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
62                        *level1p != NULL) {
63
64                         level2p = (cfs_page_t **)cfs_page_address(*level1p);
65                         count2 = 0;
66
67                         while (count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
68                                *level2p != NULL) {
69
70                                 cfs_free_page(*level2p);
71                                 ldu->ldu_memhog_pages--;
72                                 level2p++;
73                                 count2++;
74                         }
75
76                         cfs_free_page(*level1p);
77                         ldu->ldu_memhog_pages--;
78                         level1p++;
79                         count1++;
80                 }
81
82                 cfs_free_page(*level0p);
83                 ldu->ldu_memhog_pages--;
84
85                 *level0p = NULL;
86         }
87
88         LASSERT (ldu->ldu_memhog_pages == 0);
89 }
90
91 int
92 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
93 {
94         cfs_page_t **level0p;
95         cfs_page_t **level1p;
96         cfs_page_t **level2p;
97         int           count1;
98         int           count2;
99
100         LASSERT (ldu->ldu_memhog_pages == 0);
101         LASSERT (ldu->ldu_memhog_root_page == NULL);
102
103         if (npages < 0)
104                 return -EINVAL;
105
106         if (npages == 0)
107                 return 0;
108
109         level0p = &ldu->ldu_memhog_root_page;
110         *level0p = cfs_alloc_page(flags);
111         if (*level0p == NULL)
112                 return -ENOMEM;
113         ldu->ldu_memhog_pages++;
114
115         level1p = (cfs_page_t **)cfs_page_address(*level0p);
116         count1 = 0;
117         memset(level1p, 0, CFS_PAGE_SIZE);
118
119         while (ldu->ldu_memhog_pages < npages &&
120                count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
121
122                 if (cfs_signal_pending())
123                         return (-EINTR);
124
125                 *level1p = cfs_alloc_page(flags);
126                 if (*level1p == NULL)
127                         return -ENOMEM;
128                 ldu->ldu_memhog_pages++;
129
130                 level2p = (cfs_page_t **)cfs_page_address(*level1p);
131                 count2 = 0;
132                 memset(level2p, 0, CFS_PAGE_SIZE);
133
134                 while (ldu->ldu_memhog_pages < npages &&
135                        count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
136
137                         if (cfs_signal_pending())
138                                 return (-EINTR);
139
140                         *level2p = cfs_alloc_page(flags);
141                         if (*level2p == NULL)
142                                 return (-ENOMEM);
143                         ldu->ldu_memhog_pages++;
144
145                         level2p++;
146                         count2++;
147                 }
148
149                 level1p++;
150                 count1++;
151         }
152
153         return 0;
154 }
155
156 /* called when opening /dev/device */
157 static int libcfs_psdev_open(unsigned long flags, void *args)
158 {
159         struct libcfs_device_userstate *ldu;
160         ENTRY;
161
162         PORTAL_MODULE_USE;
163
164         LIBCFS_ALLOC(ldu, sizeof(*ldu));
165         if (ldu != NULL) {
166                 ldu->ldu_memhog_pages = 0;
167                 ldu->ldu_memhog_root_page = NULL;
168         }
169         *(struct libcfs_device_userstate **)args = ldu;
170
171         RETURN(0);
172 }
173
174 /* called when closing /dev/device */
175 static int libcfs_psdev_release(unsigned long flags, void *args)
176 {
177         struct libcfs_device_userstate *ldu;
178         ENTRY;
179
180         ldu = (struct libcfs_device_userstate *)args;
181         if (ldu != NULL) {
182                 kportal_memhog_free(ldu);
183                 LIBCFS_FREE(ldu, sizeof(*ldu));
184         }
185
186         PORTAL_MODULE_UNUSE;
187         RETURN(0);
188 }
189
190 static struct rw_semaphore ioctl_list_sem;
191 static struct list_head ioctl_list;
192
193 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
194 {
195         int rc = 0;
196
197         down_write(&ioctl_list_sem);
198         if (!list_empty(&hand->item))
199                 rc = -EBUSY;
200         else
201                 list_add_tail(&hand->item, &ioctl_list);
202         up_write(&ioctl_list_sem);
203
204         return rc;
205 }
206 EXPORT_SYMBOL(libcfs_register_ioctl);
207
208 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
209 {
210         int rc = 0;
211
212         down_write(&ioctl_list_sem);
213         if (list_empty(&hand->item))
214                 rc = -ENOENT;
215         else
216                 list_del_init(&hand->item);
217         up_write(&ioctl_list_sem);
218
219         return rc;
220 }
221 EXPORT_SYMBOL(libcfs_deregister_ioctl);
222
223 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
224 {
225         char    buf[1024];
226         int err = -EINVAL;
227         struct libcfs_ioctl_data *data;
228         ENTRY;
229
230         /* 'cmd' and permissions get checked in our arch-specific caller */
231
232         if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
233                 CERROR("PORTALS ioctl: data error\n");
234                 RETURN(-EINVAL);
235         }
236         data = (struct libcfs_ioctl_data *)buf;
237
238         switch (cmd) {
239         case IOC_LIBCFS_CLEAR_DEBUG:
240                 libcfs_debug_clear_buffer();
241                 RETURN(0);
242         /*
243          * case IOC_LIBCFS_PANIC:
244          * Handled in arch/cfs_module.c
245          */
246         case IOC_LIBCFS_MARK_DEBUG:
247                 if (data->ioc_inlbuf1 == NULL ||
248                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
249                         RETURN(-EINVAL);
250                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
251                 RETURN(0);
252 #if LWT_SUPPORT
253         case IOC_LIBCFS_LWT_CONTROL:
254                 err = lwt_control ((data->ioc_flags & 1) != 0, 
255                                    (data->ioc_flags & 2) != 0);
256                 break;
257
258         case IOC_LIBCFS_LWT_SNAPSHOT: {
259                 cycles_t   now;
260                 int        ncpu;
261                 int        total_size;
262
263                 err = lwt_snapshot (&now, &ncpu, &total_size,
264                                     data->ioc_pbuf1, data->ioc_plen1);
265                 data->ioc_u64[0] = now;
266                 data->ioc_u32[0] = ncpu;
267                 data->ioc_u32[1] = total_size;
268
269                 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
270                 data->ioc_u32[2] = sizeof(lwt_event_t);
271                 data->ioc_u32[3] = offsetof(lwt_event_t, lwte_where);
272
273                 if (err == 0 &&
274                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
275                         err = -EFAULT;
276                 break;
277         }
278
279         case IOC_LIBCFS_LWT_LOOKUP_STRING:
280                 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
281                                          data->ioc_pbuf2, data->ioc_plen2);
282                 if (err == 0 &&
283                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
284                         err = -EFAULT;
285                 break;
286 #endif
287         case IOC_LIBCFS_MEMHOG:
288                 if (pfile->private_data == NULL) {
289                         err = -EINVAL;
290                 } else {
291                         kportal_memhog_free(pfile->private_data);
292                         /* XXX The ioc_flags is not GFP flags now, need to be fixed */
293                         err = kportal_memhog_alloc(pfile->private_data,
294                                                    data->ioc_count,
295                                                    data->ioc_flags);
296                         if (err != 0)
297                                 kportal_memhog_free(pfile->private_data);
298                 }
299                 break;
300
301         case IOC_LIBCFS_PING_TEST: {
302                 extern void (kping_client)(struct libcfs_ioctl_data *);
303                 void (*ping)(struct libcfs_ioctl_data *);
304
305                 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
306                        data->ioc_count, libcfs_nid2str(data->ioc_nid),
307                        libcfs_nid2str(data->ioc_nid));
308                 ping = PORTAL_SYMBOL_GET(kping_client);
309                 if (!ping)
310                         CERROR("PORTAL_SYMBOL_GET failed\n");
311                 else {
312                         ping(data);
313                         PORTAL_SYMBOL_PUT(kping_client);
314                 }
315                 RETURN(0);
316         }
317
318         default: {
319                 struct libcfs_ioctl_handler *hand;
320                 err = -EINVAL;
321                 down_read(&ioctl_list_sem);
322                 list_for_each_entry(hand, &ioctl_list, item) {
323                         err = hand->handle_ioctl(cmd, data);
324                         if (err != -EINVAL) {
325                                 if (err == 0)
326                                         err = libcfs_ioctl_popdata(arg, 
327                                                         data, sizeof (*data));
328                                 break;
329                         }
330                 }
331                 up_read(&ioctl_list_sem);
332                 break;
333         }
334         }
335
336         RETURN(err);
337 }
338
339 struct cfs_psdev_ops libcfs_psdev_ops = {
340         libcfs_psdev_open,
341         libcfs_psdev_release,
342         NULL,
343         NULL,
344         libcfs_ioctl
345 };
346
347 extern int insert_proc(void);
348 extern void remove_proc(void);
349 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
350 MODULE_DESCRIPTION("Portals v3.1");
351 MODULE_LICENSE("GPL");
352
353 extern cfs_psdev_t libcfs_dev;
354 extern struct rw_semaphore tracefile_sem;
355 extern struct semaphore trace_thread_sem;
356
357 extern void libcfs_init_nidstrings(void);
358 extern int libcfs_arch_init(void);
359 extern void libcfs_arch_cleanup(void);
360
361 static int init_libcfs_module(void)
362 {
363         int rc;
364
365         libcfs_arch_init();
366         libcfs_init_nidstrings();
367         init_rwsem(&tracefile_sem);
368         init_mutex(&trace_thread_sem);
369         init_rwsem(&ioctl_list_sem);
370         CFS_INIT_LIST_HEAD(&ioctl_list);
371
372         rc = libcfs_debug_init(5 * 1024 * 1024);
373         if (rc < 0) {
374                 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
375                 return (rc);
376         }
377
378 #if LWT_SUPPORT
379         rc = lwt_init();
380         if (rc != 0) {
381                 CERROR("lwt_init: error %d\n", rc);
382                 goto cleanup_debug;
383         }
384 #endif
385         rc = cfs_psdev_register(&libcfs_dev);
386         if (rc) {
387                 CERROR("misc_register: error %d\n", rc);
388                 goto cleanup_lwt;
389         }
390
391         rc = insert_proc();
392         if (rc) {
393                 CERROR("insert_proc: error %d\n", rc);
394                 goto cleanup_deregister;
395         }
396
397         CDEBUG (D_OTHER, "portals setup OK\n");
398         return (0);
399
400  cleanup_deregister:
401         cfs_psdev_deregister(&libcfs_dev);
402  cleanup_lwt:
403 #if LWT_SUPPORT
404         lwt_fini();
405  cleanup_debug:
406 #endif
407         libcfs_debug_cleanup();
408         return rc;
409 }
410
411 static void exit_libcfs_module(void)
412 {
413         int rc;
414
415         remove_proc();
416
417         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
418                atomic_read(&libcfs_kmemory));
419
420         rc = cfs_psdev_deregister(&libcfs_dev);
421         if (rc)
422                 CERROR("misc_deregister error %d\n", rc);
423
424 #if LWT_SUPPORT
425         lwt_fini();
426 #endif
427
428         if (atomic_read(&libcfs_kmemory) != 0)
429                 CERROR("Portals memory leaked: %d bytes\n",
430                        atomic_read(&libcfs_kmemory));
431
432         rc = libcfs_debug_cleanup();
433         if (rc)
434                 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n", rc);
435         libcfs_arch_cleanup();
436 }
437
438 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);