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
38 #define LNET_MAX_IOCTL_BUF_LEN (sizeof(struct lnet_ioctl_net_config) + \
39 sizeof(struct lnet_ioctl_config_data))
41 #include <libcfs/libcfs.h>
42 #include <libcfs/libcfs_crypto.h>
43 #include <lnet/lib-lnet.h>
44 #include <lnet/lib-dlc.h>
45 #include <lnet/lnet.h>
46 #include <lnet/nidstr.h>
47 #include "tracefile.h"
50 kportal_memhog_free (struct libcfs_device_userstate *ldu)
52 struct page **level0p = &ldu->ldu_memhog_root_page;
53 struct page **level1p;
54 struct page **level2p;
58 if (*level0p != NULL) {
59 level1p = (struct page **)page_address(*level0p);
62 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
65 level2p = (struct page **)page_address(*level1p);
68 while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
71 __free_page(*level2p);
72 ldu->ldu_memhog_pages--;
77 __free_page(*level1p);
78 ldu->ldu_memhog_pages--;
83 __free_page(*level0p);
84 ldu->ldu_memhog_pages--;
89 LASSERT(ldu->ldu_memhog_pages == 0);
93 kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
96 struct page **level0p;
97 struct page **level1p;
98 struct page **level2p;
102 LASSERT(ldu->ldu_memhog_pages == 0);
103 LASSERT(ldu->ldu_memhog_root_page == NULL);
111 level0p = &ldu->ldu_memhog_root_page;
112 *level0p = alloc_page(flags);
113 if (*level0p == NULL)
115 ldu->ldu_memhog_pages++;
117 level1p = (struct page **)page_address(*level0p);
119 memset(level1p, 0, PAGE_CACHE_SIZE);
121 while (ldu->ldu_memhog_pages < npages &&
122 count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
124 if (cfs_signal_pending())
127 *level1p = alloc_page(flags);
128 if (*level1p == NULL)
130 ldu->ldu_memhog_pages++;
132 level2p = (struct page **)page_address(*level1p);
134 memset(level2p, 0, PAGE_CACHE_SIZE);
136 while (ldu->ldu_memhog_pages < npages &&
137 count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
139 if (cfs_signal_pending())
142 *level2p = alloc_page(flags);
143 if (*level2p == NULL)
145 ldu->ldu_memhog_pages++;
158 /* called when opening /dev/device */
159 static int libcfs_psdev_open(unsigned long flags, void *args)
161 struct libcfs_device_userstate *ldu;
164 try_module_get(THIS_MODULE);
166 LIBCFS_ALLOC(ldu, sizeof(*ldu));
168 ldu->ldu_memhog_pages = 0;
169 ldu->ldu_memhog_root_page = NULL;
171 *(struct libcfs_device_userstate **)args = ldu;
176 /* called when closing /dev/device */
177 static int libcfs_psdev_release(unsigned long flags, void *args)
179 struct libcfs_device_userstate *ldu;
182 ldu = (struct libcfs_device_userstate *)args;
184 kportal_memhog_free(ldu);
185 LIBCFS_FREE(ldu, sizeof(*ldu));
188 module_put(THIS_MODULE);
192 static struct rw_semaphore ioctl_list_sem;
193 static struct list_head ioctl_list;
195 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
199 down_write(&ioctl_list_sem);
200 if (!list_empty(&hand->item))
203 list_add_tail(&hand->item, &ioctl_list);
204 up_write(&ioctl_list_sem);
208 EXPORT_SYMBOL(libcfs_register_ioctl);
210 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
214 down_write(&ioctl_list_sem);
215 if (list_empty(&hand->item))
218 list_del_init(&hand->item);
219 up_write(&ioctl_list_sem);
223 EXPORT_SYMBOL(libcfs_deregister_ioctl);
225 static int libcfs_ioctl_handle(struct cfs_psdev_file *pfile, unsigned long cmd,
226 void __user *arg, struct libcfs_ioctl_hdr *hdr)
228 struct libcfs_ioctl_data *data = NULL;
232 /* The libcfs_ioctl_data_adjust() function performs adjustment
233 * operations on the libcfs_ioctl_data structure to make
234 * it usable by the code. This doesn't need to be called
235 * for new data structures added. */
236 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
237 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
238 err = libcfs_ioctl_data_adjust(data);
245 case IOC_LIBCFS_CLEAR_DEBUG:
246 libcfs_debug_clear_buffer();
249 * case IOC_LIBCFS_PANIC:
250 * Handled in arch/cfs_module.c
252 case IOC_LIBCFS_MARK_DEBUG:
253 if (data->ioc_inlbuf1 == NULL ||
254 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
256 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
258 case IOC_LIBCFS_MEMHOG:
259 if (pfile->private_data == NULL) {
262 kportal_memhog_free(pfile->private_data);
263 /* XXX The ioc_flags is not GFP flags now, need to
265 err = kportal_memhog_alloc(pfile->private_data,
269 kportal_memhog_free(pfile->private_data);
273 case IOC_LIBCFS_PING_TEST: {
274 extern void (kping_client)(struct libcfs_ioctl_data *);
275 void (*ping)(struct libcfs_ioctl_data *);
277 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
278 data->ioc_count, libcfs_nid2str(data->ioc_nid),
279 libcfs_nid2str(data->ioc_nid));
280 ping = symbol_get(kping_client);
282 CERROR("symbol_get failed\n");
285 symbol_put(kping_client);
291 struct libcfs_ioctl_handler *hand;
294 down_read(&ioctl_list_sem);
295 list_for_each_entry(hand, &ioctl_list, item) {
296 err = hand->handle_ioctl(cmd, hdr);
297 if (err != -EINVAL) {
299 err = libcfs_ioctl_popdata(arg,
304 up_read(&ioctl_list_sem);
312 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
313 unsigned long cmd, void __user *arg)
315 struct libcfs_ioctl_hdr *hdr;
320 err = libcfs_ioctl_getdata_len(arg, &buf_len);
325 * do a check here to restrict the size of the memory
326 * to allocate to guard against DoS attacks.
328 if (buf_len > LNET_MAX_IOCTL_BUF_LEN) {
329 CERROR("LNET: user buffer exceeds kernel buffer\n");
333 LIBCFS_ALLOC_GFP(hdr, buf_len, GFP_IOFS);
337 /* 'cmd' and permissions get checked in our arch-specific caller */
338 if (libcfs_ioctl_getdata(hdr, buf_len, arg)) {
339 CERROR("LNET ioctl: data error\n");
340 GOTO(out, err = -EINVAL);
343 err = libcfs_ioctl_handle(pfile, cmd, arg, hdr);
346 LIBCFS_FREE(hdr, buf_len);
351 struct cfs_psdev_ops libcfs_psdev_ops = {
353 libcfs_psdev_release,
359 extern int insert_proc(void);
360 extern void remove_proc(void);
361 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
362 MODULE_DESCRIPTION("Portals v3.1");
363 MODULE_LICENSE("GPL");
365 extern struct miscdevice libcfs_dev;
366 extern struct rw_semaphore cfs_tracefile_sem;
367 extern struct mutex cfs_trace_thread_mutex;
368 extern struct cfs_wi_sched *cfs_sched_rehash;
370 extern void libcfs_init_nidstrings(void);
371 extern int libcfs_arch_init(void);
372 extern void libcfs_arch_cleanup(void);
374 static int init_libcfs_module(void)
379 libcfs_init_nidstrings();
380 init_rwsem(&cfs_tracefile_sem);
381 mutex_init(&cfs_trace_thread_mutex);
382 init_rwsem(&ioctl_list_sem);
383 INIT_LIST_HEAD(&ioctl_list);
384 init_waitqueue_head(&cfs_race_waitq);
386 rc = libcfs_debug_init(5 * 1024 * 1024);
388 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
396 rc = misc_register(&libcfs_dev);
398 CERROR("misc_register: error %d\n", rc);
402 rc = cfs_wi_startup();
404 CERROR("initialize workitem: error %d\n", rc);
405 goto cleanup_deregister;
408 /* max to 4 threads, should be enough for rehash */
409 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
410 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
411 rc, &cfs_sched_rehash);
413 CERROR("Startup workitem scheduler: error: %d\n", rc);
414 goto cleanup_deregister;
417 rc = cfs_crypto_register();
419 CERROR("cfs_crypto_regster: error %d\n", rc);
426 CERROR("insert_proc: error %d\n", rc);
430 CDEBUG (D_OTHER, "portals setup OK\n");
433 cfs_crypto_unregister();
437 misc_deregister(&libcfs_dev);
441 libcfs_debug_cleanup();
445 static void exit_libcfs_module(void)
451 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
452 atomic_read(&libcfs_kmemory));
454 if (cfs_sched_rehash != NULL) {
455 cfs_wi_sched_destroy(cfs_sched_rehash);
456 cfs_sched_rehash = NULL;
459 cfs_crypto_unregister();
462 rc = misc_deregister(&libcfs_dev);
464 CERROR("misc_deregister error %d\n", rc);
468 if (atomic_read(&libcfs_kmemory) != 0)
469 CERROR("Portals memory leaked: %d bytes\n",
470 atomic_read(&libcfs_kmemory));
472 rc = libcfs_debug_cleanup();
474 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
477 fini_rwsem(&ioctl_list_sem);
478 fini_rwsem(&cfs_tracefile_sem);
480 libcfs_arch_cleanup();
483 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);