Whamcloud - gitweb
LU-13783 libcfs: provide fallback kallsyms_lookup_name()
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-prim.c
index cc02829..0940c80 100644 (file)
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * GPL HEADER START
  *
- *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.gnu.org/licenses/gpl-2.0.html
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2013, Intel Corporation.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
  */
 
 #define DEBUG_SUBSYSTEM S_LNET
-#ifndef AUTOCONF_INCLUDED
-#include <linux/config.h>
-#endif
+
+#include <linux/kallsyms.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
-#include <libcfs/libcfs.h>
+#include <linux/fs.h>
+#include <linux/sched.h>
+#ifdef HAVE_SCHED_HEADERS
+#include <linux/sched/mm.h>
+#endif
+#include <linux/uaccess.h>
 
 #if defined(CONFIG_KGDB)
 #include <asm/kgdb.h>
 #endif
 
-void cfs_enter_debugger(void)
+#include <libcfs/linux/linux-time.h>
+#include <libcfs/linux/linux-wait.h>
+
+#ifndef HAVE_KTIME_GET_TS64
+void ktime_get_ts64(struct timespec64 *ts)
 {
-#if defined(CONFIG_KGDB)
-        BREAKPOINT();
-#elif defined(__arch_um__)
-        asm("int $3");
-#else
-        /* nothing */
-#endif
+       struct timespec now;
+
+       ktime_get_ts(&now);
+       *ts = timespec_to_timespec64(now);
 }
+EXPORT_SYMBOL(ktime_get_ts64);
+#endif /* HAVE_KTIME_GET_TS64 */
 
