Whamcloud - gitweb
b60201695cbf01f2fcbaf79fe1b853dcfd75a229
[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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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 (c) 2008, 2010, Oracle and/or its affiliates. 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 cfs_rw_semaphore_t ioctl_list_sem;
191 static cfs_list_t ioctl_list;
192
193 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
194 {
195         int rc = 0;
196
197         cfs_down_write(&ioctl_list_sem);
198         if (!cfs_list_empty(&hand->item))
199                 rc = -EBUSY;
200         else
201                 cfs_list_add_tail(&hand->item, &ioctl_list);
202         cfs_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         cfs_down_write(&ioctl_list_sem);
213         if (cfs_list_empty(&hand->item))
214                 rc = -ENOENT;
215         else
216                 cfs_list_del_init(&hand->item);
217         cfs_up_write(&ioctl_list_sem);
218
219         return rc;
220 }
221 EXPORT_SYMBOL(libcfs_deregister_ioctl);
222
223 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile,unsigned long cmd,
224                             void *arg, struct libcfs_ioctl_data *data)
225 {
226         int err = -EINVAL;
227         ENTRY;
228
229         switch (cmd) {
230         case IOC_LIBCFS_CLEAR_DEBUG:
231                 libcfs_debug_clear_buffer();
232                 RETURN(0);
233         /*
234          * case IOC_LIBCFS_PANIC:
235          * Handled in arch/cfs_module.c
236          */
237         case IOC_LIBCFS_MARK_DEBUG:
238                 if (data->ioc_inlbuf1 == NULL ||
239                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
240                         RETURN(-EINVAL);
241                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
242                 RETURN(0);
243 #if LWT_SUPPORT
244         case IOC_LIBCFS_LWT_CONTROL:
245                 err = lwt_control ((data->ioc_flags & 1) != 0, 
246                                    (data->ioc_flags & 2) != 0);
247                 break;
248
249         case IOC_LIBCFS_LWT_SNAPSHOT: {
250                 cfs_cycles_t   now;
251                 int            ncpu;
252                 int            total_size;
253
254                 err = lwt_snapshot (&now, &ncpu, &total_size,
255                                     data->ioc_pbuf1, data->ioc_plen1);
256                 data->ioc_u64[0] = now;
257                 data->ioc_u32[0] = ncpu;
258                 data->ioc_u32[1] = total_size;
259
260                 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
261                 data->ioc_u32[2] = sizeof(lwt_event_t);
262                 data->ioc_u32[3] = offsetof(lwt_event_t, lwte_where);
263
264                 if (err == 0 &&
265                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
266                         err = -EFAULT;
267                 break;
268         }
269
270         case IOC_LIBCFS_LWT_LOOKUP_STRING:
271                 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
272                                          data->ioc_pbuf2, data->ioc_plen2);
273                 if (err == 0 &&
274                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
275                         err = -EFAULT;
276                 break;
277 #endif
278         case IOC_LIBCFS_MEMHOG:
279                 if (pfile->private_data == NULL) {
280                         err = -EINVAL;
281                 } else {
282                         kportal_memhog_free(pfile->private_data);
283                         /* XXX The ioc_flags is not GFP flags now, need to be fixed */
284                         err = kportal_memhog_alloc(pfile->private_data,
285                                                    data->ioc_count,
286                                                    data->ioc_flags);
287                         if (err != 0)
288                                 kportal_memhog_free(pfile->private_data);
289                 }
290                 break;
291
292         case IOC_LIBCFS_PING_TEST: {
293                 extern void (kping_client)(struct libcfs_ioctl_data *);
294                 void (*ping)(struct libcfs_ioctl_data *);
295
296                 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
297                        data->ioc_count, libcfs_nid2str(data->ioc_nid),
298                        libcfs_nid2str(data->ioc_nid));
299                 ping = PORTAL_SYMBOL_GET(kping_client);
300                 if (!ping)
301                         CERROR("PORTAL_SYMBOL_GET failed\n");
302                 else {
303                         ping(data);
304                         PORTAL_SYMBOL_PUT(kping_client);
305                 }
306                 RETURN(0);
307         }
308
309         default: {
310                 struct libcfs_ioctl_handler *hand;
311                 err = -EINVAL;
312                 cfs_down_read(&ioctl_list_sem);
313                 cfs_list_for_each_entry_typed(hand, &ioctl_list,
314                         struct libcfs_ioctl_handler, item) {
315                         err = hand->handle_ioctl(cmd, data);
316                         if (err != -EINVAL) {
317                                 if (err == 0)
318                                         err = libcfs_ioctl_popdata(arg, 
319                                                         data, sizeof (*data));
320                                 break;
321                         }
322                 }
323                 cfs_up_read(&ioctl_list_sem);
324                 break;
325         }
326         }
327
328         RETURN(err);
329 }
330
331 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
332 {
333         char    *buf;
334         struct libcfs_ioctl_data *data;
335         int err = 0;
336         ENTRY;
337
338         LIBCFS_ALLOC_GFP(buf, 1024, CFS_ALLOC_STD);
339         if (buf == NULL)
340                 RETURN(-ENOMEM);
341
342         /* 'cmd' and permissions get checked in our arch-specific caller */
343         if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
344                 CERROR("PORTALS ioctl: data error\n");
345                 GOTO(out, err = -EINVAL);
346         }
347         data = (struct libcfs_ioctl_data *)buf;
348
349         err = libcfs_ioctl_int(pfile, cmd, arg, data);
350
351 out:
352         LIBCFS_FREE(buf, 1024);
353         RETURN(err);
354 }
355
356
357 struct cfs_psdev_ops libcfs_psdev_ops = {
358         libcfs_psdev_open,
359         libcfs_psdev_release,
360         NULL,
361         NULL,
362         libcfs_ioctl
363 };
364
365 extern int insert_proc(void);
366 extern void remove_proc(void);
367 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
368 MODULE_DESCRIPTION("Portals v3.1");
369 MODULE_LICENSE("GPL");
370
371 extern cfs_psdev_t libcfs_dev;
372 extern cfs_rw_semaphore_t cfs_tracefile_sem;
373 extern cfs_semaphore_t cfs_trace_thread_sem;
374
375 extern void libcfs_init_nidstrings(void);
376 extern int libcfs_arch_init(void);
377 extern void libcfs_arch_cleanup(void);
378
379 static int init_libcfs_module(void)
380 {
381         int rc;
382
383         libcfs_arch_init();
384         libcfs_init_nidstrings();
385         cfs_init_rwsem(&cfs_tracefile_sem);
386         cfs_init_mutex(&cfs_trace_thread_sem);
387         cfs_init_rwsem(&ioctl_list_sem);
388         CFS_INIT_LIST_HEAD(&ioctl_list);
389         cfs_waitq_init(&cfs_race_waitq);
390
391         rc = libcfs_debug_init(5 * 1024 * 1024);
392         if (rc < 0) {
393                 printk(CFS_KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
394                 return (rc);
395         }
396
397 #if LWT_SUPPORT
398         rc = lwt_init();
399         if (rc != 0) {
400                 CERROR("lwt_init: error %d\n", rc);
401                 goto cleanup_debug;
402         }
403 #endif
404         rc = cfs_psdev_register(&libcfs_dev);
405         if (rc) {
406                 CERROR("misc_register: error %d\n", rc);
407                 goto cleanup_lwt;
408         }
409
410         rc = cfs_wi_startup();
411         if (rc) {
412                 CERROR("startup workitem: error %d\n", rc);
413                 goto cleanup_deregister;
414         }
415
416         rc = insert_proc();
417         if (rc) {
418                 CERROR("insert_proc: error %d\n", rc);
419                 goto cleanup_wi;
420         }
421
422         CDEBUG (D_OTHER, "portals setup OK\n");
423         return (0);
424
425  cleanup_wi:
426         cfs_wi_shutdown();
427  cleanup_deregister:
428         cfs_psdev_deregister(&libcfs_dev);
429  cleanup_lwt:
430 #if LWT_SUPPORT
431         lwt_fini();
432  cleanup_debug:
433 #endif
434         libcfs_debug_cleanup();
435         return rc;
436 }
437
438 static void exit_libcfs_module(void)
439 {
440         int rc;
441
442         remove_proc();
443
444         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
445                cfs_atomic_read(&libcfs_kmemory));
446
447         cfs_wi_shutdown();
448         rc = cfs_psdev_deregister(&libcfs_dev);
449         if (rc)
450                 CERROR("misc_deregister error %d\n", rc);
451
452 #if LWT_SUPPORT
453         lwt_fini();
454 #endif
455
456         if (cfs_atomic_read(&libcfs_kmemory) != 0)
457                 CERROR("Portals memory leaked: %d bytes\n",
458                        cfs_atomic_read(&libcfs_kmemory));
459
460         rc = libcfs_debug_cleanup();
461         if (rc)
462                 printk(CFS_KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
463                        rc);
464
465         cfs_fini_rwsem(&ioctl_list_sem);
466         cfs_fini_rwsem(&cfs_tracefile_sem);
467
468         libcfs_arch_cleanup();
469 }
470
471 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);