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 2008 Sun Microsystems, Inc. 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 struct rw_semaphore ioctl_list_sem;
191 static struct list_head ioctl_list;
193 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
197 down_write(&ioctl_list_sem);
198 if (!list_empty(&hand->item))
201 list_add_tail(&hand->item, &ioctl_list);
202 up_write(&ioctl_list_sem);
206 EXPORT_SYMBOL(libcfs_register_ioctl);
208 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
212 down_write(&ioctl_list_sem);
213 if (list_empty(&hand->item))
216 list_del_init(&hand->item);
217 up_write(&ioctl_list_sem);
221 EXPORT_SYMBOL(libcfs_deregister_ioctl);
223 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
227 struct libcfs_ioctl_data *data;
230 /* 'cmd' and permissions get checked in our arch-specific caller */
232 if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
233 CERROR("PORTALS ioctl: data error\n");
236 data = (struct libcfs_ioctl_data *)buf;
239 case IOC_LIBCFS_CLEAR_DEBUG:
240 libcfs_debug_clear_buffer();
243 * case IOC_LIBCFS_PANIC:
244 * Handled in arch/cfs_module.c
246 case IOC_LIBCFS_MARK_DEBUG:
247 if (data->ioc_inlbuf1 == NULL ||
248 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
250 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
253 case IOC_LIBCFS_LWT_CONTROL:
254 err = lwt_control ((data->ioc_flags & 1) != 0,
255 (data->ioc_flags & 2) != 0);
258 case IOC_LIBCFS_LWT_SNAPSHOT: {
263 err = lwt_snapshot (&now, &ncpu, &total_size,
264 data->ioc_pbuf1, data->ioc_plen1);
265 data->ioc_u64[0] = now;
266 data->ioc_u32[0] = ncpu;
267 data->ioc_u32[1] = total_size;
269 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
270 data->ioc_u32[2] = sizeof(lwt_event_t);
271 data->ioc_u32[3] = offsetof(lwt_event_t, lwte_where);
274 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
279 case IOC_LIBCFS_LWT_LOOKUP_STRING:
280 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
281 data->ioc_pbuf2, data->ioc_plen2);
283 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
287 case IOC_LIBCFS_MEMHOG:
288 if (pfile->private_data == NULL) {
291 kportal_memhog_free(pfile->private_data);
292 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
293 err = kportal_memhog_alloc(pfile->private_data,
297 kportal_memhog_free(pfile->private_data);
301 case IOC_LIBCFS_PING_TEST: {
302 extern void (kping_client)(struct libcfs_ioctl_data *);
303 void (*ping)(struct libcfs_ioctl_data *);
305 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
306 data->ioc_count, libcfs_nid2str(data->ioc_nid),
307 libcfs_nid2str(data->ioc_nid));
308 ping = PORTAL_SYMBOL_GET(kping_client);
310 CERROR("PORTAL_SYMBOL_GET failed\n");
313 PORTAL_SYMBOL_PUT(kping_client);
319 struct libcfs_ioctl_handler *hand;
321 down_read(&ioctl_list_sem);
322 cfs_list_for_each_entry_typed(hand, &ioctl_list,
323 struct libcfs_ioctl_handler, item) {
324 err = hand->handle_ioctl(cmd, data);
325 if (err != -EINVAL) {
327 err = libcfs_ioctl_popdata(arg,
328 data, sizeof (*data));
332 up_read(&ioctl_list_sem);
340 struct cfs_psdev_ops libcfs_psdev_ops = {
342 libcfs_psdev_release,
348 extern int insert_proc(void);
349 extern void remove_proc(void);
350 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
351 MODULE_DESCRIPTION("Portals v3.1");
352 MODULE_LICENSE("GPL");
354 extern cfs_psdev_t libcfs_dev;
355 extern struct rw_semaphore tracefile_sem;
356 extern struct semaphore trace_thread_sem;
358 extern void libcfs_init_nidstrings(void);
359 extern int libcfs_arch_init(void);
360 extern void libcfs_arch_cleanup(void);
362 static int init_libcfs_module(void)
367 libcfs_init_nidstrings();
368 init_rwsem(&tracefile_sem);
369 init_mutex(&trace_thread_sem);
370 init_rwsem(&ioctl_list_sem);
371 CFS_INIT_LIST_HEAD(&ioctl_list);
373 rc = libcfs_debug_init(5 * 1024 * 1024);
375 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
382 CERROR("lwt_init: error %d\n", rc);
386 rc = cfs_psdev_register(&libcfs_dev);
388 CERROR("misc_register: error %d\n", rc);
394 CERROR("insert_proc: error %d\n", rc);
395 goto cleanup_deregister;
398 CDEBUG (D_OTHER, "portals setup OK\n");
402 cfs_psdev_deregister(&libcfs_dev);
408 libcfs_debug_cleanup();
412 static void exit_libcfs_module(void)
418 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
419 atomic_read(&libcfs_kmemory));
421 rc = cfs_psdev_deregister(&libcfs_dev);
423 CERROR("misc_deregister error %d\n", rc);
429 if (atomic_read(&libcfs_kmemory) != 0)
430 CERROR("Portals memory leaked: %d bytes\n",
431 atomic_read(&libcfs_kmemory));
433 rc = libcfs_debug_cleanup();
435 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n", rc);
437 fini_rwsem(&ioctl_list_sem);
438 fini_rwsem(&tracefile_sem);
440 libcfs_arch_cleanup();
443 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);