1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_LNET
27 #include <lnet/lib-lnet.h>
28 #include <lnet/lnet.h>
29 #include <libcfs/kp30.h>
30 #include "tracefile.h"
33 kportal_memhog_free (struct libcfs_device_userstate *ldu)
35 cfs_page_t **level0p = &ldu->ldu_memhog_root_page;
41 if (*level0p != NULL) {
43 level1p = (cfs_page_t **)cfs_page_address(*level0p);
46 while (count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
49 level2p = (cfs_page_t **)cfs_page_address(*level1p);
52 while (count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
55 cfs_free_page(*level2p);
56 ldu->ldu_memhog_pages--;
61 cfs_free_page(*level1p);
62 ldu->ldu_memhog_pages--;
67 cfs_free_page(*level0p);
68 ldu->ldu_memhog_pages--;
73 LASSERT (ldu->ldu_memhog_pages == 0);
77 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
85 LASSERT (ldu->ldu_memhog_pages == 0);
86 LASSERT (ldu->ldu_memhog_root_page == NULL);
94 level0p = &ldu->ldu_memhog_root_page;
95 *level0p = cfs_alloc_page(flags);
98 ldu->ldu_memhog_pages++;
100 level1p = (cfs_page_t **)cfs_page_address(*level0p);
102 memset(level1p, 0, CFS_PAGE_SIZE);
104 while (ldu->ldu_memhog_pages < npages &&
105 count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
107 if (cfs_signal_pending())
110 *level1p = cfs_alloc_page(flags);
111 if (*level1p == NULL)
113 ldu->ldu_memhog_pages++;
115 level2p = (cfs_page_t **)cfs_page_address(*level1p);
117 memset(level2p, 0, CFS_PAGE_SIZE);
119 while (ldu->ldu_memhog_pages < npages &&
120 count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
122 if (cfs_signal_pending())
125 *level2p = cfs_alloc_page(flags);
126 if (*level2p == NULL)
128 ldu->ldu_memhog_pages++;
141 /* called when opening /dev/device */
142 static int libcfs_psdev_open(unsigned long flags, void *args)
144 struct libcfs_device_userstate *ldu;
149 LIBCFS_ALLOC(ldu, sizeof(*ldu));
151 ldu->ldu_memhog_pages = 0;
152 ldu->ldu_memhog_root_page = NULL;
154 *(struct libcfs_device_userstate **)args = ldu;
159 /* called when closing /dev/device */
160 static int libcfs_psdev_release(unsigned long flags, void *args)
162 struct libcfs_device_userstate *ldu;
165 ldu = (struct libcfs_device_userstate *)args;
167 kportal_memhog_free(ldu);
168 LIBCFS_FREE(ldu, sizeof(*ldu));
175 static struct rw_semaphore ioctl_list_sem;
176 static struct list_head ioctl_list;
178 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
182 down_write(&ioctl_list_sem);
183 if (!list_empty(&hand->item))
186 list_add_tail(&hand->item, &ioctl_list);
187 up_write(&ioctl_list_sem);
191 EXPORT_SYMBOL(libcfs_register_ioctl);
193 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
197 down_write(&ioctl_list_sem);
198 if (list_empty(&hand->item))
201 list_del_init(&hand->item);
202 up_write(&ioctl_list_sem);
206 EXPORT_SYMBOL(libcfs_deregister_ioctl);
208 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
212 struct libcfs_ioctl_data *data;
215 /* 'cmd' and permissions get checked in our arch-specific caller */
217 if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
218 CERROR("PORTALS ioctl: data error\n");
221 data = (struct libcfs_ioctl_data *)buf;
224 case IOC_LIBCFS_CLEAR_DEBUG:
225 libcfs_debug_clear_buffer();
228 * case IOC_LIBCFS_PANIC:
229 * Handled in arch/cfs_module.c
231 case IOC_LIBCFS_MARK_DEBUG:
232 if (data->ioc_inlbuf1 == NULL ||
233 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
235 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
238 case IOC_LIBCFS_LWT_CONTROL:
239 err = lwt_control ((data->ioc_flags & 1) != 0,
240 (data->ioc_flags & 2) != 0);
243 case IOC_LIBCFS_LWT_SNAPSHOT: {
248 err = lwt_snapshot (&now, &ncpu, &total_size,
249 data->ioc_pbuf1, data->ioc_plen1);
250 data->ioc_u64[0] = now;
251 data->ioc_u32[0] = ncpu;
252 data->ioc_u32[1] = total_size;
254 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
255 data->ioc_u32[2] = sizeof(lwt_event_t);
256 data->ioc_u32[3] = offsetof(lwt_event_t, lwte_where);
259 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
264 case IOC_LIBCFS_LWT_LOOKUP_STRING:
265 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
266 data->ioc_pbuf2, data->ioc_plen2);
268 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
272 case IOC_LIBCFS_MEMHOG:
273 if (pfile->private_data == NULL) {
276 kportal_memhog_free(pfile->private_data);
277 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
278 err = kportal_memhog_alloc(pfile->private_data,
282 kportal_memhog_free(pfile->private_data);
286 case IOC_LIBCFS_PING_TEST: {
287 extern void (kping_client)(struct libcfs_ioctl_data *);
288 void (*ping)(struct libcfs_ioctl_data *);
290 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
291 data->ioc_count, libcfs_nid2str(data->ioc_nid),
292 libcfs_nid2str(data->ioc_nid));
293 ping = PORTAL_SYMBOL_GET(kping_client);
295 CERROR("PORTAL_SYMBOL_GET failed\n");
298 PORTAL_SYMBOL_PUT(kping_client);
304 struct libcfs_ioctl_handler *hand;
306 down_read(&ioctl_list_sem);
307 list_for_each_entry(hand, &ioctl_list, item) {
308 err = hand->handle_ioctl(cmd, data);
309 if (err != -EINVAL) {
311 err = libcfs_ioctl_popdata(arg,
312 data, sizeof (*data));
316 up_read(&ioctl_list_sem);
324 struct cfs_psdev_ops libcfs_psdev_ops = {
326 libcfs_psdev_release,
332 extern int insert_proc(void);
333 extern void remove_proc(void);
334 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
335 MODULE_DESCRIPTION("Portals v3.1");
336 MODULE_LICENSE("GPL");
338 extern cfs_psdev_t libcfs_dev;
339 extern struct rw_semaphore tracefile_sem;
340 extern struct semaphore trace_thread_sem;
342 extern void libcfs_init_nidstrings(void);
343 extern int libcfs_arch_init(void);
344 extern void libcfs_arch_cleanup(void);
346 static int init_libcfs_module(void)
351 libcfs_init_nidstrings();
352 init_rwsem(&tracefile_sem);
353 init_mutex(&trace_thread_sem);
354 init_rwsem(&ioctl_list_sem);
355 CFS_INIT_LIST_HEAD(&ioctl_list);
357 rc = libcfs_debug_init(5 * 1024 * 1024);
359 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
366 CERROR("lwt_init: error %d\n", rc);
370 rc = cfs_psdev_register(&libcfs_dev);
372 CERROR("misc_register: error %d\n", rc);
378 CERROR("insert_proc: error %d\n", rc);
379 goto cleanup_deregister;
382 CDEBUG (D_OTHER, "portals setup OK\n");
386 cfs_psdev_deregister(&libcfs_dev);
392 libcfs_debug_cleanup();
396 static void exit_libcfs_module(void)
402 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
403 atomic_read(&libcfs_kmemory));
405 rc = cfs_psdev_deregister(&libcfs_dev);
407 CERROR("misc_deregister error %d\n", rc);
413 if (atomic_read(&libcfs_kmemory) != 0)
414 CERROR("Portals memory leaked: %d bytes\n",
415 atomic_read(&libcfs_kmemory));
417 rc = libcfs_debug_cleanup();
419 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n", rc);
420 libcfs_arch_cleanup();
423 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);