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 <linux/libcfs/libcfs.h>
40 #include <linux/libcfs/libcfs_crypto.h>
41 #include <linux/lnet/lib-lnet.h>
42 #include <linux/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) {
56 level1p = (struct page **)page_address(*level0p);
59 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
62 level2p = (struct page **)page_address(*level1p);
65 while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
68 __free_page(*level2p);
69 ldu->ldu_memhog_pages--;
74 __free_page(*level1p);
75 ldu->ldu_memhog_pages--;
80 __free_page(*level0p);
81 ldu->ldu_memhog_pages--;
86 LASSERT (ldu->ldu_memhog_pages == 0);
90 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
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;
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;
176 ldu = (struct libcfs_device_userstate *)args;
178 kportal_memhog_free(ldu);
179 LIBCFS_FREE(ldu, sizeof(*ldu));
182 module_put(THIS_MODULE);
186 static struct rw_semaphore ioctl_list_sem;
187 static struct list_head ioctl_list;
189 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
193 down_write(&ioctl_list_sem);
194 if (!list_empty(&hand->item))
197 list_add_tail(&hand->item, &ioctl_list);
198 up_write(&ioctl_list_sem);
202 EXPORT_SYMBOL(libcfs_register_ioctl);
204 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
208 down_write(&ioctl_list_sem);
209 if (list_empty(&hand->item))
212 list_del_init(&hand->item);
213 up_write(&ioctl_list_sem);
217 EXPORT_SYMBOL(libcfs_deregister_ioctl);
219 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile,unsigned long cmd,
220 void *arg, struct libcfs_ioctl_data *data)
225 case IOC_LIBCFS_CLEAR_DEBUG:
226 libcfs_debug_clear_buffer();
229 * case IOC_LIBCFS_PANIC:
230 * Handled in arch/cfs_module.c
232 case IOC_LIBCFS_MARK_DEBUG:
233 if (data->ioc_inlbuf1 == NULL ||
234 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
236 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
238 case IOC_LIBCFS_MEMHOG:
239 if (pfile->private_data == NULL) {
242 kportal_memhog_free(pfile->private_data);
243 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
244 err = kportal_memhog_alloc(pfile->private_data,
248 kportal_memhog_free(pfile->private_data);
252 case IOC_LIBCFS_PING_TEST: {
253 extern void (kping_client)(struct libcfs_ioctl_data *);
254 void (*ping)(struct libcfs_ioctl_data *);
256 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
257 data->ioc_count, libcfs_nid2str(data->ioc_nid),
258 libcfs_nid2str(data->ioc_nid));
259 ping = symbol_get(kping_client);
261 CERROR("symbol_get failed\n");
264 symbol_put(kping_client);
270 struct libcfs_ioctl_handler *hand;
272 down_read(&ioctl_list_sem);
273 list_for_each_entry(hand, &ioctl_list, item) {
274 err = hand->handle_ioctl(cmd, data);
275 if (err != -EINVAL) {
277 err = libcfs_ioctl_popdata(arg,
278 data, sizeof (*data));
282 up_read(&ioctl_list_sem);
290 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
293 struct libcfs_ioctl_data *data;
296 LIBCFS_ALLOC_GFP(buf, 1024, GFP_IOFS);
300 /* 'cmd' and permissions get checked in our arch-specific caller */
301 if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
302 CERROR("PORTALS ioctl: data error\n");
303 GOTO(out, err = -EINVAL);
305 data = (struct libcfs_ioctl_data *)buf;
307 err = libcfs_ioctl_int(pfile, cmd, arg, data);
310 LIBCFS_FREE(buf, 1024);
315 struct cfs_psdev_ops libcfs_psdev_ops = {
317 libcfs_psdev_release,
323 extern int insert_proc(void);
324 extern void remove_proc(void);
325 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
326 MODULE_DESCRIPTION("Portals v3.1");
327 MODULE_LICENSE("GPL");
329 extern struct miscdevice libcfs_dev;
330 extern struct rw_semaphore cfs_tracefile_sem;
331 extern struct mutex cfs_trace_thread_mutex;
332 extern struct cfs_wi_sched *cfs_sched_rehash;
334 extern void libcfs_init_nidstrings(void);
335 extern int libcfs_arch_init(void);
336 extern void libcfs_arch_cleanup(void);
338 static int init_libcfs_module(void)
343 libcfs_init_nidstrings();
344 init_rwsem(&cfs_tracefile_sem);
345 mutex_init(&cfs_trace_thread_mutex);
346 init_rwsem(&ioctl_list_sem);
347 INIT_LIST_HEAD(&ioctl_list);
348 init_waitqueue_head(&cfs_race_waitq);
350 rc = libcfs_debug_init(5 * 1024 * 1024);
352 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
360 rc = misc_register(&libcfs_dev);
362 CERROR("misc_register: error %d\n", rc);
366 rc = cfs_wi_startup();
368 CERROR("initialize workitem: error %d\n", rc);
369 goto cleanup_deregister;
372 /* max to 4 threads, should be enough for rehash */
373 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
374 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
375 rc, &cfs_sched_rehash);
377 CERROR("Startup workitem scheduler: error: %d\n", rc);
378 goto cleanup_deregister;
381 rc = cfs_crypto_register();
383 CERROR("cfs_crypto_regster: error %d\n", rc);
390 CERROR("insert_proc: error %d\n", rc);
394 CDEBUG (D_OTHER, "portals setup OK\n");
397 cfs_crypto_unregister();
401 misc_deregister(&libcfs_dev);
405 libcfs_debug_cleanup();
409 static void exit_libcfs_module(void)
415 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
416 atomic_read(&libcfs_kmemory));
418 if (cfs_sched_rehash != NULL) {
419 cfs_wi_sched_destroy(cfs_sched_rehash);
420 cfs_sched_rehash = NULL;
423 cfs_crypto_unregister();
426 rc = misc_deregister(&libcfs_dev);
428 CERROR("misc_deregister error %d\n", rc);
432 if (atomic_read(&libcfs_kmemory) != 0)
433 CERROR("Portals memory leaked: %d bytes\n",
434 atomic_read(&libcfs_kmemory));
436 rc = libcfs_debug_cleanup();
438 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
441 fini_rwsem(&ioctl_list_sem);
442 fini_rwsem(&cfs_tracefile_sem);
444 libcfs_arch_cleanup();
447 MODULE_VERSION("1.0.0");
448 module_init(init_libcfs_module);
449 module_exit(exit_libcfs_module);