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 <lnet/lib-lnet.h>
39 #include <lnet/lnet.h>
40 #include "tracefile.h"
43 kportal_memhog_free (struct libcfs_device_userstate *ldu)
45 cfs_page_t **level0p = &ldu->ldu_memhog_root_page;
51 if (*level0p != NULL) {
53 level1p = (cfs_page_t **)cfs_page_address(*level0p);
56 while (count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
59 level2p = (cfs_page_t **)cfs_page_address(*level1p);
62 while (count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
65 cfs_free_page(*level2p);
66 ldu->ldu_memhog_pages--;
71 cfs_free_page(*level1p);
72 ldu->ldu_memhog_pages--;
77 cfs_free_page(*level0p);
78 ldu->ldu_memhog_pages--;
83 LASSERT (ldu->ldu_memhog_pages == 0);
87 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
95 LASSERT (ldu->ldu_memhog_pages == 0);
96 LASSERT (ldu->ldu_memhog_root_page == NULL);
104 level0p = &ldu->ldu_memhog_root_page;
105 *level0p = cfs_alloc_page(flags);
106 if (*level0p == NULL)
108 ldu->ldu_memhog_pages++;
110 level1p = (cfs_page_t **)cfs_page_address(*level0p);
112 memset(level1p, 0, CFS_PAGE_SIZE);
114 while (ldu->ldu_memhog_pages < npages &&
115 count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
117 if (cfs_signal_pending())
120 *level1p = cfs_alloc_page(flags);
121 if (*level1p == NULL)
123 ldu->ldu_memhog_pages++;
125 level2p = (cfs_page_t **)cfs_page_address(*level1p);
127 memset(level2p, 0, CFS_PAGE_SIZE);
129 while (ldu->ldu_memhog_pages < npages &&
130 count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
132 if (cfs_signal_pending())
135 *level2p = cfs_alloc_page(flags);
136 if (*level2p == NULL)
138 ldu->ldu_memhog_pages++;
151 /* called when opening /dev/device */
152 static int libcfs_psdev_open(unsigned long flags, void *args)
154 struct libcfs_device_userstate *ldu;
159 LIBCFS_ALLOC(ldu, sizeof(*ldu));
161 ldu->ldu_memhog_pages = 0;
162 ldu->ldu_memhog_root_page = NULL;
164 *(struct libcfs_device_userstate **)args = ldu;
169 /* called when closing /dev/device */
170 static int libcfs_psdev_release(unsigned long flags, void *args)
172 struct libcfs_device_userstate *ldu;
175 ldu = (struct libcfs_device_userstate *)args;
177 kportal_memhog_free(ldu);
178 LIBCFS_FREE(ldu, sizeof(*ldu));
185 static cfs_rw_semaphore_t ioctl_list_sem;
186 static cfs_list_t ioctl_list;
188 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
192 cfs_down_write(&ioctl_list_sem);
193 if (!cfs_list_empty(&hand->item))
196 cfs_list_add_tail(&hand->item, &ioctl_list);
197 cfs_up_write(&ioctl_list_sem);
201 EXPORT_SYMBOL(libcfs_register_ioctl);
203 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
207 cfs_down_write(&ioctl_list_sem);
208 if (cfs_list_empty(&hand->item))
211 cfs_list_del_init(&hand->item);
212 cfs_up_write(&ioctl_list_sem);
216 EXPORT_SYMBOL(libcfs_deregister_ioctl);
218 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile,unsigned long cmd,
219 void *arg, struct libcfs_ioctl_data *data)
225 case IOC_LIBCFS_CLEAR_DEBUG:
226 libcfs_debug_clear_buffer();
229 * case IOC_LIBCFS_PANIC:
230 * Handled in arch/cfs_module.c
232 case IOC_LIBCFS_MARK_DEBUG:
233 if (data->ioc_inlbuf1 == NULL ||
234 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
236 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
239 case IOC_LIBCFS_LWT_CONTROL:
240 err = lwt_control ((data->ioc_flags & 1) != 0,
241 (data->ioc_flags & 2) != 0);
244 case IOC_LIBCFS_LWT_SNAPSHOT: {
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;
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);
260 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
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);
269 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
273 case IOC_LIBCFS_MEMHOG:
274 if (pfile->private_data == NULL) {
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,
283 kportal_memhog_free(pfile->private_data);
287 case IOC_LIBCFS_PING_TEST: {
288 extern void (kping_client)(struct libcfs_ioctl_data *);
289 void (*ping)(struct libcfs_ioctl_data *);
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);
296 CERROR("PORTAL_SYMBOL_GET failed\n");
299 PORTAL_SYMBOL_PUT(kping_client);
305 struct libcfs_ioctl_handler *hand;
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) {
313 err = libcfs_ioctl_popdata(arg,
314 data, sizeof (*data));
318 cfs_up_read(&ioctl_list_sem);
326 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
329 struct libcfs_ioctl_data *data;
333 LIBCFS_ALLOC_GFP(buf, 1024, CFS_ALLOC_STD);
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);
342 data = (struct libcfs_ioctl_data *)buf;
344 err = libcfs_ioctl_int(pfile, cmd, arg, data);
347 LIBCFS_FREE(buf, 1024);
352 struct cfs_psdev_ops libcfs_psdev_ops = {
354 libcfs_psdev_release,
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");
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;
370 extern void libcfs_init_nidstrings(void);
371 extern int libcfs_arch_init(void);
372 extern void libcfs_arch_cleanup(void);
374 static int init_libcfs_module(void)
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);
386 rc = libcfs_debug_init(5 * 1024 * 1024);
388 printk(CFS_KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
399 CERROR("lwt_init: error %d\n", rc);
403 rc = cfs_psdev_register(&libcfs_dev);
405 CERROR("misc_register: error %d\n", rc);
409 rc = cfs_wi_startup();
411 CERROR("startup workitem: error %d\n", rc);
412 goto cleanup_deregister;
417 CERROR("insert_proc: error %d\n", rc);
421 CDEBUG (D_OTHER, "portals setup OK\n");
427 cfs_psdev_deregister(&libcfs_dev);
433 libcfs_debug_cleanup();
437 static void exit_libcfs_module(void)
443 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
444 cfs_atomic_read(&libcfs_kmemory));
447 rc = cfs_psdev_deregister(&libcfs_dev);
449 CERROR("misc_deregister error %d\n", rc);
456 if (cfs_atomic_read(&libcfs_kmemory) != 0)
457 CERROR("Portals memory leaked: %d bytes\n",
458 cfs_atomic_read(&libcfs_kmemory));
460 rc = libcfs_debug_cleanup();
462 printk(CFS_KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
465 cfs_fini_rwsem(&ioctl_list_sem);
466 cfs_fini_rwsem(&cfs_tracefile_sem);
468 libcfs_arch_cleanup();
471 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);