Whamcloud - gitweb
LU-6204 build: clean up kernel module metadata
[fs/lustre-release.git] / libcfs / libcfs / module.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38
39 #include <libcfs/libcfs.h>
40 #include <libcfs/libcfs_crypto.h>
41 #include <lnet/lib-lnet.h>
42
43 static void
44 kportal_memhog_free (struct libcfs_device_userstate *ldu)
45 {
46         struct page **level0p = &ldu->ldu_memhog_root_page;
47         struct page **level1p;
48         struct page **level2p;
49         int           count1;
50         int           count2;
51
52         if (*level0p != NULL) {
53                 level1p = (struct page **)page_address(*level0p);
54                 count1 = 0;
55
56                 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
57                        *level1p != NULL) {
58
59                         level2p = (struct page **)page_address(*level1p);
60                         count2 = 0;
61
62                         while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
63                                *level2p != NULL) {
64
65                                 __free_page(*level2p);
66                                 ldu->ldu_memhog_pages--;
67                                 level2p++;
68                                 count2++;
69                         }
70
71                         __free_page(*level1p);
72                         ldu->ldu_memhog_pages--;
73                         level1p++;
74                         count1++;
75                 }
76
77                 __free_page(*level0p);
78                 ldu->ldu_memhog_pages--;
79
80                 *level0p = NULL;
81         }
82
83         LASSERT(ldu->ldu_memhog_pages == 0);
84 }
85
86 static int
87 kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
88                      gfp_t flags)
89 {
90         struct page **level0p;
91         struct page **level1p;
92         struct page **level2p;
93         int           count1;
94         int           count2;
95
96         LASSERT(ldu->ldu_memhog_pages == 0);
97         LASSERT(ldu->ldu_memhog_root_page == NULL);
98
99         if (npages < 0)
100                 return -EINVAL;
101
102         if (npages == 0)
103                 return 0;
104
105         level0p = &ldu->ldu_memhog_root_page;
106         *level0p = alloc_page(flags);
107         if (*level0p == NULL)
108                 return -ENOMEM;
109         ldu->ldu_memhog_pages++;
110
111         level1p = (struct page **)page_address(*level0p);
112         count1 = 0;
113         memset(level1p, 0, PAGE_CACHE_SIZE);
114
115         while (ldu->ldu_memhog_pages < npages &&
116                count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
117
118                 if (signal_pending(current))
119                         return -EINTR;
120
121                 *level1p = alloc_page(flags);
122                 if (*level1p == NULL)
123                         return -ENOMEM;
124                 ldu->ldu_memhog_pages++;
125
126                 level2p = (struct page **)page_address(*level1p);
127                 count2 = 0;
128                 memset(level2p, 0, PAGE_CACHE_SIZE);
129
130                 while (ldu->ldu_memhog_pages < npages &&
131                        count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
132
133                         if (signal_pending(current))
134                                 return -EINTR;
135
136                         *level2p = alloc_page(flags);
137                         if (*level2p == NULL)
138                                 return -ENOMEM;
139                         ldu->ldu_memhog_pages++;
140
141                         level2p++;
142                         count2++;
143                 }
144
145                 level1p++;
146                 count1++;
147         }
148
149         return 0;
150 }
151
152 /* called when opening /dev/device */
153 static int libcfs_psdev_open(unsigned long flags, void *args)
154 {
155         struct libcfs_device_userstate *ldu;
156         ENTRY;
157
158         try_module_get(THIS_MODULE);
159
160         LIBCFS_ALLOC(ldu, sizeof(*ldu));
161         if (ldu != NULL) {
162                 ldu->ldu_memhog_pages = 0;
163                 ldu->ldu_memhog_root_page = NULL;
164         }
165         *(struct libcfs_device_userstate **)args = ldu;
166
167         RETURN(0);
168 }
169
170 /* called when closing /dev/device */
171 static int libcfs_psdev_release(unsigned long flags, void *args)
172 {
173         struct libcfs_device_userstate *ldu;
174         ENTRY;
175
176         ldu = (struct libcfs_device_userstate *)args;
177         if (ldu != NULL) {
178                 kportal_memhog_free(ldu);
179                 LIBCFS_FREE(ldu, sizeof(*ldu));
180         }
181
182         module_put(THIS_MODULE);
183         RETURN(0);
184 }
185
186 static DECLARE_RWSEM(ioctl_list_sem);
187 static LIST_HEAD(ioctl_list);
188
189 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
190 {
191         int rc = 0;
192
193         down_write(&ioctl_list_sem);
194         if (!list_empty(&hand->item))
195                 rc = -EBUSY;
196         else
197                 list_add_tail(&hand->item, &ioctl_list);
198         up_write(&ioctl_list_sem);
199
200         return rc;
201 }
202 EXPORT_SYMBOL(libcfs_register_ioctl);
203
204 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
205 {
206         int rc = 0;
207
208         down_write(&ioctl_list_sem);
209         if (list_empty(&hand->item))
210                 rc = -ENOENT;
211         else
212                 list_del_init(&hand->item);
213         up_write(&ioctl_list_sem);
214
215         return rc;
216 }
217 EXPORT_SYMBOL(libcfs_deregister_ioctl);
218
219 static int libcfs_ioctl(struct cfs_psdev_file *pfile,
220                         unsigned long cmd, void __user *uparam)
221 {
222         struct libcfs_ioctl_data *data = NULL;
223         struct libcfs_ioctl_hdr  *hdr;
224         int                       err;
225         ENTRY;
226
227         /* 'cmd' and permissions get checked in our arch-specific caller */
228         err = libcfs_ioctl_getdata(&hdr, uparam);
229         if (err != 0) {
230                 CDEBUG_LIMIT(D_ERROR,
231                              "libcfs ioctl: data header error %d\n", err);
232                 RETURN(err);
233         }
234
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);
242                 if (err != 0)
243                         GOTO(out, err);
244         }
245
246         CDEBUG(D_IOCTL, "libcfs ioctl cmd %lu\n", cmd);
247         switch (cmd) {
248         case IOC_LIBCFS_CLEAR_DEBUG:
249                 libcfs_debug_clear_buffer();
250                 break;
251         /*
252          * case IOC_LIBCFS_PANIC:
253          * Handled in arch/cfs_module.c
254          */
255         case IOC_LIBCFS_MARK_DEBUG:
256                 if (data == NULL ||
257                     data->ioc_inlbuf1 == NULL ||
258                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
259                         GOTO(out, err = -EINVAL);
260
261                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
262                 break;
263
264         case IOC_LIBCFS_MEMHOG:
265                 if (data == NULL)
266                         GOTO(out, err = -EINVAL);
267
268                 if (pfile->private_data == NULL)
269                         GOTO(out, err = -EINVAL);
270
271                 kportal_memhog_free(pfile->private_data);
272                 err = kportal_memhog_alloc(pfile->private_data,
273                                            data->ioc_count, data->ioc_flags);
274                 if (err != 0)
275                         kportal_memhog_free(pfile->private_data);
276                 break;
277
278         default: {
279                 struct libcfs_ioctl_handler *hand;
280
281                 err = -EINVAL;
282                 down_read(&ioctl_list_sem);
283                 list_for_each_entry(hand, &ioctl_list, item) {
284                         err = hand->handle_ioctl(cmd, hdr);
285                         if (err == -EINVAL)
286                                 continue;
287
288                         if (err == 0) {
289                                 if (copy_to_user(uparam, hdr, hdr->ioc_len))
290                                         err = -EFAULT;
291                         }
292                         break;
293                 }
294                 up_read(&ioctl_list_sem);
295                 break; }
296         }
297 out:
298         LIBCFS_FREE(hdr, hdr->ioc_len);
299         RETURN(err);
300 }
301
302 struct cfs_psdev_ops libcfs_psdev_ops = {
303         libcfs_psdev_open,
304         libcfs_psdev_release,
305         NULL,
306         NULL,
307         libcfs_ioctl
308 };
309
310 static int __init libcfs_init(void)
311 {
312         int rc;
313
314         rc = libcfs_debug_init(5 * 1024 * 1024);
315         if (rc < 0) {
316                 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
317                 return (rc);
318         }
319
320         rc = cfs_cpu_init();
321         if (rc != 0)
322                 goto cleanup_debug;
323
324         rc = misc_register(&libcfs_dev);
325         if (rc) {
326                 CERROR("misc_register: error %d\n", rc);
327                 goto cleanup_cpu;
328         }
329
330         rc = cfs_wi_startup();
331         if (rc) {
332                 CERROR("initialize workitem: error %d\n", rc);
333                 goto cleanup_deregister;
334         }
335
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);
340         if (rc != 0) {
341                 CERROR("Startup workitem scheduler: error: %d\n", rc);
342                 goto cleanup_deregister;
343         }
344
345         rc = cfs_crypto_register();
346         if (rc) {
347                 CERROR("cfs_crypto_regster: error %d\n", rc);
348                 goto cleanup_wi;
349         }
350
351
352         rc = insert_proc();
353         if (rc) {
354                 CERROR("insert_proc: error %d\n", rc);
355                 goto cleanup_crypto;
356         }
357
358         CDEBUG (D_OTHER, "portals setup OK\n");
359         return 0;
360 cleanup_crypto:
361         cfs_crypto_unregister();
362 cleanup_wi:
363         cfs_wi_shutdown();
364 cleanup_deregister:
365         misc_deregister(&libcfs_dev);
366 cleanup_cpu:
367         cfs_cpu_fini();
368 cleanup_debug:
369         libcfs_debug_cleanup();
370         return rc;
371 }
372
373 static void __exit libcfs_exit(void)
374 {
375         int rc;
376
377         remove_proc();
378
379         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
380                atomic_read(&libcfs_kmemory));
381
382         if (cfs_sched_rehash != NULL) {
383                 cfs_wi_sched_destroy(cfs_sched_rehash);
384                 cfs_sched_rehash = NULL;
385         }
386
387         cfs_crypto_unregister();
388         cfs_wi_shutdown();
389
390         misc_deregister(&libcfs_dev);
391
392         cfs_cpu_fini();
393
394         if (atomic_read(&libcfs_kmemory) != 0)
395                 CERROR("Portals memory leaked: %d bytes\n",
396                        atomic_read(&libcfs_kmemory));
397
398         rc = libcfs_debug_cleanup();
399         if (rc)
400                 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
401                        rc);
402 }
403
404 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
405 MODULE_DESCRIPTION("Lustre helper library");
406 MODULE_VERSION(LIBCFS_VERSION);
407 MODULE_LICENSE("GPL");
408
409 module_init(libcfs_init);
410 module_exit(libcfs_exit);