-void cfs_daemonize(char *str) {
-        unsigned long flags;
+#ifndef HAVE_KTIME_GET_REAL_TS64
+void ktime_get_real_ts64(struct timespec64 *ts)
+{
+       struct timespec now;
 
-        lock_kernel();
-#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,63))
-        daemonize(str);
-#else
-        daemonize();
-        exit_files(current);
-        reparent_to_init();
-        snprintf (current->comm, sizeof (current->comm), "%s", str);
-#endif
-        SIGNAL_MASK_LOCK(current, flags);
-        sigfillset(&current->blocked);
-        RECALC_SIGPENDING;
-        SIGNAL_MASK_UNLOCK(current, flags);
-        unlock_kernel();
+       getnstimeofday(&now);
+       *ts = timespec_to_timespec64(now);
 }
-
-int cfs_daemonize_ctxt(char *str) {
-        struct task_struct *tsk = current;
-        struct fs_struct *fs = NULL;
-
-        cfs_daemonize(str);
-        fs = copy_fs_struct(tsk->fs);
-        if (fs == NULL)
-                return -ENOMEM;
-        exit_fs(tsk);
-        tsk->fs = fs;
-        return 0;
+EXPORT_SYMBOL(ktime_get_real_ts64);
+#endif /* HAVE_KTIME_GET_REAL_TS64 */
+
+#ifndef HAVE_KTIME_GET_REAL_SECONDS
+/*
+ * Get the seconds portion of CLOCK_REALTIME (wall clock).
+ * This is the clock that can be altered by NTP and is
+ * independent of a reboot.
+ */
+time64_t ktime_get_real_seconds(void)
+{
+       return (time64_t)get_seconds();
 }
-
-
-sigset_t
-cfs_get_blockedsigs(void)
+EXPORT_SYMBOL(ktime_get_real_seconds);
+#endif /* HAVE_KTIME_GET_REAL_SECONDS */
+
+#ifndef HAVE_KTIME_GET_SECONDS
+/*
+ * Get the seconds portion of CLOCK_MONOTONIC
+ * This clock is immutable and is reset across
+ * reboots. For older platforms this is a
+ * wrapper around get_seconds which is valid
+ * until 2038. By that time this will be gone
+ * one would hope.
+ */
+time64_t ktime_get_seconds(void)
 {
-        unsigned long          flags;
-        sigset_t        old;
+       struct timespec64 now;
 
-        SIGNAL_MASK_LOCK(current, flags);
-        old = current->blocked;
-        SIGNAL_MASK_UNLOCK(current, flags);
-        return old;
+       ktime_get_ts64(&now);
+       return now.tv_sec;
 }
+EXPORT_SYMBOL(ktime_get_seconds);
+#endif /* HAVE_KTIME_GET_SECONDS */
 
-sigset_t
-cfs_block_allsigs(void)
-{
-        unsigned long          flags;
-        sigset_t        old;
+static int (*cfs_apply_workqueue_attrs_t)(struct workqueue_struct *wq,
+                                         const struct workqueue_attrs *attrs);
 
-        SIGNAL_MASK_LOCK(current, flags);
-        old = current->blocked;
-        sigfillset(&current->blocked);
-        RECALC_SIGPENDING;
-        SIGNAL_MASK_UNLOCK(current, flags);
-
-        return old;
+int cfs_apply_workqueue_attrs(struct workqueue_struct *wq,
+                             const struct workqueue_attrs *attrs)
+{
+       if (cfs_apply_workqueue_attrs_t)
+               return cfs_apply_workqueue_attrs_t(wq, attrs);
+       return 0;
 }
+EXPORT_SYMBOL_GPL(cfs_apply_workqueue_attrs);
 
-sigset_t
-cfs_block_sigs(sigset_t bits)
+#ifndef HAVE_XARRAY_SUPPORT
+struct kmem_cache (*radix_tree_node_cachep);
+#endif
+
+void __init cfs_arch_init(void)
 {
-        unsigned long  flags;
-        sigset_t        old;
-
-        SIGNAL_MASK_LOCK(current, flags);
-        old = current->blocked;
-        current->blocked = bits;
-        RECALC_SIGPENDING;
-        SIGNAL_MASK_UNLOCK(current, flags);
-        return old;
+#ifndef HAVE_WAIT_VAR_EVENT
+       wait_bit_init();
+#endif
+       cfs_apply_workqueue_attrs_t =
+               (void *)kallsyms_lookup_name("apply_workqueue_attrs");
+#ifndef HAVE_XARRAY_SUPPORT
+       radix_tree_node_cachep =
+               (void *)kallsyms_lookup_name("radix_tree_node_cachep");
+#endif
 }
 
-void
-cfs_restore_sigs (cfs_sigset_t old)
+int cfs_kernel_write(struct file *filp, const void *buf, size_t count,
+                    loff_t *pos)
 {
-        unsigned long  flags;
+#ifdef HAVE_NEW_KERNEL_WRITE
+       return kernel_write(filp, buf, count, pos);
+#else
+       mm_segment_t __old_fs = get_fs();
+       int rc;
 
-        SIGNAL_MASK_LOCK(current, flags);
-        current->blocked = old;
-        RECALC_SIGPENDING;
-        SIGNAL_MASK_UNLOCK(current, flags);
-}
+       set_fs(KERNEL_DS);
+       rc = vfs_write(filp, (__force const char __user *)buf, count, pos);
+       set_fs(__old_fs);
 
-int
-cfs_signal_pending(void)
-{
-        return signal_pending(current);
+       return rc;
+#endif
 }
+EXPORT_SYMBOL(cfs_kernel_write);
 
-void
-cfs_clear_sigpending(void)
+ssize_t cfs_kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
 {
-        unsigned long flags;
+#ifdef HAVE_KERNEL_READ_LAST_POSP
+       return kernel_read(file, buf, count, pos);
+#else
+       ssize_t size = kernel_read(file, *pos, buf, count);
 
-        SIGNAL_MASK_LOCK(current, flags);
-        CLEAR_SIGPENDING;
-        SIGNAL_MASK_UNLOCK(current, flags);
+       if (size > 0)
+               *pos += size;
+       return size;
+#endif
 }
+EXPORT_SYMBOL(cfs_kernel_read);
 
-int
-libcfs_arch_init(void)
+#ifndef HAVE_KSET_FIND_OBJ
+struct kobject *kset_find_obj(struct kset *kset, const char *name)
 {
-        return 0;
+       struct kobject *ret = NULL;
+       struct kobject *k;
+
+       spin_lock(&kset->list_lock);
+
+       list_for_each_entry(k, &kset->list, entry) {
+               if (kobject_name(k) && !strcmp(kobject_name(k), name)) {
+                       if (kref_get_unless_zero(&k->kref))
+                               ret = k;
+                       break;
+               }
+       }
+
+       spin_unlock(&kset->list_lock);
+       return ret;
 }
+EXPORT_SYMBOL_GPL(kset_find_obj);
+#endif
 
-void
-libcfs_arch_cleanup(void)
+#ifndef HAVE_KSTRTOBOOL_FROM_USER
+int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
 {
-        return;
+       /* Longest string needed to differentiate, newline, terminator */
+       char buf[4];
+
+       count = min(count, sizeof(buf) - 1);
+       if (copy_from_user(buf, s, count))
+               return -EFAULT;
+       buf[count] = '\0';
+       return strtobool(buf, res);
 }
-
-EXPORT_SYMBOL(libcfs_arch_init);
-EXPORT_SYMBOL(libcfs_arch_cleanup);
-EXPORT_SYMBOL(cfs_daemonize);
-EXPORT_SYMBOL(cfs_daemonize_ctxt);
-EXPORT_SYMBOL(cfs_block_allsigs);
-EXPORT_SYMBOL(cfs_block_sigs);
-EXPORT_SYMBOL(cfs_get_blockedsigs);
-EXPORT_SYMBOL(cfs_restore_sigs);
-EXPORT_SYMBOL(cfs_signal_pending);
-EXPORT_SYMBOL(cfs_clear_sigpending);
+EXPORT_SYMBOL(kstrtobool_from_user);
+#endif /* !HAVE_KSTRTOBOOL_FROM_USER */