4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
35 #define DEBUG_SUBSYSTEM S_LNET
37 #include <libcfs/libcfs.h>
38 #include <libcfs/libcfs_crypto.h>
39 #include <lnet/lib-lnet.h>
40 #include <lnet/lnet.h>
41 #include "tracefile.h"
44 kportal_memhog_free (struct libcfs_device_userstate *ldu)
46 cfs_page_t **level0p = &ldu->ldu_memhog_root_page;
52 if (*level0p != NULL) {
54 level1p = (cfs_page_t **)cfs_page_address(*level0p);
57 while (count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
60 level2p = (cfs_page_t **)cfs_page_address(*level1p);
63 while (count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
66 cfs_free_page(*level2p);
67 ldu->ldu_memhog_pages--;
72 cfs_free_page(*level1p);
73 ldu->ldu_memhog_pages--;
78 cfs_free_page(*level0p);
79 ldu->ldu_memhog_pages--;
84 LASSERT (ldu->ldu_memhog_pages == 0);
88 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
96 LASSERT (ldu->ldu_memhog_pages == 0);
97 LASSERT (ldu->ldu_memhog_root_page == NULL);
105 level0p = &ldu->ldu_memhog_root_page;
106 *level0p = cfs_alloc_page(flags);
107 if (*level0p == NULL)
109 ldu->ldu_memhog_pages++;
111 level1p = (cfs_page_t **)cfs_page_address(*level0p);
113 memset(level1p, 0, CFS_PAGE_SIZE);
115 while (ldu->ldu_memhog_pages < npages &&
116 count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
118 if (cfs_signal_pending())
121 *level1p = cfs_alloc_page(flags);
122 if (*level1p == NULL)
124 ldu->ldu_memhog_pages++;
126 level2p = (cfs_page_t **)cfs_page_address(*level1p);
128 memset(level2p, 0, CFS_PAGE_SIZE);
130 while (ldu->ldu_memhog_pages < npages &&
131 count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
133 if (cfs_signal_pending())
136 *level2p = cfs_alloc_page(flags);
137 if (*level2p == NULL)
139 ldu->ldu_memhog_pages++;
152 /* called when opening /dev/device */
153 static int libcfs_psdev_open(unsigned long flags, void *args)
155 struct libcfs_device_userstate *ldu;
160 LIBCFS_ALLOC(ldu, sizeof(*ldu));
162 ldu->ldu_memhog_pages = 0;
163 ldu->ldu_memhog_root_page = NULL;
165 *(struct libcfs_device_userstate **)args = ldu;
170 /* called when closing /dev/device */
171 static int libcfs_psdev_release(unsigned long flags, void *args)
173 struct libcfs_device_userstate *ldu;
176 ldu = (struct libcfs_device_userstate *)args;
178 kportal_memhog_free(ldu);
179 LIBCFS_FREE(ldu, sizeof(*ldu));
186 static cfs_rw_semaphore_t ioctl_list_sem;
187 static cfs_list_t ioctl_list;
189 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
193 cfs_down_write(&ioctl_list_sem);
194 if (!cfs_list_empty(&hand->item))
197 cfs_list_add_tail(&hand->item, &ioctl_list);
198 cfs_up_write(&ioctl_list_sem);
202 EXPORT_SYMBOL(libcfs_register_ioctl);
204 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
208 cfs_down_write(&ioctl_list_sem);
209 if (cfs_list_empty(&hand->item))
212 cfs_list_del_init(&hand->item);
213 cfs_up_write(&ioctl_list_sem);
217 EXPORT_SYMBOL(libcfs_deregister_ioctl);
219 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile,unsigned long cmd,
220 void *arg, struct libcfs_ioctl_data *data)
226 case IOC_LIBCFS_CLEAR_DEBUG:
227 libcfs_debug_clear_buffer();
230 * case IOC_LIBCFS_PANIC:
231 * Handled in arch/cfs_module.c
233 case IOC_LIBCFS_MARK_DEBUG:
234 if (data->ioc_inlbuf1 == NULL ||
235 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
237 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
240 case IOC_LIBCFS_LWT_CONTROL:
241 err = lwt_control ((data->ioc_flags & 1) != 0,
242 (data->ioc_flags & 2) != 0);
245 case IOC_LIBCFS_LWT_SNAPSHOT: {
250 err = lwt_snapshot (&now, &ncpu, &total_size,
251 data->ioc_pbuf1, data->ioc_plen1);
252 data->ioc_u64[0] = now;
253 data->ioc_u32[0] = ncpu;
254 data->ioc_u32[1] = total_size;
256 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
257 data->ioc_u32[2] = sizeof(lwt_event_t);
258 data->ioc_u32[3] = offsetof(lwt_event_t, lwte_where);
261 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
266 case IOC_LIBCFS_LWT_LOOKUP_STRING:
267 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
268 data->ioc_pbuf2, data->ioc_plen2);
270 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
274 case IOC_LIBCFS_MEMHOG:
275 if (pfile->private_data == NULL) {
278 kportal_memhog_free(pfile->private_data);
279 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
280 err = kportal_memhog_alloc(pfile->private_data,
284 kportal_memhog_free(pfile->private_data);
288 case IOC_LIBCFS_PING_TEST: {
289 extern void (kping_client)(struct libcfs_ioctl_data *);
290 void (*ping)(struct libcfs_ioctl_data *);
292 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
293 data->ioc_count, libcfs_nid2str(data->ioc_nid),
294 libcfs_nid2str(data->ioc_nid));
295 ping = PORTAL_SYMBOL_GET(kping_client);
297 CERROR("PORTAL_SYMBOL_GET failed\n");
300 PORTAL_SYMBOL_PUT(kping_client);
306 struct libcfs_ioctl_handler *hand;
308 cfs_down_read(&ioctl_list_sem);
309 cfs_list_for_each_entry_typed(hand, &ioctl_list,
310 struct libcfs_ioctl_handler, item) {
311 err = hand->handle_ioctl(cmd, data);
312 if (err != -EINVAL) {
314 err = libcfs_ioctl_popdata(arg,
315 data, sizeof (*data));
319 cfs_up_read(&ioctl_list_sem);
327 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
330 struct libcfs_ioctl_data *data;
334 LIBCFS_ALLOC_GFP(buf, 1024, CFS_ALLOC_STD);
338 /* 'cmd' and permissions get checked in our arch-specific caller */
339 if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
340 CERROR("PORTALS ioctl: data error\n");
341 GOTO(out, err = -EINVAL);
343 data = (struct libcfs_ioctl_data *)buf;
345 err = libcfs_ioctl_int(pfile, cmd, arg, data);
348 LIBCFS_FREE(buf, 1024);
353 struct cfs_psdev_ops libcfs_psdev_ops = {
355 libcfs_psdev_release,
361 extern int insert_proc(void);
362 extern void remove_proc(void);
363 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
364 MODULE_DESCRIPTION("Portals v3.1");
365 MODULE_LICENSE("GPL");
367 extern cfs_psdev_t libcfs_dev;
368 extern cfs_rw_semaphore_t cfs_tracefile_sem;
369 extern cfs_mutex_t cfs_trace_thread_mutex;
370 extern struct cfs_wi_sched *cfs_sched_rehash;
372 extern void libcfs_init_nidstrings(void);
373 extern int libcfs_arch_init(void);
374 extern void libcfs_arch_cleanup(void);
376 static int init_libcfs_module(void)
381 libcfs_init_nidstrings();
382 cfs_init_rwsem(&cfs_tracefile_sem);
383 cfs_mutex_init(&cfs_trace_thread_mutex);
384 cfs_init_rwsem(&ioctl_list_sem);
385 CFS_INIT_LIST_HEAD(&ioctl_list);
386 cfs_waitq_init(&cfs_race_waitq);
388 rc = libcfs_debug_init(5 * 1024 * 1024);
390 printk(CFS_KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
401 CERROR("lwt_init: error %d\n", rc);
405 rc = cfs_psdev_register(&libcfs_dev);
407 CERROR("misc_register: error %d\n", rc);
411 rc = cfs_wi_startup();
413 CERROR("initialize workitem: error %d\n", rc);
414 goto cleanup_deregister;
417 /* max to 4 threads, should be enough for rehash */
418 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
419 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
420 rc, &cfs_sched_rehash);
422 CERROR("Startup workitem scheduler: error: %d\n", rc);
423 goto cleanup_deregister;
426 rc = cfs_crypto_register();
428 CERROR("cfs_crypto_regster: error %d\n", rc);
435 CERROR("insert_proc: error %d\n", rc);
439 CDEBUG (D_OTHER, "portals setup OK\n");
442 cfs_crypto_unregister();
446 cfs_psdev_deregister(&libcfs_dev);
452 libcfs_debug_cleanup();
456 static void exit_libcfs_module(void)
462 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
463 cfs_atomic_read(&libcfs_kmemory));
465 if (cfs_sched_rehash != NULL) {
466 cfs_wi_sched_destroy(cfs_sched_rehash);
467 cfs_sched_rehash = NULL;
470 cfs_crypto_unregister();
473 rc = cfs_psdev_deregister(&libcfs_dev);
475 CERROR("misc_deregister error %d\n", rc);
482 if (cfs_atomic_read(&libcfs_kmemory) != 0)
483 CERROR("Portals memory leaked: %d bytes\n",
484 cfs_atomic_read(&libcfs_kmemory));
486 rc = libcfs_debug_cleanup();
488 printk(CFS_KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
491 cfs_fini_rwsem(&ioctl_list_sem);
492 cfs_fini_rwsem(&cfs_tracefile_sem);
494 libcfs_arch_cleanup();
497 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);