Whamcloud - gitweb
LU-13783 libcfs: provide fallback kallsyms_lookup_name()
[fs/lustre-release.git] / libcfs / libcfs / linux / linux-prim.c
index 73fd714..0940c80 100644 (file)
  *
  * You should have received a copy of the GNU General Public License
  * version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
  */
 
 #define DEBUG_SUBSYSTEM S_LNET
+
+#include <linux/kallsyms.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
-#include <linux/fs_struct.h>
+#include <linux/fs.h>
 #include <linux/sched.h>
-
-#include <libcfs/libcfs.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_init_timer(struct timer_list *t)
-{
-       init_timer(t);
-}
-EXPORT_SYMBOL(cfs_init_timer);
+#include <libcfs/linux/linux-time.h>
+#include <libcfs/linux/linux-wait.h>
 
-void cfs_timer_init(struct timer_list *t, cfs_timer_func_t *func, void *arg)
+#ifndef HAVE_KTIME_GET_TS64
+void ktime_get_ts64(struct timespec64 *ts)
 {
-       init_timer(t);
-       t->function = func;
-       t->data = (unsigned long)arg;
-}
-EXPORT_SYMBOL(cfs_timer_init);
+       struct timespec now;
 
-void cfs_timer_arm(struct timer_list *t, cfs_time_t deadline)
-{
-       mod_timer(t, deadline);
+       ktime_get_ts(&now);
+       *ts = timespec_to_timespec64(now);
 }
-EXPORT_SYMBOL(cfs_timer_arm);
+EXPORT_SYMBOL(ktime_get_ts64);
+#endif /* HAVE_KTIME_GET_TS64 */
 
-void cfs_timer_disarm(struct timer_list *t)
+#ifndef HAVE_KTIME_GET_REAL_TS64
+void ktime_get_real_ts64(struct timespec64 *ts)
 {
-       del_timer(t);
+       struct timespec now;
+
+       getnstimeofday(&now);
+       *ts = timespec_to_timespec64(now);
 }
-EXPORT_SYMBOL(cfs_timer_disarm);
+EXPORT_SYMBOL(ktime_get_real_ts64);
+#endif /* HAVE_KTIME_GET_REAL_TS64 */
 
-int  cfs_timer_is_armed(struct timer_list *t)
+#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 timer_pending(t);
+       return (time64_t)get_seconds();
 }
-EXPORT_SYMBOL(cfs_timer_is_armed);
+EXPORT_SYMBOL(ktime_get_real_seconds);
+#endif /* HAVE_KTIME_GET_REAL_SECONDS */
 
-cfs_time_t cfs_timer_deadline(struct timer_list *t)
+#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)
 {
-       return t->expires;
+       struct timespec64 now;
+
+       ktime_get_ts64(&now);
+       return now.tv_sec;
 }
-EXPORT_SYMBOL(cfs_timer_deadline);
+EXPORT_SYMBOL(ktime_get_seconds);
+#endif /* HAVE_KTIME_GET_SECONDS */
 
-#ifndef HAVE_KTIME_GET_REAL_TS64
-void ktime_get_real_ts64(struct timespec64 *ts)
+static int (*cfs_apply_workqueue_attrs_t)(struct workqueue_struct *wq,
+                                         const struct workqueue_attrs *attrs);
+
+int cfs_apply_workqueue_attrs(struct workqueue_struct *wq,
+                             const struct workqueue_attrs *attrs)
 {
-       *ts = timespec_to_timespec64(CURRENT_TIME);
+       if (cfs_apply_workqueue_attrs_t)
+               return cfs_apply_workqueue_attrs_t(wq, attrs);
+       return 0;
 }
-EXPORT_SYMBOL(ktime_get_real_ts64);
-#endif /* HAVE_KTIME_GET_REAL_TS64 */
+EXPORT_SYMBOL_GPL(cfs_apply_workqueue_attrs);
 
