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 "tracefile.h"
49 kportal_memhog_free (struct libcfs_device_userstate *ldu)
51 struct page **level0p = &ldu->ldu_memhog_root_page;
52 struct page **level1p;
53 struct page **level2p;
57 if (*level0p != NULL) {
58 level1p = (struct page **)page_address(*level0p);
61 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
64 level2p = (struct page **)page_address(*level1p);
67 while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
70 __free_page(*level2p);
71 ldu->ldu_memhog_pages--;
76 __free_page(*level1p);
77 ldu->ldu_memhog_pages--;
82 __free_page(*level0p);
83 ldu->ldu_memhog_pages--;
88 LASSERT(ldu->ldu_memhog_pages == 0);
92 kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
95 struct page **level0p;
96 struct page **level1p;
97 struct page **level2p;
101 LASSERT(ldu->ldu_memhog_pages == 0);
102 LASSERT(ldu->ldu_memhog_root_page == NULL);
110 level0p = &ldu->ldu_memhog_root_page;
111 *level0p = alloc_page(flags);
112 if (*level0p == NULL)
114 ldu->ldu_memhog_pages++;
116 level1p = (struct page **)page_address(*level0p);
118 memset(level1p, 0, PAGE_CACHE_SIZE);
120 while (ldu->ldu_memhog_pages < npages &&
121 count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
123 if (cfs_signal_pending())
126 *level1p = alloc_page(flags);
127 if (*level1p == NULL)
129 ldu->ldu_memhog_pages++;
131 level2p = (struct page **)page_address(*level1p);
133 memset(level2p, 0, PAGE_CACHE_SIZE);
135 while (ldu->ldu_memhog_pages < npages &&
136 count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
138 if (cfs_signal_pending())
141 *level2p = alloc_page(flags);
142 if (*level2p == NULL)
144 ldu->ldu_memhog_pages++;
157 /* called when opening /dev/device */
158 static int libcfs_psdev_open(unsigned long flags, void *args)
160 struct libcfs_device_userstate *ldu;
163 try_module_get(THIS_MODULE);
165 LIBCFS_ALLOC(ldu, sizeof(*ldu));
167 ldu->ldu_memhog_pages = 0;
168 ldu->ldu_memhog_root_page = NULL;
170 *(struct libcfs_device_userstate **)args = ldu;
175 /* called when closing /dev/device */
176 static int libcfs_psdev_release(unsigned long flags, void *args)
178 struct libcfs_device_userstate *ldu;
181 ldu = (struct libcfs_device_userstate *)args;
183 kportal_memhog_free(ldu);
184 LIBCFS_FREE(ldu, sizeof(*ldu));
187 module_put(THIS_MODULE);
191 static struct rw_semaphore ioctl_list_sem;
192 static struct list_head ioctl_list;
194 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
198 down_write(&ioctl_list_sem);
199 if (!list_empty(&hand->item))
202 list_add_tail(&hand->item, &ioctl_list);
203 up_write(&ioctl_list_sem);
207 EXPORT_SYMBOL(libcfs_register_ioctl);
209 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
213 down_write(&ioctl_list_sem);
214 if (list_empty(&hand->item))
217 list_del_init(&hand->item);
218 up_write(&ioctl_list_sem);
222 EXPORT_SYMBOL(libcfs_deregister_ioctl);
224 static int libcfs_ioctl_handle(struct cfs_psdev_file *pfile, unsigned long cmd,
225 void *arg, struct libcfs_ioctl_hdr *hdr)
227 struct libcfs_ioctl_data *data = NULL;
231 /* The libcfs_ioctl_data_adjust() function performs adjustment
232 * operations on the libcfs_ioctl_data structure to make
233 * it usable by the code. This doesn't need to be called
234 * for new data structures added. */
235 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
236 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
237 err = libcfs_ioctl_data_adjust(data);
244 case IOC_LIBCFS_CLEAR_DEBUG:
245 libcfs_debug_clear_buffer();
248 * case IOC_LIBCFS_PANIC:
249 * Handled in arch/cfs_module.c
251 case IOC_LIBCFS_MARK_DEBUG:
252 if (data->ioc_inlbuf1 == NULL ||
253 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
255 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
257 case IOC_LIBCFS_MEMHOG:
258 if (pfile->private_data == NULL) {
261 kportal_memhog_free(pfile->private_data);
262 /* XXX The ioc_flags is not GFP flags now, need to
264 err = kportal_memhog_alloc(pfile->private_data,
268 kportal_memhog_free(pfile->private_data);
272 case IOC_LIBCFS_PING_TEST: {
273 extern void (kping_client)(struct libcfs_ioctl_data *);
274 void (*ping)(struct libcfs_ioctl_data *);
276 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
277 data->ioc_count, libcfs_nid2str(data->ioc_nid),
278 libcfs_nid2str(data->ioc_nid));
279 ping = symbol_get(kping_client);
281 CERROR("symbol_get failed\n");
284 symbol_put(kping_client);
290 struct libcfs_ioctl_handler *hand;
293 down_read(&ioctl_list_sem);
294 list_for_each_entry(hand, &ioctl_list, item) {
295 err = hand->handle_ioctl(cmd, hdr);
296 if (err != -EINVAL) {
298 err = libcfs_ioctl_popdata(arg,
303 up_read(&ioctl_list_sem);
311 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
312 unsigned long cmd, void *arg)
314 struct libcfs_ioctl_hdr *hdr;
319 err = libcfs_ioctl_getdata_len(arg, &buf_len);
324 * do a check here to restrict the size of the memory
325 * to allocate to guard against DoS attacks.
327 if (buf_len > LNET_MAX_IOCTL_BUF_LEN) {
328 CERROR("LNET: user buffer exceeds kernel buffer\n");
332 LIBCFS_ALLOC_GFP(hdr, buf_len, GFP_IOFS);
336 /* 'cmd' and permissions get checked in our arch-specific caller */
337 if (libcfs_ioctl_getdata(hdr, buf_len, arg)) {
338 CERROR("LNET ioctl: data error\n");
339 GOTO(out, err = -EINVAL);
342 err = libcfs_ioctl_handle(pfile, cmd, arg, hdr);
345 LIBCFS_FREE(hdr, buf_len);
350 struct cfs_psdev_ops libcfs_psdev_ops = {
352 libcfs_psdev_release,
358 extern int insert_proc(void);
359 extern void remove_proc(void);
360 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
361 MODULE_DESCRIPTION("Portals v3.1");
362 MODULE_LICENSE("GPL");
364 extern struct miscdevice libcfs_dev;
365 extern struct rw_semaphore cfs_tracefile_sem;
366 extern struct mutex cfs_trace_thread_mutex;
367 extern struct cfs_wi_sched *cfs_sched_rehash;
369 extern void libcfs_init_nidstrings(void);
370 extern int libcfs_arch_init(void);
371 extern void libcfs_arch_cleanup(void);
373 static int init_libcfs_module(void)
378 libcfs_init_nidstrings();
379 init_rwsem(&cfs_tracefile_sem);
380 mutex_init(&cfs_trace_thread_mutex);
381 init_rwsem(&ioctl_list_sem);
382 INIT_LIST_HEAD(&ioctl_list);
383 init_waitqueue_head(&cfs_race_waitq);
385 rc = libcfs_debug_init(5 * 1024 * 1024);
387 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
395 rc = misc_register(&libcfs_dev);
397 CERROR("misc_register: error %d\n", rc);
401 rc = cfs_wi_startup();
403 CERROR("initialize workitem: error %d\n", rc);
404 goto cleanup_deregister;
407 /* max to 4 threads, should be enough for rehash */
408 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
409 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
410 rc, &cfs_sched_rehash);
412 CERROR("Startup workitem scheduler: error: %d\n", rc);
413 goto cleanup_deregister;
416 rc = cfs_crypto_register();
418 CERROR("cfs_crypto_regster: error %d\n", rc);
425 CERROR("insert_proc: error %d\n", rc);
429 CDEBUG (D_OTHER, "portals setup OK\n");
432 cfs_crypto_unregister();
436 misc_deregister(&libcfs_dev);
440 libcfs_debug_cleanup();
444 static void exit_libcfs_module(void)
450 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
451 atomic_read(&libcfs_kmemory));
453 if (cfs_sched_rehash != NULL) {
454 cfs_wi_sched_destroy(cfs_sched_rehash);
455 cfs_sched_rehash = NULL;
458 cfs_crypto_unregister();
461 rc = misc_deregister(&libcfs_dev);
463 CERROR("misc_deregister error %d\n", rc);
467 if (atomic_read(&libcfs_kmemory) != 0)
468 CERROR("Portals memory leaked: %d bytes\n",
469 atomic_read(&libcfs_kmemory));
471 rc = libcfs_debug_cleanup();
473 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
476 fini_rwsem(&ioctl_list_sem);
477 fini_rwsem(&cfs_tracefile_sem);
479 libcfs_arch_cleanup();
482 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);