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