Whamcloud - gitweb
LU-1187 tests: add create remote directory to racer
[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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #define DEBUG_SUBSYSTEM S_LNET
36
37 #include <libcfs/libcfs.h>
38 #include <libcfs/libcfs_crypto.h>
39 #include <lnet/lib-lnet.h>
40 #include <lnet/lnet.h>
41 #include "tracefile.h"
42
43 void
44 kportal_memhog_free (struct libcfs_device_userstate *ldu)
45 {
46         cfs_page_t **level0p = &ldu->ldu_memhog_root_page;
47         cfs_page_t **level1p;
48         cfs_page_t **level2p;
49         int           count1;
50         int           count2;
51
52         if (*level0p != NULL) {
53
54                 level1p = (cfs_page_t **)cfs_page_address(*level0p);
55                 count1 = 0;
56
57                 while (count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
58                        *level1p != NULL) {
59
60                         level2p = (cfs_page_t **)cfs_page_address(*level1p);
61                         count2 = 0;
62
63                         while (count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *) &&
64                                *level2p != NULL) {
65
66                                 cfs_free_page(*level2p);
67                                 ldu->ldu_memhog_pages--;
68                                 level2p++;
69                                 count2++;
70                         }
71
72                         cfs_free_page(*level1p);
73                         ldu->ldu_memhog_pages--;
74                         level1p++;
75                         count1++;
76                 }
77
78                 cfs_free_page(*level0p);
79                 ldu->ldu_memhog_pages--;
80
81                 *level0p = NULL;
82         }
83
84         LASSERT (ldu->ldu_memhog_pages == 0);
85 }
86
87 int
88 kportal_memhog_alloc (struct libcfs_device_userstate *ldu, int npages, int flags)
89 {
90         cfs_page_t **level0p;
91         cfs_page_t **level1p;
92         cfs_page_t **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 = cfs_alloc_page(flags);
107         if (*level0p == NULL)
108                 return -ENOMEM;
109         ldu->ldu_memhog_pages++;
110
111         level1p = (cfs_page_t **)cfs_page_address(*level0p);
112         count1 = 0;
113         memset(level1p, 0, CFS_PAGE_SIZE);
114
115         while (ldu->ldu_memhog_pages < npages &&
116                count1 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
117
118                 if (cfs_signal_pending())
119                         return (-EINTR);
120
121                 *level1p = cfs_alloc_page(flags);
122                 if (*level1p == NULL)
123                         return -ENOMEM;
124                 ldu->ldu_memhog_pages++;
125
126                 level2p = (cfs_page_t **)cfs_page_address(*level1p);
127                 count2 = 0;
128                 memset(level2p, 0, CFS_PAGE_SIZE);
129
130                 while (ldu->ldu_memhog_pages < npages &&
131                        count2 < CFS_PAGE_SIZE/sizeof(cfs_page_t *)) {
132
133                         if (cfs_signal_pending())
134                                 return (-EINTR);
135
136                         *level2p = cfs_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         PORTAL_MODULE_USE;
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         PORTAL_MODULE_UNUSE;
183         RETURN(0);
184 }
185
186 static struct rw_semaphore ioctl_list_sem;
187 static cfs_list_t 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 (!cfs_list_empty(&hand->item))
195                 rc = -EBUSY;
196         else
197                 cfs_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 (cfs_list_empty(&hand->item))
210                 rc = -ENOENT;
211         else
212                 cfs_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         ENTRY;
224
225         switch (cmd) {
226         case IOC_LIBCFS_CLEAR_DEBUG:
227                 libcfs_debug_clear_buffer();
228                 RETURN(0);
229         /*
230          * case IOC_LIBCFS_PANIC:
231          * Handled in arch/cfs_module.c
232          */
233         case IOC_LIBCFS_MARK_DEBUG:
234                 if (data->ioc_inlbuf1 == NULL ||
235                     data->ioc_inlbuf1[data->ioc_inllen1 - 1] != '\0')
236                         RETURN(-EINVAL);
237                 libcfs_debug_mark_buffer(data->ioc_inlbuf1);
238                 RETURN(0);
239 #if LWT_SUPPORT
240         case IOC_LIBCFS_LWT_CONTROL:
241                 err = lwt_control ((data->ioc_flags & 1) != 0, 
242                                    (data->ioc_flags & 2) != 0);
243                 break;
244
245         case IOC_LIBCFS_LWT_SNAPSHOT: {
246                 cfs_cycles_t   now;
247                 int            ncpu;
248                 int            total_size;
249
250                 err = lwt_snapshot (&now, &ncpu, &total_size,
251                                     data->ioc_pbuf1, data->ioc_plen1);
252                 data->ioc_u64[0] = now;
253                 data->ioc_u32[0] = ncpu;
254                 data->ioc_u32[1] = total_size;
255
256                 /* Hedge against broken user/kernel typedefs (e.g. cycles_t) */
257                 data->ioc_u32[2] = sizeof(lwt_event_t);
258                 data->ioc_u32[3] = offsetof(lwt_event_t, lwte_where);
259
260                 if (err == 0 &&
261                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
262                         err = -EFAULT;
263                 break;
264         }
265
266         case IOC_LIBCFS_LWT_LOOKUP_STRING:
267                 err = lwt_lookup_string (&data->ioc_count, data->ioc_pbuf1,
268                                          data->ioc_pbuf2, data->ioc_plen2);
269                 if (err == 0 &&
270                     libcfs_ioctl_popdata(arg, data, sizeof (*data)))
271                         err = -EFAULT;
272                 break;
273 #endif
274         case IOC_LIBCFS_MEMHOG:
275                 if (pfile->private_data == NULL) {
276                         err = -EINVAL;
277                 } else {
278                         kportal_memhog_free(pfile->private_data);
279                         /* XXX The ioc_flags is not GFP flags now, need to be fixed */
280                         err = kportal_memhog_alloc(pfile->private_data,
281                                                    data->ioc_count,
282                                                    data->ioc_flags);
283                         if (err != 0)
284                                 kportal_memhog_free(pfile->private_data);
285                 }
286                 break;
287
288         case IOC_LIBCFS_PING_TEST: {
289                 extern void (kping_client)(struct libcfs_ioctl_data *);
290                 void (*ping)(struct libcfs_ioctl_data *);
291
292                 CDEBUG(D_IOCTL, "doing %d pings to nid %s (%s)\n",
293                        data->ioc_count, libcfs_nid2str(data->ioc_nid),
294                        libcfs_nid2str(data->ioc_nid));
295                 ping = PORTAL_SYMBOL_GET(kping_client);
296                 if (!ping)
297                         CERROR("PORTAL_SYMBOL_GET failed\n");
298                 else {
299                         ping(data);
300                         PORTAL_SYMBOL_PUT(kping_client);
301                 }
302                 RETURN(0);
303         }
304
305         default: {
306                 struct libcfs_ioctl_handler *hand;
307                 err = -EINVAL;
308                 down_read(&ioctl_list_sem);
309                 cfs_list_for_each_entry_typed(hand, &ioctl_list,
310                         struct libcfs_ioctl_handler, item) {
311                         err = hand->handle_ioctl(cmd, data);
312                         if (err != -EINVAL) {
313                                 if (err == 0)
314                                         err = libcfs_ioctl_popdata(arg, 
315                                                         data, sizeof (*data));
316                                 break;
317                         }
318                 }
319                 up_read(&ioctl_list_sem);
320                 break;
321         }
322         }
323
324         RETURN(err);
325 }
326
327 static int libcfs_ioctl(struct cfs_psdev_file *pfile, unsigned long cmd, void *arg)
328 {
329         char    *buf;
330         struct libcfs_ioctl_data *data;
331         int err = 0;
332         ENTRY;
333
334         LIBCFS_ALLOC_GFP(buf, 1024, CFS_ALLOC_STD);
335         if (buf == NULL)
336                 RETURN(-ENOMEM);
337
338         /* 'cmd' and permissions get checked in our arch-specific caller */
339         if (libcfs_ioctl_getdata(buf, buf + 800, (void *)arg)) {
340                 CERROR("PORTALS ioctl: data error\n");
341                 GOTO(out, err = -EINVAL);
342         }
343         data = (struct libcfs_ioctl_data *)buf;
344
345         err = libcfs_ioctl_int(pfile, cmd, arg, data);
346
347 out:
348         LIBCFS_FREE(buf, 1024);
349         RETURN(err);
350 }
351
352
353 struct cfs_psdev_ops libcfs_psdev_ops = {
354         libcfs_psdev_open,
355         libcfs_psdev_release,
356         NULL,
357         NULL,
358         libcfs_ioctl
359 };
360
361 extern int insert_proc(void);
362 extern void remove_proc(void);
363 MODULE_AUTHOR("Peter J. Braam <braam@clusterfs.com>");
364 MODULE_DESCRIPTION("Portals v3.1");
365 MODULE_LICENSE("GPL");
366
367 extern cfs_psdev_t libcfs_dev;
368 extern struct rw_semaphore cfs_tracefile_sem;
369 extern struct mutex cfs_trace_thread_mutex;
370 extern struct cfs_wi_sched *cfs_sched_rehash;
371
372 extern void libcfs_init_nidstrings(void);
373 extern int libcfs_arch_init(void);
374 extern void libcfs_arch_cleanup(void);
375
376 static int init_libcfs_module(void)
377 {
378         int rc;
379
380         libcfs_arch_init();
381         libcfs_init_nidstrings();
382         init_rwsem(&cfs_tracefile_sem);
383         mutex_init(&cfs_trace_thread_mutex);
384         init_rwsem(&ioctl_list_sem);
385         CFS_INIT_LIST_HEAD(&ioctl_list);
386         cfs_waitq_init(&cfs_race_waitq);
387
388         rc = libcfs_debug_init(5 * 1024 * 1024);
389         if (rc < 0) {
390                 printk(CFS_KERN_ERR "LustreError: libcfs_debug_init: %d\n", rc);
391                 return (rc);
392         }
393
394         rc = cfs_cpu_init();
395         if (rc != 0)
396                 goto cleanup_debug;
397
398 #if LWT_SUPPORT
399         rc = lwt_init();
400         if (rc != 0) {
401                 CERROR("lwt_init: error %d\n", rc);
402                 goto cleanup_debug;
403         }
404 #endif
405         rc = cfs_psdev_register(&libcfs_dev);
406         if (rc) {
407                 CERROR("misc_register: error %d\n", rc);
408                 goto cleanup_lwt;
409         }
410
411         rc = cfs_wi_startup();
412         if (rc) {
413                 CERROR("initialize workitem: error %d\n", rc);
414                 goto cleanup_deregister;
415         }
416
417         /* max to 4 threads, should be enough for rehash */
418         rc = min(cfs_cpt_weight(cfs_cpt_table, CFS_CPT_ANY), 4);
419         rc = cfs_wi_sched_create("cfs_rh", cfs_cpt_table, CFS_CPT_ANY,
420                                  rc, &cfs_sched_rehash);
421         if (rc != 0) {
422                 CERROR("Startup workitem scheduler: error: %d\n", rc);
423                 goto cleanup_deregister;
424         }
425
426         rc = cfs_crypto_register();
427         if (rc) {
428                 CERROR("cfs_crypto_regster: error %d\n", rc);
429                 goto cleanup_wi;
430         }
431
432
433         rc = insert_proc();
434         if (rc) {
435                 CERROR("insert_proc: error %d\n", rc);
436                 goto cleanup_crypto;
437         }
438
439         CDEBUG (D_OTHER, "portals setup OK\n");
440         return 0;
441  cleanup_crypto:
442         cfs_crypto_unregister();
443  cleanup_wi:
444         cfs_wi_shutdown();
445  cleanup_deregister:
446         cfs_psdev_deregister(&libcfs_dev);
447  cleanup_lwt:
448 #if LWT_SUPPORT
449         lwt_fini();
450 #endif
451  cleanup_debug:
452         libcfs_debug_cleanup();
453         return rc;
454 }
455
456 static void exit_libcfs_module(void)
457 {
458         int rc;
459
460         remove_proc();
461
462         CDEBUG(D_MALLOC, "before Portals cleanup: kmem %d\n",
463                cfs_atomic_read(&libcfs_kmemory));
464
465         if (cfs_sched_rehash != NULL) {
466                 cfs_wi_sched_destroy(cfs_sched_rehash);
467                 cfs_sched_rehash = NULL;
468         }
469
470         cfs_crypto_unregister();
471         cfs_wi_shutdown();
472
473         rc = cfs_psdev_deregister(&libcfs_dev);
474         if (rc)
475                 CERROR("misc_deregister error %d\n", rc);
476
477 #if LWT_SUPPORT
478         lwt_fini();
479 #endif
480         cfs_cpu_fini();
481
482         if (cfs_atomic_read(&libcfs_kmemory) != 0)
483                 CERROR("Portals memory leaked: %d bytes\n",
484                        cfs_atomic_read(&libcfs_kmemory));
485
486         rc = libcfs_debug_cleanup();
487         if (rc)
488                 printk(CFS_KERN_ERR "LustreError: libcfs_debug_cleanup: %d\n",
489                        rc);
490
491         fini_rwsem(&ioctl_list_sem);
492         fini_rwsem(&cfs_tracefile_sem);
493
494         libcfs_arch_cleanup();
495 }
496
497 cfs_module(libcfs, "1.0.0", init_libcfs_module, exit_libcfs_module);