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