Whamcloud - gitweb
LU-2446 build: Update Whamcloud copyright messages for Intel
[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  * Copyright (c) 2012, Intel Corporation.
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 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <libcfs/libcfs.h>
40 #include <libcfs/libcfs_crypto.h>
41 #include <lnet/lib-lnet.h>
42 #include <lnet/lnet.h>
43 #include "tracefile.h"
44
45 void
46 kportal_memhog_free (struct libcfs_device_userstate *ldu)
47 {
48         cfs_page_t **level0p = &ldu->ldu_memhog_root_page;
49         cfs_page_t **level1p;
50         cfs_page_t **level2p;
51         int           count1;
52         int           count2;
53
54         if (*level0p != NULL) {
55
56                 level1p = (cfs_page_t **)cfs_page_address(*level0p);
57                 count1 = 0;
58
59                 while (count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
60                        *level1p != NULL) {
61
62                         level2p = (cfs_page_t **)cfs_page_address(*level1p);
63                         count2 = 0;
64
65                         while (count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
66                                *level2p != NULL) {
67
68                                 cfs_free_page(*level2p);
69                                 ldu->ldu_memhog_pages--;
70                                 level2p++;
71                                 count2++;
72                         }
73
74                         cfs_free_page(*level1p);
75                         ldu->ldu_memhog_pages--;
76                         level1p++;
77                         count1++;
78                 }
79
80                 cfs_free_page(*level0p);
81                 ldu->ldu_memhog_pages--;
82
83                 *level0p = NULL;
84         }
85
86         LASSERT (ldu->ldu_memhog_pages == 0);
87 }
88
89 int
90 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
91 {
92         cfs_page_t **level0p;
93         cfs_page_t **level1p;
94         cfs_page_t **level2p;
95         int           count1;
96         int           count2;
97
98         LASSERT (ldu->ldu_memhog_pages == 0);
99         LASSERT (ldu->ldu_memhog_root_page == NULL);
100
101         if (npages < 0)
102                 return -EINVAL;
103
104         if (npages == 0)
105                 return 0;
106
107         level0p = &ldu->ldu_memhog_root_page;
108         *level0p = cfs_alloc_page(flags);
109         if (*level0p == NULL)
110                 return -ENOMEM;
111         ldu->ldu_memhog_pages++;
112
113         level1p = (cfs_page_t **)cfs_page_address(*level0p);
114         count1 = 0;
115         memset(level1p, 0, CFS_PAGE_SIZE);
116
117         while (ldu->ldu_memhog_pages < npages &&
118                count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
119
120                 if (cfs_signal_pending())
121                         return (-EINTR);
122
123                 *level1p = cfs_alloc_page(flags);
124                 if (*level1p == NULL)
125                         return -ENOMEM;
126                 ldu->ldu_memhog_pages++;
127
128                 level2p = (cfs_page_t **)cfs_page_address(*level1p);
129                 count2 = 0;
130                 memset(level2p, 0, CFS_PAGE_SIZE);
131
132                 while (ldu->ldu_memhog_pages < npages &&
133                        count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
134
135                         if (cfs_signal_pending())
136                                 return (-EINTR);
137
138                         *level2p = cfs_alloc_page(flags);
139                         if (*level2p == NULL)
140                                 return (-ENOMEM);
141                         ldu->ldu_memhog_pages++;
142
143                         level2p++;
144                         count2++;
145                 }
146
147                 level1p++;
148                 count1++;
149         }
150
151         return 0;
152 }
153
154 /* called when opening /dev/device */
155 static int libcfs_psdev_open(unsigned long flags, void *args)
156 {
157         struct libcfs_device_userstate *ldu;
158         ENTRY;
159
160         PORTAL_MODULE_USE;
161
162         LIBCFS_ALLOC(ldu, sizeof(*ldu));
163         if (ldu != NULL) {
164                 ldu->ldu_memhog_pages = 0;
165                 ldu->ldu_memhog_root_page = NULL;
166         }
167         *(struct libcfs_device_userstate **)args = ldu;
168
169         RETURN(0);
170 }
171
172 /* called when closing /dev/device */
173 static int libcfs_psdev_release(unsigned long flags, void *args)
174 {
175         struct libcfs_device_userstate *ldu;
176         ENTRY;
177
178         ldu = (struct libcfs_device_userstate *)args;
179         if (ldu != NULL) {
180                 kportal_memhog_free(ldu);
181                 LIBCFS_FREE(ldu, sizeof(*ldu));
182         }
183
184         PORTAL_MODULE_UNUSE;
185         RETURN(0);
186 }
187
188 static struct rw_semaphore ioctl_list_sem;
189 static cfs_list_t ioctl_list;
190
191 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
192 {
193         int rc = 0;
194
195         down_write(&ioctl_list_sem);
196         if (!cfs_list_empty(&hand->item))
197                 rc = -EBUSY;
198         else
199                 cfs_list_add_tail(&hand->item, &ioctl_list);
200         up_write(&ioctl_list_sem);
201
202         return rc;
203 }
204 EXPORT_SYMBOL(libcfs_register_ioctl);
205
206 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
207 {
208         int rc = 0;
209
210         down_write(&ioctl_list_sem);
211         if (cfs_list_empty(&hand->item))
212                 rc = -ENOENT;
213         else
214                 cfs_list_del_init(&hand->item);
215         up_write(&ioctl_list_sem);
216
217         return rc;
218 }
219 EXPORT_SYMBOL(libcfs_deregister_ioctl);
220
221 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile,unsigned long cmd,
222                             void *arg, struct libcfs_ioctl_data *data)
223 {
224         int err = -EINVAL;
225         ENTRY;
226
227         switch (cmd) {
228         case IOC_LIBCFS_CLEAR_DEBUG:
229                 libcfs_debug_clear_buffer();
230                 RETURN(0);
231         /*
232          * case IOC_LIBCFS_PANIC:
233          * Handled in arch/cfs_module.c
234          */
235         case IOC_LIBCFS_MARK_DEBUG:
236                 if (data->ioc_inlbuf1 == NULL ||
237                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
238                         RETURN(-EINVAL);
239                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
240                 RETURN(0);
241 #if LWT_SUPPORT
242         case IOC_LIBCFS_LWT_CONTROL:
243                 err = lwt_control ((data->ioc_flags & 1) != 0, 
244                                    (data->ioc_flags & 2) != 0);
245                 break;
246
247         case IOC_LIBCFS_LWT_SNAPSHOT: {
248                 cfs_cycles_t   now;
249                 int            ncpu;
250                 int            total_size;
251
252                 err = lwt_snapshot (&now, &ncpu, &total_size,
253                                     data->ioc_pbuf1, data->ioc_plen1);
254                 data->ioc_u64[0] = now;
255                 data->ioc_u32[0] = ncpu;
256                 data->ioc_u32[1] = total_size;
257
258                 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
259                 data->ioc_u32[2] = sizeof(lwt_event_t);
260                 data->ioc_u32[3] = offsetof(lwt_event_t, lwte_where);
261
262                 if (err == 0 &&
263                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
264                         err = -EFAULT;
265                 break;
266         }
267
268         case IOC_LIBCFS_LWT_LOOKUP_STRING:
269                 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
270                                          data->ioc_pbuf2, data->ioc_plen2);
271                 if (err == 0 &&
272                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
273                         err = -EFAULT;
274                 break;
275 #endif
276         case IOC_LIBCFS_MEMHOG:
277                 if (pfile->private_data == NULL) {
278                         err = -EINVAL;
279                 } else {
280                         kportal_memhog_free(pfile->private_data);
281                         /* XXX The ioc_flags is not GFP flags now, need to be fixed */
282                         err = kportal_memhog_alloc(pfile->private_data,
283                                                    data->ioc_count,
284                                                    data->ioc_flags);
285                         if (err != 0)
286                                 kportal_memhog_free(pfile->private_data);
287                 }
288                 break;
289
290         case IOC_LIBCFS_PING_TEST: {
291                 extern void (kping_client)(struct libcfs_ioctl_data *);
292                 void (*ping)(struct libcfs_ioctl_data *);
293
294                 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
295                        data->ioc_count, libcfs_nid2str(data->ioc_nid),
296                        libcfs_nid2str(data->ioc_nid));
297                 ping = PORTAL_SYMBOL_GET(kping_client);
298                 if (!ping)
299                         CERROR("PORTAL_SYMBOL_GET failed\n");
300                 else {
301                         ping(data);
302                         PORTAL_SYMBOL_PUT(kping_client);
303                 }
304                 RETURN(0);
305         }
306
307         default: {
308                 struct libcfs_ioctl_handler *hand;
309                 err = -EINVAL;
310                 down_read(&ioctl_list_sem);
311                 cfs_list_for_each_entry_typed(hand, &ioctl_list,
312                         struct libcfs_ioctl_handler, item) {
313                         err = hand->handle_ioctl(cmd, data);
314                         if (err != -EINVAL) {
315                                 if (err == 0)
316                                         err = libcfs_ioctl_popdata(arg, 
317                                                         data, sizeof (*data));
318                                 break;
319                         }
320                 }
321                 up_read(&ioctl_list_sem);
322                 break;
323         }
324         }
325
326         RETURN(err);
327 }
328
329 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
330 {
331         char    *buf;
332         struct libcfs_ioctl_data *data;
333         int err = 0;
334         ENTRY;
335
336         LIBCFS_ALLOC_GFP(buf, 1024, CFS_ALLOC_STD);
337         if (buf == NULL)
338                 RETURN(-ENOMEM);
339
340         /* 'cmd' and permissions get checked in our arch-specific caller */
341         if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
342                 CERROR("PORTALS ioctl: data error\n");
343                 GOTO(out, err = -EINVAL);
344         }
345         data = (struct libcfs_ioctl_data *)buf;
346
347         err = libcfs_ioctl_int(pfile, cmd, arg, data);
348
349 out:
350         LIBCFS_FREE(buf, 1024);
351         RETURN(err);
352 }
353
354
355 struct cfs_psdev_ops libcfs_psdev_ops = {
356         libcfs_psdev_open,
357         libcfs_psdev_release,
358         NULL,
359         NULL,
360         libcfs_ioctl
361 };
362
363 extern int insert_proc(void);
364 extern void remove_proc(void);
365 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
366 MODULE_DESCRIPTION("Portals v3.1");
367 MODULE_LICENSE("GPL");
368
369 extern cfs_psdev_t libcfs_dev;
370 extern struct rw_semaphore cfs_tracefile_sem;
371 extern struct mutex cfs_trace_thread_mutex;
372 extern struct cfs_wi_sched *cfs_sched_rehash;
373
374 extern void libcfs_init_nidstrings(void);
375 extern int libcfs_arch_init(void);
376 extern void libcfs_arch_cleanup(void);
377
378 static int init_libcfs_module(void)
379 {
380         int rc;
381
382         libcfs_arch_init();
383         libcfs_init_nidstrings();
384         init_rwsem(&cfs_tracefile_sem);
385         mutex_init(&cfs_trace_thread_mutex);
386         init_rwsem(&ioctl_list_sem);
387         CFS_INIT_LIST_HEAD(&ioctl_list);
388         cfs_waitq_init(&cfs_race_waitq);
389
390         rc = libcfs_debug_init(5 * 1024 * 1024);
391         if (rc < 0) {
392                 printk(CFS_KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
393                 return (rc);
394         }
395
396         rc = cfs_cpu_init();
397         if (rc != 0)
398                 goto cleanup_debug;
399
400 #if LWT_SUPPORT
401         rc = lwt_init();
402         if (rc != 0) {
403                 CERROR("lwt_init: error %d\n", rc);
404                 goto cleanup_debug;
405         }
406 #endif
407         rc = cfs_psdev_register(&libcfs_dev);
408         if (rc) {
409                 CERROR("misc_register: error %d\n", rc);
410                 goto cleanup_lwt;
411         }
412
413         rc = cfs_wi_startup();
414         if (rc) {
415                 CERROR("initialize workitem: error %d\n", rc);
416                 goto cleanup_deregister;
417         }
418
419         /* max to 4 threads, should be enough for rehash */
420         rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
421         rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
422                                  rc, &cfs_sched_rehash);
423         if (rc != 0) {
424                 CERROR("Startup workitem scheduler: error: %d\n", rc);
425                 goto cleanup_deregister;
426         }
427
428         rc = cfs_crypto_register();
429         if (rc) {
430                 CERROR("cfs_crypto_regster: error %d\n", rc);
431                 goto cleanup_wi;
432         }
433
434
435         rc = insert_proc();
436         if (rc) {
437                 CERROR("insert_proc: error %d\n", rc);
438                 goto cleanup_crypto;
439         }
440
441         CDEBUG (D_OTHER, "portals setup OK\n");
442         return 0;
443  cleanup_crypto:
444         cfs_crypto_unregister();
445  cleanup_wi:
446         cfs_wi_shutdown();
447  cleanup_deregister:
448         cfs_psdev_deregister(&libcfs_dev);
449  cleanup_lwt:
450 #if LWT_SUPPORT
451         lwt_fini();
452 #endif
453  cleanup_debug:
454         libcfs_debug_cleanup();
455         return rc;
456 }
457
458 static void exit_libcfs_module(void)
459 {
460         int rc;
461
462         remove_proc();
463
464         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
465                cfs_atomic_read(&libcfs_kmemory));
466
467         if (cfs_sched_rehash != NULL) {
468                 cfs_wi_sched_destroy(cfs_sched_rehash);
469                 cfs_sched_rehash = NULL;
470         }
471
472         cfs_crypto_unregister();
473         cfs_wi_shutdown();
474
475         rc = cfs_psdev_deregister(&libcfs_dev);
476         if (rc)
477                 CERROR("misc_deregister error %d\n", rc);
478
479 #if LWT_SUPPORT
480         lwt_fini();
481 #endif
482         cfs_cpu_fini();
483
484         if (cfs_atomic_read(&libcfs_kmemory) != 0)
485                 CERROR("Portals memory leaked: %d bytes\n",
486                        cfs_atomic_read(&libcfs_kmemory));
487
488         rc = libcfs_debug_cleanup();
489         if (rc)
490                 printk(CFS_KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
491                        rc);
492
493         fini_rwsem(&ioctl_list_sem);
494         fini_rwsem(&cfs_tracefile_sem);
495
496         libcfs_arch_cleanup();
497 }
498
499 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);