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);
240 case IOC_LIBCFS_MEMHOG:
241 if (pfile->private_data == NULL) {
244 kportal_memhog_free(pfile->private_data);
245 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
246 err = kportal_memhog_alloc(pfile->private_data,
250 kportal_memhog_free(pfile->private_data);
254 case IOC_LIBCFS_PING_TEST: {
255 extern void (kping_client)(struct libcfs_ioctl_data *);
256 void (*ping)(struct libcfs_ioctl_data *);
258 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
259 data->ioc_count, libcfs_nid2str(data->ioc_nid),
260 libcfs_nid2str(data->ioc_nid));
261 ping = symbol_get(kping_client);
263 CERROR("symbol_get failed\n");
266 symbol_put(kping_client);
272 struct libcfs_ioctl_handler *hand;
275 down_read(&ioctl_list_sem);
276 list_for_each_entry(hand, &ioctl_list, item) {
277 err = hand->handle_ioctl(cmd, data);
278 if (err != -EINVAL) {
280 err = libcfs_ioctl_popdata(arg,
281 data, sizeof (*data));
285 up_read(&ioctl_list_sem);
293 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
294 unsigned long cmd, void *arg)
297 struct libcfs_ioctl_data *data;
301 LIBCFS_ALLOC_GFP(buf, 1024, GFP_IOFS);
305 /* 'cmd' and permissions get checked in our arch-specific caller */
306 if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
307 CERROR("PORTALS ioctl: data error\n");
308 GOTO(out, err = -EINVAL);
310 data = (struct libcfs_ioctl_data *)buf;
312 err = libcfs_ioctl_int(pfile, cmd, arg, data);
315 LIBCFS_FREE(buf, 1024);
320 struct cfs_psdev_ops libcfs_psdev_ops = {
322 libcfs_psdev_release,
328 extern int insert_proc(void);
329 extern void remove_proc(void);
330 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
331 MODULE_DESCRIPTION("Portals v3.1");
332 MODULE_LICENSE("GPL");
334 extern struct miscdevice libcfs_dev;
335 extern struct rw_semaphore cfs_tracefile_sem;
336 extern struct mutex cfs_trace_thread_mutex;
337 extern struct cfs_wi_sched *cfs_sched_rehash;
339 extern void libcfs_init_nidstrings(void);
340 extern int libcfs_arch_init(void);
341 extern void libcfs_arch_cleanup(void);
343 static int init_libcfs_module(void)
348 libcfs_init_nidstrings();
349 init_rwsem(&cfs_tracefile_sem);
350 mutex_init(&cfs_trace_thread_mutex);
351 init_rwsem(&ioctl_list_sem);
352 INIT_LIST_HEAD(&ioctl_list);
353 init_waitqueue_head(&cfs_race_waitq);
355 rc = libcfs_debug_init(5 * 1024 * 1024);
357 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
365 rc = misc_register(&libcfs_dev);
367 CERROR("misc_register: error %d\n", rc);
371 rc = cfs_wi_startup();
373 CERROR("initialize workitem: error %d\n", rc);
374 goto cleanup_deregister;
377 /* max to 4 threads, should be enough for rehash */
378 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
379 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
380 rc, &cfs_sched_rehash);
382 CERROR("Startup workitem scheduler: error: %d\n", rc);
383 goto cleanup_deregister;
386 rc = cfs_crypto_register();
388 CERROR("cfs_crypto_regster: error %d\n", rc);
395 CERROR("insert_proc: error %d\n", rc);
399 CDEBUG (D_OTHER, "portals setup OK\n");
402 cfs_crypto_unregister();
406 misc_deregister(&libcfs_dev);
410 libcfs_debug_cleanup();
414 static void exit_libcfs_module(void)
420 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
421 atomic_read(&libcfs_kmemory));
423 if (cfs_sched_rehash != NULL) {
424 cfs_wi_sched_destroy(cfs_sched_rehash);
425 cfs_sched_rehash = NULL;
428 cfs_crypto_unregister();
431 rc = misc_deregister(&libcfs_dev);
433 CERROR("misc_deregister error %d\n", rc);
437 if (atomic_read(&libcfs_kmemory) != 0)
438 CERROR("Portals memory leaked: %d bytes\n",
439 atomic_read(&libcfs_kmemory));
441 rc = libcfs_debug_cleanup();
443 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
446 fini_rwsem(&ioctl_list_sem);
447 fini_rwsem(&cfs_tracefile_sem);
449 libcfs_arch_cleanup();
452 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);