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.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LNET
39 #include <libcfs/libcfs.h>
40 #include <libcfs/libcfs_crypto.h>
41 #include <lnet/lib-lnet.h>
42 #include <lnet/lnet.h>
43 #include "tracefile.h"
46 kportal_memhog_free (struct libcfs_device_userstate *ldu)
48 struct page **level0p = &ldu->ldu_memhog_root_page;
49 struct page **level1p;
50 struct page **level2p;
54 if (*level0p != NULL) {
55 level1p = (struct page **)page_address(*level0p);
58 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
61 level2p = (struct page **)page_address(*level1p);
64 while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
67 __free_page(*level2p);
68 ldu->ldu_memhog_pages--;
73 __free_page(*level1p);
74 ldu->ldu_memhog_pages--;
79 __free_page(*level0p);
80 ldu->ldu_memhog_pages--;
85 LASSERT(ldu->ldu_memhog_pages == 0);
89 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
91 struct page **level0p;
92 struct page **level1p;
93 struct page **level2p;
97 LASSERT(ldu->ldu_memhog_pages == 0);
98 LASSERT(ldu->ldu_memhog_root_page == NULL);
106 level0p = &ldu->ldu_memhog_root_page;
107 *level0p = alloc_page(flags);
108 if (*level0p == NULL)
110 ldu->ldu_memhog_pages++;
112 level1p = (struct page **)page_address(*level0p);
114 memset(level1p, 0, PAGE_CACHE_SIZE);
116 while (ldu->ldu_memhog_pages < npages &&
117 count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
119 if (cfs_signal_pending())
122 *level1p = alloc_page(flags);
123 if (*level1p == NULL)
125 ldu->ldu_memhog_pages++;
127 level2p = (struct page **)page_address(*level1p);
129 memset(level2p, 0, PAGE_CACHE_SIZE);
131 while (ldu->ldu_memhog_pages < npages &&
132 count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
134 if (cfs_signal_pending())
137 *level2p = alloc_page(flags);
138 if (*level2p == NULL)
140 ldu->ldu_memhog_pages++;
153 /* called when opening /dev/device */
154 static int libcfs_psdev_open(unsigned long flags, void *args)
156 struct libcfs_device_userstate *ldu;
159 try_module_get(THIS_MODULE);
161 LIBCFS_ALLOC(ldu, sizeof(*ldu));
163 ldu->ldu_memhog_pages = 0;
164 ldu->ldu_memhog_root_page = NULL;
166 *(struct libcfs_device_userstate **)args = ldu;
171 /* called when closing /dev/device */
172 static int libcfs_psdev_release(unsigned long flags, void *args)
174 struct libcfs_device_userstate *ldu;
177 ldu = (struct libcfs_device_userstate *)args;
179 kportal_memhog_free(ldu);
180 LIBCFS_FREE(ldu, sizeof(*ldu));
183 module_put(THIS_MODULE);
187 static struct rw_semaphore ioctl_list_sem;
188 static struct list_head ioctl_list;
190 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
194 down_write(&ioctl_list_sem);
195 if (!list_empty(&hand->item))
198 list_add_tail(&hand->item, &ioctl_list);
199 up_write(&ioctl_list_sem);
203 EXPORT_SYMBOL(libcfs_register_ioctl);
205 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
209 down_write(&ioctl_list_sem);
210 if (list_empty(&hand->item))
213 list_del_init(&hand->item);
214 up_write(&ioctl_list_sem);
218 EXPORT_SYMBOL(libcfs_deregister_ioctl);
220 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile,unsigned long cmd,
221 void *arg, struct libcfs_ioctl_data *data)
227 case IOC_LIBCFS_CLEAR_DEBUG:
228 libcfs_debug_clear_buffer();
231 * case IOC_LIBCFS_PANIC:
232 * Handled in arch/cfs_module.c
234 case IOC_LIBCFS_MARK_DEBUG:
235 if (data->ioc_inlbuf1 == NULL ||
236 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
238 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
241 case IOC_LIBCFS_LWT_CONTROL:
242 err = lwt_control ((data->ioc_flags & 1) != 0,
243 (data->ioc_flags & 2) != 0);
246 case IOC_LIBCFS_LWT_SNAPSHOT: {
251 err = lwt_snapshot (&now, &ncpu, &total_size,
252 data->ioc_pbuf1, data->ioc_plen1);
253 data->ioc_u64[0] = now;
254 data->ioc_u32[0] = ncpu;
255 data->ioc_u32[1] = total_size;
257 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
258 data->ioc_u32[2] = sizeof(lwt_event_t);
259 data->ioc_u32[3] = offsetof(lwt_event_t, lwte_where);
262 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
267 case IOC_LIBCFS_LWT_LOOKUP_STRING:
268 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
269 data->ioc_pbuf2, data->ioc_plen2);
271 libcfs_ioctl_popdata(arg, data, sizeof (*data)))
275 case IOC_LIBCFS_MEMHOG:
276 if (pfile->private_data == NULL) {
279 kportal_memhog_free(pfile->private_data);
280 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
281 err = kportal_memhog_alloc(pfile->private_data,
285 kportal_memhog_free(pfile->private_data);
289 case IOC_LIBCFS_PING_TEST: {
290 extern void (kping_client)(struct libcfs_ioctl_data *);
291 void (*ping)(struct libcfs_ioctl_data *);
293 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
294 data->ioc_count, libcfs_nid2str(data->ioc_nid),
295 libcfs_nid2str(data->ioc_nid));
296 ping = symbol_get(kping_client);
298 CERROR("symbol_get failed\n");
301 symbol_put(kping_client);
307 struct libcfs_ioctl_handler *hand;
310 down_read(&ioctl_list_sem);
311 list_for_each_entry(hand, &ioctl_list, item) {
312 err = hand->handle_ioctl(cmd, data);
313 if (err != -EINVAL) {
315 err = libcfs_ioctl_popdata(arg,
316 data, sizeof (*data));
320 up_read(&ioctl_list_sem);
328 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
329 unsigned long cmd, void *arg)
332 struct libcfs_ioctl_data *data;
336 LIBCFS_ALLOC_GFP(buf, 1024, GFP_IOFS);
340 /* 'cmd' and permissions get checked in our arch-specific caller */
341 if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
342 CERROR("PORTALS ioctl: data error\n");
343 GOTO(out, err = -EINVAL);
345 data = (struct libcfs_ioctl_data *)buf;
347 err = libcfs_ioctl_int(pfile, cmd, arg, data);
350 LIBCFS_FREE(buf, 1024);
355 struct cfs_psdev_ops libcfs_psdev_ops = {
357 libcfs_psdev_release,
363 extern int insert_proc(void);
364 extern void remove_proc(void);
365 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
366 MODULE_DESCRIPTION("Portals v3.1");
367 MODULE_LICENSE("GPL");
369 extern struct miscdevice libcfs_dev;
370 extern struct rw_semaphore cfs_tracefile_sem;
371 extern struct mutex cfs_trace_thread_mutex;
372 extern struct cfs_wi_sched *cfs_sched_rehash;
374 extern void libcfs_init_nidstrings(void);
375 extern int libcfs_arch_init(void);
376 extern void libcfs_arch_cleanup(void);
378 static int init_libcfs_module(void)
383 libcfs_init_nidstrings();
384 init_rwsem(&cfs_tracefile_sem);
385 mutex_init(&cfs_trace_thread_mutex);
386 init_rwsem(&ioctl_list_sem);
387 INIT_LIST_HEAD(&ioctl_list);
388 init_waitqueue_head(&cfs_race_waitq);
390 rc = libcfs_debug_init(5 * 1024 * 1024);
392 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
403 CERROR("lwt_init: error %d\n", rc);
407 rc = misc_register(&libcfs_dev);
409 CERROR("misc_register: error %d\n", rc);
413 rc = cfs_wi_startup();
415 CERROR("initialize workitem: error %d\n", rc);
416 goto cleanup_deregister;
419 /* max to 4 threads, should be enough for rehash */
420 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
421 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
422 rc, &cfs_sched_rehash);
424 CERROR("Startup workitem scheduler: error: %d\n", rc);
425 goto cleanup_deregister;
428 rc = cfs_crypto_register();
430 CERROR("cfs_crypto_regster: error %d\n", rc);
437 CERROR("insert_proc: error %d\n", rc);
441 CDEBUG (D_OTHER, "portals setup OK\n");
444 cfs_crypto_unregister();
448 misc_deregister(&libcfs_dev);
454 libcfs_debug_cleanup();
458 static void exit_libcfs_module(void)
464 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
465 atomic_read(&libcfs_kmemory));
467 if (cfs_sched_rehash != NULL) {
468 cfs_wi_sched_destroy(cfs_sched_rehash);
469 cfs_sched_rehash = NULL;
472 cfs_crypto_unregister();
475 rc = misc_deregister(&libcfs_dev);
477 CERROR("misc_deregister error %d\n", rc);
484 if (atomic_read(&libcfs_kmemory) != 0)
485 CERROR("Portals memory leaked: %d bytes\n",
486 atomic_read(&libcfs_kmemory));
488 rc = libcfs_debug_cleanup();
490 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
493 fini_rwsem(&ioctl_list_sem);
494 fini_rwsem(&cfs_tracefile_sem);
496 libcfs_arch_cleanup();
499 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);