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>
42 #include <lnet/lib-dlc.h>
43 #include <lnet/lnet.h>
44 #include <lnet/nidstr.h>
47 kportal_memhog_free (struct libcfs_device_userstate *ldu)
49 struct page **level0p = &ldu->ldu_memhog_root_page;
50 struct page **level1p;
51 struct page **level2p;
55 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;
161 try_module_get(THIS_MODULE);
163 LIBCFS_ALLOC(ldu, sizeof(*ldu));
165 ldu->ldu_memhog_pages = 0;
166 ldu->ldu_memhog_root_page = NULL;
168 *(struct libcfs_device_userstate **)args = ldu;
173 /* called when closing /dev/device */
174 static int libcfs_psdev_release(unsigned long flags, void *args)
176 struct libcfs_device_userstate *ldu;
179 ldu = (struct libcfs_device_userstate *)args;
181 kportal_memhog_free(ldu);
182 LIBCFS_FREE(ldu, sizeof(*ldu));
185 module_put(THIS_MODULE);
189 static DECLARE_RWSEM(ioctl_list_sem);
190 static LIST_HEAD(ioctl_list);
192 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
196 down_write(&ioctl_list_sem);
197 if (!list_empty(&hand->item))
200 list_add_tail(&hand->item, &ioctl_list);
201 up_write(&ioctl_list_sem);
205 EXPORT_SYMBOL(libcfs_register_ioctl);
207 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
211 down_write(&ioctl_list_sem);
212 if (list_empty(&hand->item))
215 list_del_init(&hand->item);
216 up_write(&ioctl_list_sem);
220 EXPORT_SYMBOL(libcfs_deregister_ioctl);
222 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
223 unsigned long cmd, void __user *uparam)
225 struct libcfs_ioctl_data *data = NULL;
226 struct libcfs_ioctl_hdr *hdr;
230 /* 'cmd' and permissions get checked in our arch-specific caller */
231 err = libcfs_ioctl_getdata(&hdr, uparam);
233 CDEBUG_LIMIT(D_ERROR,
234 "libcfs ioctl: data header error %d\n", err);
238 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
239 /* The libcfs_ioctl_data_adjust() function performs adjustment
240 * operations on the libcfs_ioctl_data structure to make
241 * it usable by the code. This doesn't need to be called
242 * for new data structures added. */
243 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
244 err = libcfs_ioctl_data_adjust(data);
249 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
251 case IOC_LIBCFS_CLEAR_DEBUG:
252 libcfs_debug_clear_buffer();
255 * case IOC_LIBCFS_PANIC:
256 * Handled in arch/cfs_module.c
258 case IOC_LIBCFS_MARK_DEBUG:
260 data->ioc_inlbuf1 == NULL ||
261 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
262 GOTO(out, err = -EINVAL);
264 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
267 case IOC_LIBCFS_MEMHOG:
269 GOTO(out, err = -EINVAL);
271 if (pfile->private_data == NULL)
272 GOTO(out, err = -EINVAL);
274 kportal_memhog_free(pfile->private_data);
275 err = kportal_memhog_alloc(pfile->private_data,
276 data->ioc_count, data->ioc_flags);
278 kportal_memhog_free(pfile->private_data);
282 struct libcfs_ioctl_handler *hand;
285 down_read(&ioctl_list_sem);
286 list_for_each_entry(hand, &ioctl_list, item) {
287 err = hand->handle_ioctl(cmd, hdr);
292 err = libcfs_ioctl_popdata(hdr, uparam);
295 up_read(&ioctl_list_sem);
299 libcfs_ioctl_freedata(hdr);
303 struct cfs_psdev_ops libcfs_psdev_ops = {
305 libcfs_psdev_release,
311 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
312 MODULE_DESCRIPTION("Portals v3.1");
313 MODULE_LICENSE("GPL");
315 static int init_libcfs_module(void)
321 rc = libcfs_debug_init(5 * 1024 * 1024);
323 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
331 rc = misc_register(&libcfs_dev);
333 CERROR("misc_register: error %d\n", rc);
337 rc = cfs_wi_startup();
339 CERROR("initialize workitem: error %d\n", rc);
340 goto cleanup_deregister;
343 /* max to 4 threads, should be enough for rehash */
344 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
345 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
346 rc, &cfs_sched_rehash);
348 CERROR("Startup workitem scheduler: error: %d\n", rc);
349 goto cleanup_deregister;
352 rc = cfs_crypto_register();
354 CERROR("cfs_crypto_regster: error %d\n", rc);
361 CERROR("insert_proc: error %d\n", rc);
365 CDEBUG (D_OTHER, "portals setup OK\n");
368 cfs_crypto_unregister();
372 misc_deregister(&libcfs_dev);
376 libcfs_debug_cleanup();
380 static void exit_libcfs_module(void)
386 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
387 atomic_read(&libcfs_kmemory));
389 if (cfs_sched_rehash != NULL) {
390 cfs_wi_sched_destroy(cfs_sched_rehash);
391 cfs_sched_rehash = NULL;
394 cfs_crypto_unregister();
397 rc = misc_deregister(&libcfs_dev);
399 CERROR("misc_deregister error %d\n", rc);
403 if (atomic_read(&libcfs_kmemory) != 0)
404 CERROR("Portals memory leaked: %d bytes\n",
405 atomic_read(&libcfs_kmemory));
407 rc = libcfs_debug_cleanup();
409 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
412 libcfs_arch_cleanup();
415 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);