Whamcloud - gitweb
LU-1346 libcfs: cleanup libcfs primitive (linux-prim.h)
[fs/lustre-release.git] / libcfs / libcfs / winnt / winnt-prim.c
index da12e24..077f3a4 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2012, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -59,9 +59,7 @@
  */
 
 void
-cfs_thread_proc(
-    void * context
-    )
+cfs_thread_proc(void *context)
 {
     cfs_thread_context_t * thread_context =
         (cfs_thread_context_t *) context;
@@ -74,7 +72,7 @@ cfs_thread_proc(
 
     /* Free the context memory */
 
-    cfs_free(context);
+    kfree(context);
 
     /* Terminate this system thread */
 
@@ -82,33 +80,33 @@ cfs_thread_proc(
 }
 
 /*
- * cfs_kernel_thread
+ * kthread_run
  *   Create a system thread to execute the routine specified
  *
  * Arguments:
  *   func:  function to be executed in the thread
  *   arg:   argument transferred to func function
- *   flag:  thread creation flags.
+ *   name:  thread name to create
  *
  * Return Value:
- *   int:   0 on success or error codes
+ *   struct task_struct:   0 on success or error codes
  *
  * Notes:
  *   N/A
  */
 
-int cfs_kernel_thread(int (*func)(void *), void *arg, int flag)
+struct task_struct kthread_run(int (*func)(void *), void *arg, char *name)
 {
     cfs_handle_t  thread = NULL;
     NTSTATUS      status;
-    cfs_thread_context_t * context = NULL;
+    cfs_thread_context_t *context = NULL;
 
     /* Allocate the context to be transferred to system thread */
 
-    context = cfs_alloc(sizeof(cfs_thread_context_t), CFS_ALLOC_ZERO);
+    context = kmalloc(sizeof(cfs_thread_context_t), __GFP_ZERO);
 
     if (!context) {
-        return -ENOMEM;
+       return ERR_PTR(-ENOMEM);
     }
 
     context->func  = func;
@@ -126,11 +124,11 @@ int cfs_kernel_thread(int (*func)(void *), void *arg, int flag)
     if (!NT_SUCCESS(status)) {
 
 
-        cfs_free(context);
+       kfree(context);
 
         /* We need translate the nt status to linux error code */
 
-        return cfs_error_code(status);
+       return ERR_PTR(cfs_error_code(status));
     }
 
     //
@@ -139,7 +137,7 @@ int cfs_kernel_thread(int (*func)(void *), void *arg, int flag)
 
     ZwClose(thread);
 
-    return 0;
+       return (struct task_struct)0;
 }
 
 
@@ -148,7 +146,7 @@ int cfs_kernel_thread(int (*func)(void *), void *arg, int flag)
  */
 
 
-static CFS_DECLARE_RWSEM(cfs_symbol_lock);
+static DECLARE_RWSEM(cfs_symbol_lock);
 CFS_LIST_HEAD(cfs_symbol_list);
 
 int libcfs_is_mp_system = FALSE;
@@ -174,7 +172,7 @@ cfs_symbol_get(const char *name)
     cfs_list_t              *walker;
     struct cfs_symbol       *sym = NULL;
 
-    cfs_down_read(&cfs_symbol_lock);
+       down_read(&cfs_symbol_lock);
     cfs_list_for_each(walker, &cfs_symbol_list) {
         sym = cfs_list_entry (walker, struct cfs_symbol, sym_list);
         if (!strcmp(sym->name, name)) {
@@ -182,7 +180,7 @@ cfs_symbol_get(const char *name)
             break;
         }
     }
-    cfs_up_read(&cfs_symbol_lock);
+       up_read(&cfs_symbol_lock);
 
     if (sym != NULL)
         return sym->value;
@@ -210,7 +208,7 @@ cfs_symbol_put(const char *name)
     cfs_list_t              *walker;
     struct cfs_symbol       *sym = NULL;
 
-    cfs_down_read(&cfs_symbol_lock);
+       down_read(&cfs_symbol_lock);
     cfs_list_for_each(walker, &cfs_symbol_list) {
         sym = cfs_list_entry (walker, struct cfs_symbol, sym_list);
         if (!strcmp(sym->name, name)) {
@@ -219,7 +217,7 @@ cfs_symbol_put(const char *name)
             break;
         }
     }
-    cfs_up_read(&cfs_symbol_lock);
+       up_read(&cfs_symbol_lock);
 
     LASSERT(sym != NULL);
 }
@@ -248,26 +246,26 @@ cfs_symbol_register(const char *name, const void *value)
     struct cfs_symbol       *sym = NULL;
     struct cfs_symbol       *new = NULL;
 
-    new = cfs_alloc(sizeof(struct cfs_symbol), CFS_ALLOC_ZERO);
-    if (!new) {
-        return (-ENOMEM);
-    }
+       new = kmalloc(sizeof(struct cfs_symbol), __GFP_ZERO);
+       if (!new)
+               return -ENOMEM;
+
     strncpy(new->name, name, CFS_SYMBOL_LEN);
     new->value = (void *)value;
     new->ref = 0;
     CFS_INIT_LIST_HEAD(&new->sym_list);
 
-    cfs_down_write(&cfs_symbol_lock);
-    cfs_list_for_each(walker, &cfs_symbol_list) {
-        sym = cfs_list_entry (walker, struct cfs_symbol, sym_list);
-        if (!strcmp(sym->name, name)) {
-            cfs_up_write(&cfs_symbol_lock);
-            cfs_free(new);
-            return 0; // alreay registerred
-        }
-    }
-    cfs_list_add_tail(&new->sym_list, &cfs_symbol_list);
-    cfs_up_write(&cfs_symbol_lock);
+       down_write(&cfs_symbol_lock);
+       cfs_list_for_each(walker, &cfs_symbol_list) {
+               sym = cfs_list_entry (walker, struct cfs_symbol, sym_list);
+               if (!strcmp(sym->name, name)) {
+                       up_write(&cfs_symbol_lock);
+                       kfree(new);
+                       return 0; /* alreay registerred */
+               }
+       }
+       cfs_list_add_tail(&new->sym_list, &cfs_symbol_list);
+       up_write(&cfs_symbol_lock);
 
     return 0;
 }
