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, 2014, 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>
44 kportal_memhog_free (struct libcfs_device_userstate *ldu)
46 struct page **level0p = &ldu->ldu_memhog_root_page;
47 struct page **level1p;
48 struct page **level2p;
52 if (*level0p != NULL) {
53 level1p = (struct page **)page_address(*level0p);
56 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
59 level2p = (struct page **)page_address(*level1p);
62 while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
65 __free_page(*level2p);
66 ldu->ldu_memhog_pages--;
71 __free_page(*level1p);
72 ldu->ldu_memhog_pages--;
77 __free_page(*level0p);
78 ldu->ldu_memhog_pages--;
83 LASSERT(ldu->ldu_memhog_pages == 0);
87 kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
90 struct page **level0p;
91 struct page **level1p;
92 struct page **level2p;
96 LASSERT(ldu->ldu_memhog_pages == 0);
97 LASSERT(ldu->ldu_memhog_root_page == NULL);
105 level0p = &ldu->ldu_memhog_root_page;
106 *level0p = alloc_page(flags);
107 if (*level0p == NULL)
109 ldu->ldu_memhog_pages++;
111 level1p = (struct page **)page_address(*level0p);
113 memset(level1p, 0, PAGE_CACHE_SIZE);
115 while (ldu->ldu_memhog_pages < npages &&
116 count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
118 if (signal_pending(current))
121 *level1p = alloc_page(flags);
122 if (*level1p == NULL)
124 ldu->ldu_memhog_pages++;
126 level2p = (struct page **)page_address(*level1p);
128 memset(level2p, 0, PAGE_CACHE_SIZE);
130 while (ldu->ldu_memhog_pages < npages &&
131 count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
133 if (signal_pending(current))
136 *level2p = alloc_page(flags);
137 if (*level2p == NULL)
139 ldu->ldu_memhog_pages++;
152 /* called when opening /dev/device */
153 static int libcfs_psdev_open(unsigned long flags, void *args)
155 struct libcfs_device_userstate *ldu;
158 try_module_get(THIS_MODULE);
160 LIBCFS_ALLOC(ldu, sizeof(*ldu));
162 ldu->ldu_memhog_pages = 0;
163 ldu->ldu_memhog_root_page = NULL;
165 *(struct libcfs_device_userstate **)args = ldu;
170 /* called when closing /dev/device */
171 static int libcfs_psdev_release(unsigned long flags, void *args)
173 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 DECLARE_RWSEM(ioctl_list_sem);
187 static 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(struct cfs_psdev_file *pfile,
220 unsigned long cmd, void __user *uparam)
222 struct libcfs_ioctl_data *data = NULL;
223 struct libcfs_ioctl_hdr *hdr;
227 /* 'cmd' and permissions get checked in our arch-specific caller */
228 err = libcfs_ioctl_getdata(&hdr, uparam);
230 CDEBUG_LIMIT(D_ERROR,
231 "libcfs ioctl: data header error %d\n", err);
235 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
236 /* The libcfs_ioctl_data_adjust() function performs adjustment
237 * operations on the libcfs_ioctl_data structure to make
238 * it usable by the code. This doesn't need to be called
239 * for new data structures added. */
240 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
241 err = libcfs_ioctl_data_adjust(data);
246 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
248 case IOC_LIBCFS_CLEAR_DEBUG:
249 libcfs_debug_clear_buffer();
252 * case IOC_LIBCFS_PANIC:
253 * Handled in arch/cfs_module.c
255 case IOC_LIBCFS_MARK_DEBUG:
257 data->ioc_inlbuf1 == NULL ||
258 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
259 GOTO(out, err = -EINVAL);
261 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
264 case IOC_LIBCFS_MEMHOG:
266 GOTO(out, err = -EINVAL);
268 if (pfile->private_data == NULL)
269 GOTO(out, err = -EINVAL);
271 kportal_memhog_free(pfile->private_data);
272 err = kportal_memhog_alloc(pfile->private_data,
273 data->ioc_count, data->ioc_flags);
275 kportal_memhog_free(pfile->private_data);
279 struct libcfs_ioctl_handler *hand;
282 down_read(&ioctl_list_sem);
283 list_for_each_entry(hand, &ioctl_list, item) {
284 err = hand->handle_ioctl(cmd, hdr);
289 if (copy_to_user(uparam, hdr, hdr->ioc_len))
294 up_read(&ioctl_list_sem);
298 LIBCFS_FREE(hdr, hdr->ioc_len);
302 struct cfs_psdev_ops libcfs_psdev_ops = {
304 libcfs_psdev_release,
310 static int init_libcfs_module(void)
314 rc = libcfs_debug_init(5 * 1024 * 1024);
316 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
324 rc = misc_register(&libcfs_dev);
326 CERROR("misc_register: error %d\n", rc);
330 rc = cfs_wi_startup();
332 CERROR("initialize workitem: error %d\n", rc);
333 goto cleanup_deregister;
336 /* max to 4 threads, should be enough for rehash */
337 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
338 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
339 rc, &cfs_sched_rehash);
341 CERROR("Startup workitem scheduler: error: %d\n", rc);
342 goto cleanup_deregister;
345 rc = cfs_crypto_register();
347 CERROR("cfs_crypto_regster: error %d\n", rc);
354 CERROR("insert_proc: error %d\n", rc);
358 CDEBUG (D_OTHER, "portals setup OK\n");
361 cfs_crypto_unregister();
365 misc_deregister(&libcfs_dev);
369 libcfs_debug_cleanup();
373 static void exit_libcfs_module(void)
379 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
380 atomic_read(&libcfs_kmemory));
382 if (cfs_sched_rehash != NULL) {
383 cfs_wi_sched_destroy(cfs_sched_rehash);
384 cfs_sched_rehash = NULL;
387 cfs_crypto_unregister();
390 rc = misc_deregister(&libcfs_dev);
392 CERROR("misc_deregister error %d\n", rc);
396 if (atomic_read(&libcfs_kmemory) != 0)
397 CERROR("Portals memory leaked: %d bytes\n",
398 atomic_read(&libcfs_kmemory));
400 rc = libcfs_debug_cleanup();
402 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
406 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
407 MODULE_DESCRIPTION("Libcfs v3.1");
408 MODULE_VERSION("1.0.0");
409 MODULE_LICENSE("GPL");
411 module_init(init_libcfs_module);
412 module_exit(exit_libcfs_module);