staging: lustre: libcfs: fix sparse warnings about static declaration
[firefly-linux-kernel-4.4.55.git] / drivers / staging / lustre / lustre / 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, 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 "../../include/linux/libcfs/libcfs.h"
40 #include "../../include/linux/libcfs/libcfs_crypto.h"
41 #include "../../include/linux/lnet/lib-lnet.h"
42 #include "../../include/linux/lnet/lnet.h"
43 #include "tracefile.h"
44
45 static void kportal_memhog_free (struct libcfs_device_userstate *ldu)
46 {
47         struct page **level0p = &ldu->ldu_memhog_root_page;
48         struct page **level1p;
49         struct page **level2p;
50         int        count1;
51         int        count2;
52
53         if (*level0p != NULL) {
54
55                 level1p = (struct page **)page_address(*level0p);
56                 count1 = 0;
57
58                 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
59                        *level1p != NULL) {
60
61                         level2p = (struct page **)page_address(*level1p);
62                         count2 = 0;
63
64                         while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
65                                *level2p != NULL) {
66
67                                 __free_page(*level2p);
68                                 ldu->ldu_memhog_pages--;
69                                 level2p++;
70                                 count2++;
71                         }
72
73                         __free_page(*level1p);
74                         ldu->ldu_memhog_pages--;
75                         level1p++;
76                         count1++;
77                 }
78
79                 __free_page(*level0p);
80                 ldu->ldu_memhog_pages--;
81
82                 *level0p = NULL;
83         }
84
85         LASSERT (ldu->ldu_memhog_pages == 0);
86 }
87
88 static int kportal_memhog_alloc(struct libcfs_device_userstate *ldu, int npages,
89                      gfp_t flags)
90 {
91         struct page **level0p;
92         struct page **level1p;
93         struct page **level2p;
94         int        count1;
95         int        count2;
96
97         LASSERT (ldu->ldu_memhog_pages == 0);
98         LASSERT (ldu->ldu_memhog_root_page == NULL);
99
100         if (npages < 0)
101                 return -EINVAL;
102
103         if (npages == 0)
104                 return 0;
105
106         level0p = &ldu->ldu_memhog_root_page;
107         *level0p = alloc_page(flags);
108         if (*level0p == NULL)
109                 return -ENOMEM;
110         ldu->ldu_memhog_pages++;
111
112         level1p = (struct page **)page_address(*level0p);
113         count1 = 0;
114         memset(level1p, 0, PAGE_CACHE_SIZE);
115
116         while (ldu->ldu_memhog_pages < npages &&
117                count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
118
119                 if (cfs_signal_pending())
120                         return -EINTR;
121
122                 *level1p = alloc_page(flags);
123                 if (*level1p == NULL)
124                         return -ENOMEM;
125                 ldu->ldu_memhog_pages++;
126
127                 level2p = (struct page **)page_address(*level1p);
128                 count2 = 0;
129                 memset(level2p, 0, PAGE_CACHE_SIZE);
130
131                 while (ldu->ldu_memhog_pages < npages &&
132                        count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
133
134                         if (cfs_signal_pending())
135                                 return -EINTR;
136
137                         *level2p = alloc_page(flags);
138                         if (*level2p == NULL)
139                                 return -ENOMEM;
140                         ldu->ldu_memhog_pages++;
141
142                         level2p++;
143                         count2++;
144                 }
145
146                 level1p++;
147                 count1++;
148         }
149
150         return 0;
151 }
152
153 /* called when opening /dev/device */
154 static int libcfs_psdev_open(unsigned long flags, void *args)
155 {
156         struct libcfs_device_userstate *ldu;
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
175         ldu = (struct libcfs_device_userstate *)args;
176         if (ldu != NULL) {
177                 kportal_memhog_free(ldu);
178                 LIBCFS_FREE(ldu, sizeof(*ldu));
179         }
180
181         module_put(THIS_MODULE);
182         return 0;
183 }
184
185 static struct rw_semaphore ioctl_list_sem;
186 static struct list_head ioctl_list;
187
188 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand)
189 {
190         int rc = 0;
191
192         down_write(&ioctl_list_sem);
193         if (!list_empty(&hand->item))
194                 rc = -EBUSY;
195         else
196                 list_add_tail(&hand->item, &ioctl_list);
197         up_write(&ioctl_list_sem);
198
199         return rc;
200 }
201 EXPORT_SYMBOL(libcfs_register_ioctl);
202
203 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand)
204 {
205         int rc = 0;
206
207         down_write(&ioctl_list_sem);
208         if (list_empty(&hand->item))
209                 rc = -ENOENT;
210         else
211                 list_del_init(&hand->item);
212         up_write(&ioctl_list_sem);
213
214         return rc;
215 }
216 EXPORT_SYMBOL(libcfs_deregister_ioctl);
217
218 static int libcfs_ioctl_int(struct cfs_psdev_file *pfile, unsigned long cmd,
219                             void *arg, struct libcfs_ioctl_data *data)
220 {
221         int err = -EINVAL;
222
223         switch (cmd) {
224         case IOC_LIBCFS_CLEAR_DEBUG:
225                 libcfs_debug_clear_buffer();
226                 return 0;
227         /*
228          * case IOC_LIBCFS_PANIC:
229          * Handled in arch/cfs_module.c
230          */
231         case IOC_LIBCFS_MARK_DEBUG:
232                 if (data->ioc_inlbuf1 == NULL ||
233                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
234                         return -EINVAL;
235                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
236                 return 0;
237         case IOC_LIBCFS_MEMHOG:
238                 if (pfile->private_data == NULL) {
239                         err = -EINVAL;
240                 } else {
241                         kportal_memhog_free(pfile->private_data);
242                         /* XXX The ioc_flags is not GFP flags now, need to be fixed */
243                         err = kportal_memhog_alloc(pfile->private_data,
244                                                    data->ioc_count,
245                                                    data->ioc_flags);
246                         if (err != 0)
247                                 kportal_memhog_free(pfile->private_data);
248                 }
249                 break;
250
251         case IOC_LIBCFS_PING_TEST: {
252                 extern void (kping_client)(struct libcfs_ioctl_data *);
253                 void (*ping)(struct libcfs_ioctl_data *);
254
255                 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
256                        data->ioc_count, libcfs_nid2str(data->ioc_nid),
257                        libcfs_nid2str(data->ioc_nid));
258                 ping = symbol_get(kping_client);
259                 if (!ping)
260                         CERROR("symbol_get failed\n");
261                 else {
262                         ping(data);
263                         symbol_put(kping_client);
264                 }
265                 return 0;
266         }
267
268         default: {
269                 struct libcfs_ioctl_handler *hand;
270                 err = -EINVAL;
271                 down_read(&ioctl_list_sem);
272                 list_for_each_entry(hand, &ioctl_list, item) {
273                         err = hand->handle_ioctl(cmd, data);
274                         if (err != -EINVAL) {
275                                 if (err == 0)
276                                         err = libcfs_ioctl_popdata(arg,
277                                                         data, sizeof (*data));
278                                 break;
279                         }
280                 }
281                 up_read(&ioctl_list_sem);
282                 break;
283         }
284         }
285
286         return err;
287 }
288
289 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
290 {
291         char    *buf;
292         struct libcfs_ioctl_data *data;
293         int err = 0;
294
295         LIBCFS_ALLOC_GFP(buf, 1024, GFP_IOFS);
296         if (buf == NULL)
297                 return -ENOMEM;
298
299         /* 'cmd' and permissions get checked in our arch-specific caller */
300         if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
301                 CERROR("PORTALS ioctl: data error\n");
302                 err = -EINVAL;
303                 goto out;
304         }
305         data = (struct libcfs_ioctl_data *)buf;
306
307         err = libcfs_ioctl_int(pfile, cmd, arg, data);
308
309 out:
310         LIBCFS_FREE(buf, 1024);
311         return err;
312 }
313
314
315 struct cfs_psdev_ops libcfs_psdev_ops = {
316         libcfs_psdev_open,
317         libcfs_psdev_release,
318         NULL,
319         NULL,
320         libcfs_ioctl
321 };
322
323 extern int insert_proc(void);
324 extern void remove_proc(void);
325 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
326 MODULE_DESCRIPTION("Portals v3.1");
327 MODULE_LICENSE("GPL");
328
329 extern struct miscdevice libcfs_dev;
330 extern struct rw_semaphore cfs_tracefile_sem;
331 extern struct mutex cfs_trace_thread_mutex;
332 extern struct cfs_wi_sched *cfs_sched_rehash;
333
334 extern void libcfs_init_nidstrings(void);
335 extern int libcfs_arch_init(void);
336 extern void libcfs_arch_cleanup(void);
337
338 static int init_libcfs_module(void)
339 {
340         int rc;
341
342         libcfs_arch_init();
343         libcfs_init_nidstrings();
344         init_rwsem(&cfs_tracefile_sem);
345         mutex_init(&cfs_trace_thread_mutex);
346         init_rwsem(&ioctl_list_sem);
347         INIT_LIST_HEAD(&ioctl_list);
348         init_waitqueue_head(&cfs_race_waitq);
349
350         rc = libcfs_debug_init(5 * 1024 * 1024);
351         if (rc < 0) {
352                 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
353                 return rc;
354         }
355
356         rc = cfs_cpu_init();
357         if (rc != 0)
358                 goto cleanup_debug;
359
360         rc = misc_register(&libcfs_dev);
361         if (rc) {
362                 CERROR("misc_register: error %d\n", rc);
363                 goto cleanup_cpu;
364         }
365
366         rc = cfs_wi_startup();
367         if (rc) {
368                 CERROR("initialize workitem: error %d\n", rc);
369                 goto cleanup_deregister;
370         }
371
372         /* max to 4 threads, should be enough for rehash */
373         rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
374         rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
375                                  rc, &cfs_sched_rehash);
376         if (rc != 0) {
377                 CERROR("Startup workitem scheduler: error: %d\n", rc);
378                 goto cleanup_deregister;
379         }
380
381         rc = cfs_crypto_register();
382         if (rc) {
383                 CERROR("cfs_crypto_register: error %d\n", rc);
384                 goto cleanup_wi;
385         }
386
387
388         rc = insert_proc();
389         if (rc) {
390                 CERROR("insert_proc: error %d\n", rc);
391                 goto cleanup_crypto;
392         }
393
394         CDEBUG (D_OTHER, "portals setup OK\n");
395         return 0;
396  cleanup_crypto:
397         cfs_crypto_unregister();
398  cleanup_wi:
399         cfs_wi_shutdown();
400  cleanup_deregister:
401         misc_deregister(&libcfs_dev);
402 cleanup_cpu:
403         cfs_cpu_fini();
404  cleanup_debug:
405         libcfs_debug_cleanup();
406         return rc;
407 }
408
409 static void exit_libcfs_module(void)
410 {
411         int rc;
412
413         remove_proc();
414
415         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
416                atomic_read(&libcfs_kmemory));
417
418         if (cfs_sched_rehash != NULL) {
419                 cfs_wi_sched_destroy(cfs_sched_rehash);
420                 cfs_sched_rehash = NULL;
421         }
422
423         cfs_crypto_unregister();
424         cfs_wi_shutdown();
425
426         rc = misc_deregister(&libcfs_dev);
427         if (rc)
428                 CERROR("misc_deregister error %d\n", rc);
429
430         cfs_cpu_fini();
431
432         if (atomic_read(&libcfs_kmemory) != 0)
433                 CERROR("Portals memory leaked: %d bytes\n",
434                        atomic_read(&libcfs_kmemory));
435
436         rc = libcfs_debug_cleanup();
437         if (rc)
438                 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
439                        rc);
440
441         libcfs_arch_cleanup();
442 }
443
444 MODULE_VERSION("1.0.0");
445 module_init(init_libcfs_module);
446 module_exit(exit_libcfs_module);