@@ -293,17 +291,17 @@ cfs_symbol_unregister(const char *name)
     cfs_list_t              *nxt;
     struct cfs_symbol       *sym = NULL;
 
-    cfs_down_write(&cfs_symbol_lock);
+       down_write(&cfs_symbol_lock);
     cfs_list_for_each_safe(walker, nxt, &cfs_symbol_list) {
         sym = cfs_list_entry (walker, struct cfs_symbol, sym_list);
         if (!strcmp(sym->name, name)) {
             LASSERT(sym->ref == 0);
             cfs_list_del (&sym->sym_list);
-            cfs_free(sym);
+           kfree(sym);
             break;
         }
     }
-    cfs_up_write(&cfs_symbol_lock);
+       up_write(&cfs_symbol_lock);
 }
 
 /*
@@ -326,15 +324,15 @@ cfs_symbol_clean()
     cfs_list_t          *walker;
     struct cfs_symbol   *sym = NULL;
 
-    cfs_down_write(&cfs_symbol_lock);
-    cfs_list_for_each(walker, &cfs_symbol_list) {
-        sym = cfs_list_entry (walker, struct cfs_symbol, sym_list);
-        LASSERT(sym->ref == 0);
-        cfs_list_del (&sym->sym_list);
-        cfs_free(sym);
-    }
-    cfs_up_write(&cfs_symbol_lock);
-    return;
+       down_write(&cfs_symbol_lock);
+       cfs_list_for_each(walker, &cfs_symbol_list) {
+               sym = cfs_list_entry (walker, struct cfs_symbol, sym_list);
+               LASSERT(sym->ref == 0);
+               cfs_list_del (&sym->sym_list);
+               kfree(sym);
+       }
+       up_write(&cfs_symbol_lock);
+       return;
 }
 
 
@@ -353,10 +351,10 @@ cfs_timer_dpc_proc (
     IN PVOID SystemArgument1,
     IN PVOID SystemArgument2)
 {
-    cfs_timer_t *   timer;
+    struct timer_list *   timer;
     KIRQL           Irql;
 
-    timer = (cfs_timer_t *) DeferredContext;
+    timer = (struct timer_list *) DeferredContext;
 
     /* clear the flag */
     KeAcquireSpinLock(&(timer->Lock), &Irql);
