]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/libcfs/module.c
Merge branch 'timers/core' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
[~andy/linux] / 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 <linux/libcfs/libcfs.h>
40 #include <linux/libcfs/libcfs_crypto.h>
41 #include <linux/lnet/lib-lnet.h>
42 #include <linux/lnet/lnet.h>
43 #include "tracefile.h"
44
45 void
46 kportal_memhog_free (struct libcfs_device_userstate *ldu)
47 {
48         struct page **level0p = &ldu->ldu_memhog_root_page;
49         struct page **level1p;
50         struct page **level2p;
51         int        count1;
52         int        count2;
53
54         if (*level0p != NULL) {
55
56                 level1p = (struct page **)page_address(*level0p);
57                 count1 = 0;
58
59                 while (count1 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
60                        *level1p != NULL) {
61
62                         level2p = (struct page **)page_address(*level1p);
63                         count2 = 0;
64
65                         while (count2 < PAGE_CACHE_SIZE/sizeof(struct page *) &&
66                                *level2p != NULL) {
67
68                                 __free_page(*level2p);
69                                 ldu->ldu_memhog_pages--;
70                                 level2p++;
71                                 count2++;
72                         }
73
74                         __free_page(*level1p);
75                         ldu->ldu_memhog_pages--;
76                         level1p++;
77                         count1++;
78                 }
79
80                 __free_page(*level0p);
81                 ldu->ldu_memhog_pages--;
82
83                 *level0p = NULL;
84         }
85
86         LASSERT (ldu->ldu_memhog_pages == 0);
87 }
88
89 int
90 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
91 {
92         struct page **level0p;
93         struct page **level1p;
94         struct page **level2p;
95         int        count1;
96         int        count2;
97
98         LASSERT (ldu->ldu_memhog_pages == 0);
99         LASSERT (ldu->ldu_memhog_root_page == NULL);
100
101         if (npages < 0)
102                 return -EINVAL;
103
104         if (npages == 0)
105                 return 0;
106
107         level0p = &ldu->ldu_memhog_root_page;
108         *level0p = alloc_page(flags);
109         if (*level0p == NULL)
110                 return -ENOMEM;
111         ldu->ldu_memhog_pages++;
112
113         level1p = (struct page **)page_address(*level0p);
114         count1 = 0;
115         memset(level1p, 0, PAGE_CACHE_SIZE);
116
117         while (ldu->ldu_memhog_pages < npages &&
118                count1 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
119
120                 if (cfs_signal_pending())
121                         return (-EINTR);
122
123                 *level1p = alloc_page(flags);
124                 if (*level1p == NULL)
125                         return -ENOMEM;
126                 ldu->ldu_memhog_pages++;
127
128                 level2p = (struct page **)page_address(*level1p);
129                 count2 = 0;
130                 memset(level2p, 0, PAGE_CACHE_SIZE);
131
132                 while (ldu->ldu_memhog_pages < npages &&
133                        count2 < PAGE_CACHE_SIZE/sizeof(struct page *)) {
134
135                         if (cfs_signal_pending())
136                                 return (-EINTR);
137
138                         *level2p = alloc_page(flags);
139                         if (*level2p == NULL)
140                                 return (-ENOMEM);
141                         ldu->ldu_memhog_pages++;
142
143                         level2p++;
144                         count2++;
145                 }
146
147                 level1p++;
148                 count1++;
149         }
150
151         return 0;
152 }
153
154 /* called when opening /dev/device */
155 static int libcfs_psdev_open(unsigned long flags, void *args)
156 {
157         struct libcfs_device_userstate *ldu;
158
159         try_module_get(THIS_MODULE);
160
161         LIBCFS_ALLOC(ldu, sizeof(*ldu));
162         if (ldu != NULL) {
163                 ldu->ldu_memhog_pages = 0;
164                 ldu->ldu_memhog_root_page = NULL;
165         }
166         *(struct libcfs_device_userstate **)args = ldu;
167
168         return 0;
169 }
170
171 /* called when closing /dev/device */
172 static int libcfs_psdev_release(unsigned long flags, void *args)
173 {
174         struct libcfs_device_userstate *ldu;
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 struct rw_semaphore ioctl_list_sem;
187 static struct 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_int(struct cfs_psdev_file *pfile,unsigned long cmd,
220                             void *arg, struct libcfs_ioctl_data *data)
221 {
222         int err = -EINVAL;
223
224         switch (cmd) {
225         case IOC_LIBCFS_CLEAR_DEBUG:
226                 libcfs_debug_clear_buffer();
227                 return 0;
228         /*
229          * case IOC_LIBCFS_PANIC:
230          * Handled in arch/cfs_module.c
231          */
232         case IOC_LIBCFS_MARK_DEBUG:
233                 if (data->ioc_inlbuf1 == NULL ||
234                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
235                         return -EINVAL;
236                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
237                 return 0;
238 #if LWT_SUPPORT
239         case IOC_LIBCFS_LWT_CONTROL:
240                 err = lwt_control ((data->ioc_flags & 1) != 0,
241                                    (data->ioc_flags & 2) != 0);
242                 break;
243
244         case IOC_LIBCFS_LWT_SNAPSHOT: {
245                 cfs_cycles_t   now;
246                 int         ncpu;
247                 int         total_size;
248
249                 err = lwt_snapshot (&now, &ncpu, &total_size,
250                                     data->ioc_pbuf1, data->ioc_plen1);
251                 data->ioc_u64[0] = now;
252                 data->ioc_u32[0] = ncpu;
253                 data->ioc_u32[1] = total_size;
254
255                 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
256                 data->ioc_u32[2] = sizeof(lwt_event_t);
257                 data->ioc_u32[3] = offsetof(lwt_event_t, lwte_where);
258
259                 if (err == 0 &&
260                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
261                         err = -EFAULT;
262                 break;
263         }
264
265         case IOC_LIBCFS_LWT_LOOKUP_STRING:
266                 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
267                                          data->ioc_pbuf2, data->ioc_plen2);
268                 if (err == 0 &&
269                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
270                         err = -EFAULT;
271                 break;
272 #endif
273         case IOC_LIBCFS_MEMHOG:
274                 if (pfile->private_data == NULL) {
275                         err = -EINVAL;
276                 } else {
277                         kportal_memhog_free(pfile->private_data);
278                         /* XXX The ioc_flags is not GFP flags now, need to be fixed */
279                         err = kportal_memhog_alloc(pfile->private_data,
280                                                    data->ioc_count,
281                                                    data->ioc_flags);
282                         if (err != 0)
283                                 kportal_memhog_free(pfile->private_data);
284                 }
285                 break;
286
287         case IOC_LIBCFS_PING_TEST: {
288                 extern void (kping_client)(struct libcfs_ioctl_data *);
289                 void (*ping)(struct libcfs_ioctl_data *);
290
291                 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
292                        data->ioc_count, libcfs_nid2str(data->ioc_nid),
293                        libcfs_nid2str(data->ioc_nid));
294                 ping = symbol_get(kping_client);
295                 if (!ping)
296                         CERROR("symbol_get failed\n");
297                 else {
298                         ping(data);
299                         symbol_put(kping_client);
300                 }
301                 return 0;
302         }
303
304         default: {
305                 struct libcfs_ioctl_handler *hand;
306                 err = -EINVAL;
307                 down_read(&ioctl_list_sem);
308                 list_for_each_entry(hand, &ioctl_list, item) {
309                         err = hand->handle_ioctl(cmd, data);
310                         if (err != -EINVAL) {
311                                 if (err == 0)
312                                         err = libcfs_ioctl_popdata(arg,
313                                                         data, sizeof (*data));
314                                 break;
315                         }
316                 }
317                 up_read(&ioctl_list_sem);
318                 break;
319         }
320         }
321
322         return err;
323 }
324
325 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
326 {
327         char    *buf;
328         struct libcfs_ioctl_data *data;
329         int err = 0;
330
331         LIBCFS_ALLOC_GFP(buf, 1024, GFP_IOFS);
332         if (buf == NULL)
333                 return -ENOMEM;
334
335         /* 'cmd' and permissions get checked in our arch-specific caller */
336         if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
337                 CERROR("PORTALS ioctl: data error\n");
338                 GOTO(out, err = -EINVAL);
339         }
340         data = (struct libcfs_ioctl_data *)buf;
341
342         err = libcfs_ioctl_int(pfile, cmd, arg, data);
343
344 out:
345         LIBCFS_FREE(buf, 1024);
346         return err;
347 }
348
349
350 struct cfs_psdev_ops libcfs_psdev_ops = {
351         libcfs_psdev_open,
352         libcfs_psdev_release,
353         NULL,
354         NULL,
355         libcfs_ioctl
356 };
357
358 extern int insert_proc(void);
359 extern void remove_proc(void);
360 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
361 MODULE_DESCRIPTION("Portals v3.1");
362 MODULE_LICENSE("GPL");
363
364 extern struct miscdevice libcfs_dev;
365 extern struct rw_semaphore cfs_tracefile_sem;
366 extern struct mutex cfs_trace_thread_mutex;
367 extern struct cfs_wi_sched *cfs_sched_rehash;
368
369 extern void libcfs_init_nidstrings(void);
370 extern int libcfs_arch_init(void);
371 extern void libcfs_arch_cleanup(void);
372
373 static int init_libcfs_module(void)
374 {
375         int rc;
376
377         libcfs_arch_init();
378         libcfs_init_nidstrings();
379         init_rwsem(&cfs_tracefile_sem);
380         mutex_init(&cfs_trace_thread_mutex);
381         init_rwsem(&ioctl_list_sem);
382         INIT_LIST_HEAD(&ioctl_list);
383         init_waitqueue_head(&cfs_race_waitq);
384
385         rc = libcfs_debug_init(5 * 1024 * 1024);
386         if (rc < 0) {
387                 printk(KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
388                 return (rc);
389         }
390
391         rc = cfs_cpu_init();
392         if (rc != 0)
393                 goto cleanup_debug;
394
395 #if LWT_SUPPORT
396         rc = lwt_init();
397         if (rc != 0) {
398                 CERROR("lwt_init: error %d\n", rc);
399                 goto cleanup_debug;
400         }
401 #endif
402         rc = misc_register(&libcfs_dev);
403         if (rc) {
404                 CERROR("misc_register: error %d\n", rc);
405                 goto cleanup_lwt;
406         }
407
408         rc = cfs_wi_startup();
409         if (rc) {
410                 CERROR("initialize workitem: error %d\n", rc);
411                 goto cleanup_deregister;
412         }
413
414         /* max to 4 threads, should be enough for rehash */
415         rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
416         rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
417                                  rc, &cfs_sched_rehash);
418         if (rc != 0) {
419                 CERROR("Startup workitem scheduler: error: %d\n", rc);
420                 goto cleanup_deregister;
421         }
422
423         rc = cfs_crypto_register();
424         if (rc) {
425                 CERROR("cfs_crypto_regster: error %d\n", rc);
426                 goto cleanup_wi;
427         }
428
429
430         rc = insert_proc();
431         if (rc) {
432                 CERROR("insert_proc: error %d\n", rc);
433                 goto cleanup_crypto;
434         }
435
436         CDEBUG (D_OTHER, "portals setup OK\n");
437         return 0;
438  cleanup_crypto:
439         cfs_crypto_unregister();
440  cleanup_wi:
441         cfs_wi_shutdown();
442  cleanup_deregister:
443         misc_deregister(&libcfs_dev);
444  cleanup_lwt:
445 #if LWT_SUPPORT
446         lwt_fini();
447 #endif
448  cleanup_debug:
449         libcfs_debug_cleanup();
450         return rc;
451 }
452
453 static void exit_libcfs_module(void)
454 {
455         int rc;
456
457         remove_proc();
458
459         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
460                atomic_read(&libcfs_kmemory));
461
462         if (cfs_sched_rehash != NULL) {
463                 cfs_wi_sched_destroy(cfs_sched_rehash);
464                 cfs_sched_rehash = NULL;
465         }
466
467         cfs_crypto_unregister();
468         cfs_wi_shutdown();
469
470         rc = misc_deregister(&libcfs_dev);
471         if (rc)
472                 CERROR("misc_deregister error %d\n", rc);
473
474 #if LWT_SUPPORT
475         lwt_fini();
476 #endif
477         cfs_cpu_fini();
478
479         if (atomic_read(&libcfs_kmemory) != 0)
480                 CERROR("Portals memory leaked: %d bytes\n",
481                        atomic_read(&libcfs_kmemory));
482
483         rc = libcfs_debug_cleanup();
484         if (rc)
485                 printk(KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
486                        rc);
487
488         fini_rwsem(&ioctl_list_sem);
489         fini_rwsem(&cfs_tracefile_sem);
490
491         libcfs_arch_cleanup();
492 }
493
494 MODULE_VERSION("1.0.0");
495 module_init(init_libcfs_module);
496 module_exit(exit_libcfs_module);