-sigset_t
-cfs_block_allsigs(void)
+#ifndef HAVE_XARRAY_SUPPORT
+struct kmem_cache (*radix_tree_node_cachep);
+#endif
+
+void __init cfs_arch_init(void)
 {
-       unsigned long   flags;
-       sigset_t        old;
-
-       spin_lock_irqsave(&current->sighand->siglock, flags);
-       old = current->blocked;
-       sigfillset(&current->blocked);
-       recalc_sigpending();
-       spin_unlock_irqrestore(&current->sighand->siglock, 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
 }
-EXPORT_SYMBOL(cfs_block_allsigs);
 
-sigset_t cfs_block_sigs(unsigned long sigs)
+int cfs_kernel_write(struct file *filp, const void *buf, size_t count,
+                    loff_t *pos)
 {
-       unsigned long  flags;
-       sigset_t        old;
-
-       spin_lock_irqsave(&current->sighand->siglock, flags);
-       old = current->blocked;
-       sigaddsetmask(&current->blocked, sigs);
-       recalc_sigpending();
-       spin_unlock_irqrestore(&current->sighand->siglock, flags);
-       return old;
+#ifdef HAVE_NEW_KERNEL_WRITE
+       return kernel_write(filp, buf, count, pos);
+#else
+       mm_segment_t __old_fs = get_fs();
+       int rc;
+
+       set_fs(KERNEL_DS);
+       rc = vfs_write(filp, (__force const char __user *)buf, count, pos);
+       set_fs(__old_fs);
+
+       return rc;
+#endif
 }
-EXPORT_SYMBOL(cfs_block_sigs);
+EXPORT_SYMBOL(cfs_kernel_write);
 
-/* Block all signals except for the @sigs */
-sigset_t cfs_block_sigsinv(unsigned long sigs)
+ssize_t cfs_kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
 {
-       unsigned long flags;
-       sigset_t old;
-
-       spin_lock_irqsave(&current->sighand->siglock, flags);
-       old = current->blocked;
-       sigaddsetmask(&current->blocked, ~sigs);
-       recalc_sigpending();
-       spin_unlock_irqrestore(&current->sighand->siglock, flags);
-       return old;
+#ifdef HAVE_KERNEL_READ_LAST_POSP
+       return kernel_read(file, buf, count, pos);
+#else
+       ssize_t size = kernel_read(file, *pos, buf, count);
+
+       if (size > 0)
+               *pos += size;
+       return size;
+#endif
 }
-EXPORT_SYMBOL(cfs_block_sigsinv);
+EXPORT_SYMBOL(cfs_kernel_read);
 
-void
-cfs_restore_sigs(sigset_t old)
+#ifndef HAVE_KSET_FIND_OBJ
+struct kobject *kset_find_obj(struct kset *kset, const char *name)
 {
-       unsigned long  flags;
+       struct kobject *ret = NULL;
+       struct kobject *k;
 
-       spin_lock_irqsave(&current->sighand->siglock, flags);
-       current->blocked = old;
-       recalc_sigpending();
-       spin_unlock_irqrestore(&current->sighand->siglock, flags);
+       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(cfs_restore_sigs);
+EXPORT_SYMBOL_GPL(kset_find_obj);
+#endif
 
-void
-cfs_clear_sigpending(void)
+#ifndef HAVE_KSTRTOBOOL_FROM_USER
+int kstrtobool_from_user(const char __user *s, size_t count, bool *res)
 {
-       unsigned long flags;
-
-       spin_lock_irqsave(&current->sighand->siglock, flags);
-       clear_tsk_thread_flag(current, TIF_SIGPENDING);
-       spin_unlock_irqrestore(&current->sighand->siglock, flags);
+       /* 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(cfs_clear_sigpending);
+EXPORT_SYMBOL(kstrtobool_from_user);
+#endif /* !HAVE_KSTRTOBOOL_FROM_USER */