@@ -367,14 +365,14 @@ cfs_timer_dpc_proc (
     timer->proc((long_ptr_t)timer->arg);
 }
 
-void cfs_init_timer(cfs_timer_t *timer)
+void cfs_init_timer(struct timer_list *timer)
 {
-    memset(timer, 0, sizeof(cfs_timer_t));
+    memset(timer, 0, sizeof(struct timer_list));
 }
 
 /*
  * cfs_timer_init
- *   To initialize the cfs_timer_t
+ *   To initialize the struct timer_list
  *
  * Arguments:
  *   timer:  the cfs_timer to be initialized
@@ -388,9 +386,9 @@ void cfs_init_timer(cfs_timer_t *timer)
  *   N/A
  */
 
-void cfs_timer_init(cfs_timer_t *timer, void (*func)(ulong_ptr_t), void *arg)
+void cfs_timer_init(struct timer_list *timer, void (*func)(ulong_ptr_t), void *arg)
 {
-    memset(timer, 0, sizeof(cfs_timer_t));
+    memset(timer, 0, sizeof(struct timer_list));
 
     timer->proc = func;
     timer->arg  = arg;
@@ -404,7 +402,7 @@ void cfs_timer_init(cfs_timer_t *timer, void (*func)(ulong_ptr_t), void *arg)
 
 /*
  * cfs_timer_done
- *   To finialize the cfs_timer_t (unused)
+ *   To finialize the struct timer_list (unused)
  *
  * Arguments:
  *   timer:  the cfs_timer to be cleaned up
@@ -416,7 +414,7 @@ void cfs_timer_init(cfs_timer_t *timer, void (*func)(ulong_ptr_t), void *arg)
  *   N/A
  */
 
-void cfs_timer_done(cfs_timer_t *timer)
+void cfs_timer_done(struct timer_list *timer)
 {
     return;
 }
@@ -436,7 +434,7 @@ void cfs_timer_done(cfs_timer_t *timer)
  *   N/A
  */
 
-void cfs_timer_arm(cfs_timer_t *timer, cfs_time_t deadline)
+void cfs_timer_arm(struct timer_list *timer, cfs_time_t deadline)
 {
     LARGE_INTEGER   timeout;
     KIRQL           Irql;
@@ -444,7 +442,7 @@ void cfs_timer_arm(cfs_timer_t *timer, cfs_time_t deadline)
     KeAcquireSpinLock(&(timer->Lock), &Irql);
     if (!cfs_is_flag_set(timer->Flags, CFS_TIMER_FLAG_TIMERED)){
 
-        timeout.QuadPart = (LONGLONG)-1*1000*1000*10/CFS_HZ*deadline;
+       timeout.QuadPart = (LONGLONG)-1*1000*1000*10/HZ*deadline;
 
         if (KeSetTimer(&timer->Timer, timeout, &timer->Dpc)) {
             cfs_set_flag(timer->Flags, CFS_TIMER_FLAG_TIMERED);
@@ -470,7 +468,7 @@ void cfs_timer_arm(cfs_timer_t *timer, cfs_time_t deadline)
  *   N/A
  */
 
-void cfs_timer_disarm(cfs_timer_t *timer)
+void cfs_timer_disarm(struct timer_list *timer)
 {
     KIRQL   Irql;
 
@@ -496,7 +494,7 @@ void cfs_timer_disarm(cfs_timer_t *timer)
  *   N/A
  */
 
-int cfs_timer_is_armed(cfs_timer_t *timer)
+int cfs_timer_is_armed(struct timer_list *timer)
 {
     int     rc = 0;
     KIRQL   Irql;
@@ -524,45 +522,38 @@ int cfs_timer_is_armed(cfs_timer_t *timer)
  *   N/A
  */
 
-cfs_time_t cfs_timer_deadline(cfs_timer_t * timer)
+cfs_time_t cfs_timer_deadline(struct timer_list * timer)
 {
     return timer->deadline;
 }
 
-/*
- * daemonize routine stub
- */
-
-void cfs_daemonize(char *str)
+int unshare_fs_struct()
 {
-    return;
-}
-
-int cfs_daemonize_ctxt(char *str) {
-    cfs_daemonize(str);
-    return 0;
+       return 0;
 }
 
 /*
  *  routine related with sigals
  */
 
-cfs_sigset_t cfs_get_blockedsigs()
+sigset_t cfs_block_allsigs()
 {
         return 0;
 }
 
-cfs_sigset_t cfs_block_allsigs()
+sigset_t cfs_block_sigs(sigset_t bit)
 {
         return 0;
 }
 
-cfs_sigset_t cfs_block_sigs(sigset_t bit)
+/* Block all signals except for the @sigs. It's only used in
+ * Linux kernel, just a dummy here. */
+sigset_t cfs_block_sigsinv(unsigned long sigs)
 {
         return 0;
 }
 
-void cfs_restore_sigs(cfs_sigset_t old)
+void cfs_restore_sigs(sigset_t old)
 {
 }
 
@@ -741,12 +732,12 @@ errorout:
     return NT_SUCCESS(status);
 }
 
-int cfs_need_resched(void)
+int need_resched(void)
 {
         return 0;
 }
 
-void cfs_cond_resched(void)
+void cond_resched(void)
 {
 }
 
@@ -759,27 +750,27 @@ void cfs_libc_init();
 int
 libcfs_arch_init(void)
 {
-    int         rc;
+       int             rc;
+       spinlock_t      lock;
 
-    cfs_spinlock_t  lock;
-    /* Workground to check the system is MP build or UP build */
-    cfs_spin_lock_init(&lock);
-    cfs_spin_lock(&lock);
-    libcfs_is_mp_system = (int)lock.lock;
-    /* MP build system: it's a real spin, for UP build system, it
-       only raises the IRQL to DISPATCH_LEVEL */
-    cfs_spin_unlock(&lock);
+       /* Workground to check the system is MP build or UP build */
+       spin_lock_init(&lock);
+       spin_lock(&lock);
+       libcfs_is_mp_system = (int)lock.lock;
+       /* MP build system: it's a real spin, for UP build system, it
+        * only raises the IRQL to DISPATCH_LEVEL */
+       spin_unlock(&lock);
 
     /* initialize libc routines (confliction between libcnptr.lib
        and kernel ntoskrnl.lib) */
     cfs_libc_init();
 
-    /* create slab memory caches for page alloctors */
-    cfs_page_t_slab = cfs_mem_cache_create(
-        "CPGT", sizeof(cfs_page_t), 0, 0 );
+       /* create slab memory caches for page alloctors */
+       cfs_page_t_slab = kmem_cache_create("CPGT", sizeof(struct page),
+                                           0, 0, NULL);
 
-    cfs_page_p_slab = cfs_mem_cache_create(
-        "CPGP", CFS_PAGE_SIZE, 0, 0 );
+       cfs_page_p_slab = kmem_cache_create("CPGP", PAGE_CACHE_SIZE,
+                                           0, 0, NULL);
 
     if ( cfs_page_t_slab == NULL ||
          cfs_page_p_slab == NULL ){
@@ -817,15 +808,13 @@ libcfs_arch_init(void)
 
 errorout:
 
-    if (rc != 0) {
-        /* destroy the taskslot cache slab */
-        if (cfs_page_t_slab) {
-            cfs_mem_cache_destroy(cfs_page_t_slab);
-        }
-        if (cfs_page_p_slab) {
-            cfs_mem_cache_destroy(cfs_page_p_slab);
-        }
-    }
+       if (rc != 0) {
+               /* destroy the taskslot cache slab */
+               if (cfs_page_t_slab)
+                       kmem_cache_destroy(cfs_page_t_slab);
+               if (cfs_page_p_slab)
+                       kmem_cache_destroy(cfs_page_p_slab);
+       }
 
     return rc;
 }
@@ -847,11 +836,11 @@ libcfs_arch_cleanup(void)
 
     /* destroy the taskslot cache slab */
     if (cfs_page_t_slab) {
-        cfs_mem_cache_destroy(cfs_page_t_slab);
+kmem_cache_destroy(cfs_page_t_slab);
     }
 
     if (cfs_page_p_slab) {
-        cfs_mem_cache_destroy(cfs_page_p_slab);
+kmem_cache_destroy(cfs_page_p_slab);
     }
 
     return;