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,
92 struct page **level0p;
93 struct page **level1p;
94 struct page **level2p;
98 LASSERT(ldu->ldu_memhog_pages == 0);
99 LASSERT(ldu->ldu_memhog_root_page == NULL);
107 level0p = &ldu->ldu_memhog_root_page;
108 *level0p = alloc_page(flags);
109 if (*level0p == NULL)
111 ldu->ldu_memhog_pages++;
113 level1p = (struct page **)page_address(*level0p);
115 memset(level1p, 0, PAGE_CACHE_SIZE);
117 while (ldu->ldu_memhog_pages < npages &&
118 count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
120 if (cfs_signal_pending())
123 *level1p = alloc_page(flags);
124 if (*level1p == NULL)
126 ldu->ldu_memhog_pages++;
128 level2p = (struct page **)page_address(*level1p);
130 memset(level2p, 0, PAGE_CACHE_SIZE);
132 while (ldu->ldu_memhog_pages < npages &&
133 count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
135 if (cfs_signal_pending())
138 *level2p = alloc_page(flags);
139 if (*level2p == NULL)
141 ldu->ldu_memhog_pages++;
154 /* called when opening /dev/device */
155 static int libcfs_psdev_open(unsigned long flags, void *args)
157 struct libcfs_device_userstate *ldu;
160 try_module_get(THIS_MODULE);
162 LIBCFS_ALLOC(ldu, sizeof(*ldu));
164 ldu->ldu_memhog_pages = 0;
165 ldu->ldu_memhog_root_page = NULL;
167 *(struct libcfs_device_userstate **)args = ldu;
172 /* called when closing /dev/device */
173 static int libcfs_psdev_release(unsigned long flags, void *args)
175 struct libcfs_device_userstate *ldu;
178 ldu = (struct libcfs_device_userstate *)args;
180 kportal_memhog_free(ldu);
181 LIBCFS_FREE(ldu, sizeof(*ldu));
184 module_put(THIS_MODULE);
188 static struct rw_semaphore ioctl_list_sem;
189 static struct list_head ioctl_list;
191 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
195 down_write(&ioctl_list_sem);
196 if (!list_empty(&hand->item))
199 list_add_tail(&hand->item, &ioctl_list);
200 up_write(&ioctl_list_sem);
204 EXPORT_SYMBOL(libcfs_register_ioctl);
206 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
210 down_write(&ioctl_list_sem);
211 if (list_empty(&hand->item))
214 list_del_init(&hand->item);
215 up_write(&ioctl_list_sem);
219 EXPORT_SYMBOL(libcfs_deregister_ioctl);
221 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile,unsigned long cmd,
222 void *arg, struct libcfs_ioctl_data *data)
228 case IOC_LIBCFS_CLEAR_DEBUG:
229 libcfs_debug_clear_buffer();
232 * case IOC_LIBCFS_PANIC:
233 * Handled in arch/cfs_module.c
235 case IOC_LIBCFS_MARK_DEBUG:
236 if (data->ioc_inlbuf1 == NULL ||
237 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
239 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
241 case IOC_LIBCFS_MEMHOG:
242 if (pfile->private_data == NULL) {
245 kportal_memhog_free(pfile->private_data);
246 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
247 err = kportal_memhog_alloc(pfile->private_data,
251 kportal_memhog_free(pfile->private_data);
255 case IOC_LIBCFS_PING_TEST: {
256 extern void (kping_client)(struct libcfs_ioctl_data *);
257 void (*ping)(struct libcfs_ioctl_data *);
259 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
260 data->ioc_count, libcfs_nid2str(data->ioc_nid),
261 libcfs_nid2str(data->ioc_nid));
262 ping = symbol_get(kping_client);
264 CERROR("symbol_get failed\n");
267 symbol_put(kping_client);
273 struct libcfs_ioctl_handler *hand;
276 down_read(&ioctl_list_sem);
277 list_for_each_entry(hand, &ioctl_list, item) {
278 err = hand->handle_ioctl(cmd, data);
279 if (err != -EINVAL) {
281 err = libcfs_ioctl_popdata(arg,
282 data, sizeof (*data));
286 up_read(&ioctl_list_sem);
294 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
295 unsigned long cmd, void *arg)
298 struct libcfs_ioctl_data *data;
302 LIBCFS_ALLOC_GFP(buf, 1024, GFP_IOFS);
306 /* 'cmd' and permissions get checked in our arch-specific caller */
307 if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
308 CERROR("PORTALS ioctl: data error\n");
309 GOTO(out, err = -EINVAL);
311 data = (struct libcfs_ioctl_data *)buf;
313 err = libcfs_ioctl_int(pfile, cmd, arg, data);
316 LIBCFS_FREE(buf, 1024);
321 struct cfs_psdev_ops libcfs_psdev_ops = {
323 libcfs_psdev_release,
329 extern int insert_proc(void);
330 extern void remove_proc(void);
331 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
332 MODULE_DESCRIPTION("Portals v3.1");
333 MODULE_LICENSE("GPL");
335 extern struct miscdevice libcfs_dev;
336 extern struct rw_semaphore cfs_tracefile_sem;
337 extern struct mutex cfs_trace_thread_mutex;
338 extern struct cfs_wi_sched *cfs_sched_rehash;
340 extern void libcfs_init_nidstrings(void);
341 extern int libcfs_arch_init(void);
342 extern void libcfs_arch_cleanup(void);
344 static int init_libcfs_module(void)
349 libcfs_init_nidstrings();
350 init_rwsem(&cfs_tracefile_sem);
351 mutex_init(&cfs_trace_thread_mutex);
352 init_rwsem(&ioctl_list_sem);
353 INIT_LIST_HEAD(&ioctl_list);
354 init_waitqueue_head(&cfs_race_waitq);
356 rc = libcfs_debug_init(5 * 1024 * 1024);
358 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
366 rc = misc_register(&libcfs_dev);
368 CERROR("misc_register: error %d\n", rc);
372 rc = cfs_wi_startup();
374 CERROR("initialize workitem: error %d\n", rc);
375 goto cleanup_deregister;
378 /* max to 4 threads, should be enough for rehash */
379 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
380 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
381 rc, &cfs_sched_rehash);
383 CERROR("Startup workitem scheduler: error: %d\n", rc);
384 goto cleanup_deregister;
387 rc = cfs_crypto_register();
389 CERROR("cfs_crypto_regster: error %d\n", rc);
396 CERROR("insert_proc: error %d\n", rc);
400 CDEBUG (D_OTHER, "portals setup OK\n");
403 cfs_crypto_unregister();
407 misc_deregister(&libcfs_dev);
411 libcfs_debug_cleanup();
415 static void exit_libcfs_module(void)
421 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
422 atomic_read(&libcfs_kmemory));
424 if (cfs_sched_rehash != NULL) {
425 cfs_wi_sched_destroy(cfs_sched_rehash);
426 cfs_sched_rehash = NULL;
429 cfs_crypto_unregister();
432 rc = misc_deregister(&libcfs_dev);
434 CERROR("misc_deregister error %d\n", rc);
438 if (atomic_read(&libcfs_kmemory) != 0)
439 CERROR("Portals memory leaked: %d bytes\n",
440 atomic_read(&libcfs_kmemory));
442 rc = libcfs_debug_cleanup();
444 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
447 fini_rwsem(&ioctl_list_sem);
448 fini_rwsem(&cfs_tracefile_sem);
450 libcfs_arch_cleanup();
453 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);