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
,
93 struct page
**level0p
;
94 struct page
**level1p
;
95 struct page
**level2p
;
99 LASSERT (ldu
->ldu_memhog_pages
== 0);
100 LASSERT (ldu
->ldu_memhog_root_page
== NULL
);
108 level0p
= &ldu
->ldu_memhog_root_page
;
109 *level0p
= alloc_page(flags
);
110 if (*level0p
== NULL
)
112 ldu
->ldu_memhog_pages
++;
114 level1p
= (struct page
**)page_address(*level0p
);
116 memset(level1p
, 0, PAGE_CACHE_SIZE
);
118 while (ldu
->ldu_memhog_pages
< npages
&&
119 count1
< PAGE_CACHE_SIZE
/sizeof(struct page
*)) {
121 if (cfs_signal_pending())
124 *level1p
= alloc_page(flags
);
125 if (*level1p
== NULL
)
127 ldu
->ldu_memhog_pages
++;
129 level2p
= (struct page
**)page_address(*level1p
);
131 memset(level2p
, 0, PAGE_CACHE_SIZE
);
133 while (ldu
->ldu_memhog_pages
< npages
&&
134 count2
< PAGE_CACHE_SIZE
/sizeof(struct page
*)) {
136 if (cfs_signal_pending())
139 *level2p
= alloc_page(flags
);
140 if (*level2p
== NULL
)
142 ldu
->ldu_memhog_pages
++;
155 /* called when opening /dev/device */
156 static int libcfs_psdev_open(unsigned long flags
, void *args
)
158 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
;
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
)
226 case IOC_LIBCFS_CLEAR_DEBUG
:
227 libcfs_debug_clear_buffer();
230 * case IOC_LIBCFS_PANIC:
231 * Handled in arch/cfs_module.c
233 case IOC_LIBCFS_MARK_DEBUG
:
234 if (data
->ioc_inlbuf1
== NULL
||
235 data
->ioc_inlbuf1
[data
->ioc_inllen1
- 1] != '\0')
237 libcfs_debug_mark_buffer(data
->ioc_inlbuf1
);
239 case IOC_LIBCFS_MEMHOG
:
240 if (pfile
->private_data
== NULL
) {
243 kportal_memhog_free(pfile
->private_data
);
244 /* XXX The ioc_flags is not GFP flags now, need to be fixed */
245 err
= kportal_memhog_alloc(pfile
->private_data
,
249 kportal_memhog_free(pfile
->private_data
);
253 case IOC_LIBCFS_PING_TEST
: {
254 extern void (kping_client
)(struct libcfs_ioctl_data
*);
255 void (*ping
)(struct libcfs_ioctl_data
*);
257 CDEBUG(D_IOCTL
, "doing %d pings to nid %s (%s)\n",
258 data
->ioc_count
, libcfs_nid2str(data
->ioc_nid
),
259 libcfs_nid2str(data
->ioc_nid
));
260 ping
= symbol_get(kping_client
);
262 CERROR("symbol_get failed\n");
265 symbol_put(kping_client
);
271 struct libcfs_ioctl_handler
*hand
;
273 down_read(&ioctl_list_sem
);
274 list_for_each_entry(hand
, &ioctl_list
, item
) {
275 err
= hand
->handle_ioctl(cmd
, data
);
276 if (err
!= -EINVAL
) {
278 err
= libcfs_ioctl_popdata(arg
,
279 data
, sizeof (*data
));
283 up_read(&ioctl_list_sem
);
291 static int libcfs_ioctl(struct cfs_psdev_file
*pfile
, unsigned long cmd
, void *arg
)
294 struct libcfs_ioctl_data
*data
;
297 LIBCFS_ALLOC_GFP(buf
, 1024, GFP_IOFS
);
301 /* 'cmd' and permissions get checked in our arch-specific caller */
302 if (libcfs_ioctl_getdata(buf
, buf
+ 800, (void *)arg
)) {
303 CERROR("PORTALS ioctl: data error\n");
304 GOTO(out
, err
= -EINVAL
);
306 data
= (struct libcfs_ioctl_data
*)buf
;
308 err
= libcfs_ioctl_int(pfile
, cmd
, arg
, data
);
311 LIBCFS_FREE(buf
, 1024);
316 struct cfs_psdev_ops libcfs_psdev_ops
= {
318 libcfs_psdev_release
,
324 extern int insert_proc(void);
325 extern void remove_proc(void);
326 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
327 MODULE_DESCRIPTION("Portals v3.1");
328 MODULE_LICENSE("GPL");
330 extern struct miscdevice libcfs_dev
;
331 extern struct rw_semaphore cfs_tracefile_sem
;
332 extern struct mutex cfs_trace_thread_mutex
;
333 extern struct cfs_wi_sched
*cfs_sched_rehash
;
335 extern void libcfs_init_nidstrings(void);
336 extern int libcfs_arch_init(void);
337 extern void libcfs_arch_cleanup(void);
339 static int init_libcfs_module(void)
344 libcfs_init_nidstrings();
345 init_rwsem(&cfs_tracefile_sem
);
346 mutex_init(&cfs_trace_thread_mutex
);
347 init_rwsem(&ioctl_list_sem
);
348 INIT_LIST_HEAD(&ioctl_list
);
349 init_waitqueue_head(&cfs_race_waitq
);
351 rc
= libcfs_debug_init(5 * 1024 * 1024);
353 printk(KERN_ERR
"LustreError: libcfs_debug_init: %d\n", rc
);
361 rc
= misc_register(&libcfs_dev
);
363 CERROR("misc_register: error %d\n", rc
);
367 rc
= cfs_wi_startup();
369 CERROR("initialize workitem: error %d\n", rc
);
370 goto cleanup_deregister
;
373 /* max to 4 threads, should be enough for rehash */
374 rc
= min(cfs_cpt_weight(cfs_cpt_table
, CFS_CPT_ANY
), 4);
375 rc
= cfs_wi_sched_create("cfs_rh", cfs_cpt_table
, CFS_CPT_ANY
,
376 rc
, &cfs_sched_rehash
);
378 CERROR("Startup workitem scheduler: error: %d\n", rc
);
379 goto cleanup_deregister
;
382 rc
= cfs_crypto_register();
384 CERROR("cfs_crypto_register: error %d\n", rc
);
391 CERROR("insert_proc: error %d\n", rc
);
395 CDEBUG (D_OTHER
, "portals setup OK\n");
398 cfs_crypto_unregister();
402 misc_deregister(&libcfs_dev
);
406 libcfs_debug_cleanup();
410 static void exit_libcfs_module(void)
416 CDEBUG(D_MALLOC
, "before Portals cleanup: kmem %d\n",
417 atomic_read(&libcfs_kmemory
));
419 if (cfs_sched_rehash
!= NULL
) {
420 cfs_wi_sched_destroy(cfs_sched_rehash
);
421 cfs_sched_rehash
= NULL
;
424 cfs_crypto_unregister();
427 rc
= misc_deregister(&libcfs_dev
);
429 CERROR("misc_deregister error %d\n", rc
);
433 if (atomic_read(&libcfs_kmemory
) != 0)
434 CERROR("Portals memory leaked: %d bytes\n",
435 atomic_read(&libcfs_kmemory
));
437 rc
= libcfs_debug_cleanup();
439 printk(KERN_ERR
"LustreError: libcfs_debug_cleanup: %d\n",
442 fini_rwsem(&ioctl_list_sem
);
443 fini_rwsem(&cfs_tracefile_sem
);
445 libcfs_arch_cleanup();
448 MODULE_VERSION("1.0.0");
449 module_init(init_libcfs_module
);
450 module_exit(exit_libcfs_module
);