1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_LNET
42 #include <libcfs/libcfs.h>
43 #include <lnet/lib-lnet.h>
44 #include <lnet/lnet.h>
45 #include "tracefile.h"
48 kportal_memhog_free (struct libcfs_device_userstate *ldu)
50 cfs_page_t **level0p = &ldu->ldu_memhog_root_page;
56 if (*level0p != NULL) {
58 level1p = (cfs_page_t **)cfs_page_address(*level0p);
61 while (count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
64 level2p = (cfs_page_t **)cfs_page_address(*level1p);
67 while (count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
70 cfs_free_page(*level2p);
71 ldu->ldu_memhog_pages--;
76 cfs_free_page(*level1p);
77 ldu->ldu_memhog_pages--;
82 cfs_free_page(*level0p);
83 ldu->ldu_memhog_pages--;
88 LASSERT (ldu->ldu_memhog_pages == 0);
92 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
100 LASSERT (ldu->ldu_memhog_pages == 0);
101 LASSERT (ldu->ldu_memhog_root_page == NULL);
109 level0p = &ldu->ldu_memhog_root_page;
110 *level0p = cfs_alloc_page(flags);
111 if (*level0p == NULL)
113 ldu->ldu_memhog_pages++;
115 level1p = (cfs_page_t **)cfs_page_address(*level0p);
117 memset(level1p, 0, CFS_PAGE_SIZE);
119 while (ldu->ldu_memhog_pages < npages &&
120 count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
122 if (cfs_signal_pending())
125 *level1p = cfs_alloc_page(flags);
126 if (*level1p == NULL)
128 ldu->ldu_memhog_pages++;
130 level2p = (cfs_page_t **)cfs_page_address(*level1p);
132 memset(level2p, 0, CFS_PAGE_SIZE);
134 while (ldu->ldu_memhog_pages < npages &&
135 count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
137 if (cfs_signal_pending())
140 *level2p = cfs_alloc_page(flags);
141 if (*level2p == NULL)
143 ldu->ldu_memhog_pages++;
156 /* called when opening /dev/device */
157 static int libcfs_psdev_open(unsigned long flags, void *args)
159 struct libcfs_device_userstate *ldu;
164 LIBCFS_ALLOC(ldu, sizeof(*ldu));
166 ldu->ldu_memhog_pages = 0;
167 ldu->ldu_memhog_root_page = NULL;
169 *(struct libcfs_device_userstate **)args = ldu;
174 /* called when closing /dev/device */
175 static int libcfs_psdev_release(unsigned long flags, void *args)
177 struct libcfs_device_userstate *ldu;
180 ldu = (struct libcfs_device_userstate *)args;
182 kportal_memhog_free(ldu);
183 LIBCFS_FREE(ldu, sizeof(*ldu));
190 static cfs_rw_semaphore_t ioctl_list_sem;
191 static cfs_list_t ioctl_list;
193 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
197 cfs_down_write(&ioctl_list_sem);
198 if (!cfs_list_empty(&hand->item))
201 cfs_list_add_tail(&hand->item, &ioctl_list);
202 cfs_up_write(&ioctl_list_sem);
206 EXPORT_SYMBOL(libcfs_register_ioctl);
208 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
212 cfs_down_write(&ioctl_list_sem);
213 if (cfs_list_empty(&hand->item))
216 cfs_list_del_init(&hand->item);
217 cfs_up_write(&ioctl_list_sem);
221 EXPORT_SYMBOL(libcfs_deregister_ioctl);
223 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile,unsigned long cmd,
224 void *arg, struct libcfs_ioctl_data *data)
230 case IOC_LIBCFS_CLEAR_DEBUG:
231 libcfs_debug_clear_buffer();
234 * case IOC_LIBCFS_PANIC:
235 * Handled in arch/cfs_module.c
237 case IOC_LIBCFS_MARK_DEBUG:
238 if (data->ioc_inlbuf1 == NULL ||
239 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
241 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
244 case IOC_LIBCFS_LWT_CONTROL:
245 err = lwt_control ((data->ioc_flags & 1) != 0,
246 (data->ioc_flags & 2) != 0);
249 case IOC_LIBCFS_LWT_SNAPSHOT: {
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;
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);
265 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
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);
274 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
278 case IOC_LIBCFS_MEMHOG:
279 if (pfile->private_data == NULL) {
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,
288 kportal_memhog_free(pfile->private_data);
292 case IOC_LIBCFS_PING_TEST: {
293 extern void (kping_client)(struct libcfs_ioctl_data *);
294 void (*ping)(struct libcfs_ioctl_data *);
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);
301 CERROR("PORTAL_SYMBOL_GET failed\n");
304 PORTAL_SYMBOL_PUT(kping_client);
310 struct libcfs_ioctl_handler *hand;
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) {
318 err = libcfs_ioctl_popdata(arg,
319 data, sizeof (*data));
323 cfs_up_read(&ioctl_list_sem);
331 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
334 struct libcfs_ioctl_data *data;
338 LIBCFS_ALLOC_GFP(buf, 1024, CFS_ALLOC_STD);
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);
347 data = (struct libcfs_ioctl_data *)buf;
349 err = libcfs_ioctl_int(pfile, cmd, arg, data);
352 LIBCFS_FREE(buf, 1024);
357 struct cfs_psdev_ops libcfs_psdev_ops = {
359 libcfs_psdev_release,
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");
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;
375 extern void libcfs_init_nidstrings(void);
376 extern int libcfs_arch_init(void);
377 extern void libcfs_arch_cleanup(void);
379 static int init_libcfs_module(void)
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);
391 rc = libcfs_debug_init(5 * 1024 * 1024);
393 printk(CFS_KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
400 CERROR("lwt_init: error %d\n", rc);
404 rc = cfs_psdev_register(&libcfs_dev);
406 CERROR("misc_register: error %d\n", rc);
410 rc = cfs_wi_startup();
412 CERROR("startup workitem: error %d\n", rc);
413 goto cleanup_deregister;
418 CERROR("insert_proc: error %d\n", rc);
422 CDEBUG (D_OTHER, "portals setup OK\n");
428 cfs_psdev_deregister(&libcfs_dev);
434 libcfs_debug_cleanup();
438 static void exit_libcfs_module(void)
444 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
445 cfs_atomic_read(&libcfs_kmemory));
448 rc = cfs_psdev_deregister(&libcfs_dev);
450 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);