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>
45 #include "tracefile.h"
48 kportal_memhog_free (struct libcfs_device_userstate *ldu)
50 struct page **level0p = &ldu->ldu_memhog_root_page;
51 struct page **level1p;
52 struct page **level2p;
56 if (*level0p != NULL) {
57 level1p = (struct page **)page_address(*level0p);
60 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
63 level2p = (struct page **)page_address(*level1p);
66 while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
69 __free_page(*level2p);
70 ldu->ldu_memhog_pages--;
75 __free_page(*level1p);
76 ldu->ldu_memhog_pages--;
81 __free_page(*level0p);
82 ldu->ldu_memhog_pages--;
87 LASSERT(ldu->ldu_memhog_pages == 0);
91 kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
94 struct page **level0p;
95 struct page **level1p;
96 struct page **level2p;
100 LASSERT(ldu->ldu_memhog_pages == 0);
101 LASSERT(ldu->ldu_memhog_root_page == NULL);
109 level0p = &ldu->ldu_memhog_root_page;
110 *level0p = alloc_page(flags);
111 if (*level0p == NULL)
113 ldu->ldu_memhog_pages++;
115 level1p = (struct page **)page_address(*level0p);
117 memset(level1p, 0, PAGE_CACHE_SIZE);
119 while (ldu->ldu_memhog_pages < npages &&
120 count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
122 if (cfs_signal_pending())
125 *level1p = alloc_page(flags);
126 if (*level1p == NULL)
128 ldu->ldu_memhog_pages++;
130 level2p = (struct page **)page_address(*level1p);
132 memset(level2p, 0, PAGE_CACHE_SIZE);
134 while (ldu->ldu_memhog_pages < npages &&
135 count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
137 if (cfs_signal_pending())
140 *level2p = alloc_page(flags);
141 if (*level2p == NULL)
143 ldu->ldu_memhog_pages++;
156 /* called when opening /dev/device */
157 static int libcfs_psdev_open(unsigned long flags, void *args)
159 struct libcfs_device_userstate *ldu;
162 try_module_get(THIS_MODULE);
164 LIBCFS_ALLOC(ldu, sizeof(*ldu));
166 ldu->ldu_memhog_pages = 0;
167 ldu->ldu_memhog_root_page = NULL;
169 *(struct libcfs_device_userstate **)args = ldu;
174 /* called when closing /dev/device */
175 static int libcfs_psdev_release(unsigned long flags, void *args)
177 struct libcfs_device_userstate *ldu;
180 ldu = (struct libcfs_device_userstate *)args;
182 kportal_memhog_free(ldu);
183 LIBCFS_FREE(ldu, sizeof(*ldu));
186 module_put(THIS_MODULE);
190 static struct rw_semaphore ioctl_list_sem;
191 static struct list_head ioctl_list;
193 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
197 down_write(&ioctl_list_sem);
198 if (!list_empty(&hand->item))
201 list_add_tail(&hand->item, &ioctl_list);
202 up_write(&ioctl_list_sem);
206 EXPORT_SYMBOL(libcfs_register_ioctl);
208 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
212 down_write(&ioctl_list_sem);
213 if (list_empty(&hand->item))
216 list_del_init(&hand->item);
217 up_write(&ioctl_list_sem);
221 EXPORT_SYMBOL(libcfs_deregister_ioctl);
223 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
224 unsigned long cmd, void __user *uparam)
226 struct libcfs_ioctl_data *data = NULL;
227 struct libcfs_ioctl_hdr *hdr;
231 /* 'cmd' and permissions get checked in our arch-specific caller */
232 err = libcfs_ioctl_getdata(&hdr, uparam);
234 CDEBUG_LIMIT(D_ERROR,
235 "libcfs ioctl: data header error %d\n", err);
239 if (hdr->ioc_version == LIBCFS_IOCTL_VERSION) {
240 /* The libcfs_ioctl_data_adjust() function performs adjustment
241 * operations on the libcfs_ioctl_data structure to make
242 * it usable by the code. This doesn't need to be called
243 * for new data structures added. */
244 data = container_of(hdr, struct libcfs_ioctl_data, ioc_hdr);
245 err = libcfs_ioctl_data_adjust(data);
250 CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
252 case IOC_LIBCFS_CLEAR_DEBUG:
253 libcfs_debug_clear_buffer();
256 * case IOC_LIBCFS_PANIC:
257 * Handled in arch/cfs_module.c
259 case IOC_LIBCFS_MARK_DEBUG:
261 data->ioc_inlbuf1 == NULL ||
262 data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
263 GOTO(out, err = -EINVAL);
265 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
268 case IOC_LIBCFS_MEMHOG:
270 GOTO(out, err = -EINVAL);
272 if (pfile->private_data == NULL)
273 GOTO(out, err = -EINVAL);
275 kportal_memhog_free(pfile->private_data);
276 err = kportal_memhog_alloc(pfile->private_data,
277 data->ioc_count, data->ioc_flags);
279 kportal_memhog_free(pfile->private_data);
283 struct libcfs_ioctl_handler *hand;
286 down_read(&ioctl_list_sem);
287 list_for_each_entry(hand, &ioctl_list, item) {
288 err = hand->handle_ioctl(cmd, hdr);
293 err = libcfs_ioctl_popdata(hdr, uparam);
296 up_read(&ioctl_list_sem);
300 libcfs_ioctl_freedata(hdr);
304 struct cfs_psdev_ops libcfs_psdev_ops = {
306 libcfs_psdev_release,
312 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
313 MODULE_DESCRIPTION("Portals v3.1");
314 MODULE_LICENSE("GPL");
316 static int init_libcfs_module(void)
321 libcfs_init_nidstrings();
322 init_rwsem(&cfs_tracefile_sem);
323 mutex_init(&cfs_trace_thread_mutex);
324 init_rwsem(&ioctl_list_sem);
325 INIT_LIST_HEAD(&ioctl_list);
326 init_waitqueue_head(&cfs_race_waitq);
328 rc = libcfs_debug_init(5 * 1024 * 1024);
330 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
338 rc = misc_register(&libcfs_dev);
340 CERROR("misc_register: error %d\n", rc);
344 rc = cfs_wi_startup();
346 CERROR("initialize workitem: error %d\n", rc);
347 goto cleanup_deregister;
350 /* max to 4 threads, should be enough for rehash */
351 rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
352 rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
353 rc, &cfs_sched_rehash);
355 CERROR("Startup workitem scheduler: error: %d\n", rc);
356 goto cleanup_deregister;
359 rc = cfs_crypto_register();
361 CERROR("cfs_crypto_regster: error %d\n", rc);
368 CERROR("insert_proc: error %d\n", rc);
372 CDEBUG (D_OTHER, "portals setup OK\n");
375 cfs_crypto_unregister();
379 misc_deregister(&libcfs_dev);
383 libcfs_debug_cleanup();
387 static void exit_libcfs_module(void)
393 CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
394 atomic_read(&libcfs_kmemory));
396 if (cfs_sched_rehash != NULL) {
397 cfs_wi_sched_destroy(cfs_sched_rehash);
398 cfs_sched_rehash = NULL;
401 cfs_crypto_unregister();
404 rc = misc_deregister(&libcfs_dev);
406 CERROR("misc_deregister error %d\n", rc);
410 if (atomic_read(&libcfs_kmemory) != 0)
411 CERROR("Portals memory leaked: %d bytes\n",
412 atomic_read(&libcfs_kmemory));
414 rc = libcfs_debug_cleanup();
416 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
419 fini_rwsem(&ioctl_list_sem);
420 fini_rwsem(&cfs_tracefile_sem);
422 libcfs_arch_cleanup();
425 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);