Whamcloud - gitweb
Land b1_8_gate onto b1_8 (20081218_1708)
[fs/lustre-release.git] / lustre / liblustre / lutil.c
index 2227b0b..f907890 100644 (file)
@@ -1,22 +1,37 @@
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- *  Copyright (c) 2004 Cluster File Systems, Inc.
+ * GPL HEADER START
  *
- *   This file is part of Lustre, http://www.lustre.org.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * 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.
  *
- *   Lustre 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 for more details.
+ * 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
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * 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.
+ *
+ * GPL HEADER END
+ */
+/*
+ * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
  */
 
 #include <stdlib.h>
 #ifdef HAVE_NETDB_H
 #include <netdb.h>
 #endif
+#ifdef _AIX
+#include "syscall_AIX.h"
+#else
 #include <syscall.h>
+#endif
 #include <sys/utsname.h>
 #ifdef HAVE_NETINET_IN_H
 #include <netinet/in.h>
 
 #include "lutil.h"
 
-#if CRAY_PORTALS
-void portals_debug_dumplog(void){};
-#endif
 
-unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_NAL);
-unsigned int portal_debug = 0;
+unsigned int libcfs_subsystem_debug = ~0 - (S_LNET | S_LND);
+unsigned int libcfs_debug = 0;
 
 struct task_struct     *current;
 
@@ -61,6 +77,12 @@ void *inter_module_get(char *arg)
                 return ldlm_namespace_cleanup;
         else if (!strcmp(arg, "ldlm_replay_locks"))
                 return ldlm_replay_locks;
+        else if (!strcmp(arg, "osc_quota_interface"))
+                return &osc_quota_interface;
+        else if (!strcmp(arg, "mdc_quota_interface"))
+                return &mdc_quota_interface;
+        else if (!strcmp(arg, "lov_quota_interface"))
+                return &lov_quota_interface;
         else
                 return NULL;
 }
@@ -68,9 +90,6 @@ void *inter_module_get(char *arg)
 /*
  * random number generator stuff
  */
-#ifdef LIBLUSTRE_USE_URANDOM
-static int _rand_dev_fd = -1;
-#endif
 
 #ifdef HAVE_GETHOSTBYNAME
 static int get_ipv4_addr()
@@ -86,7 +105,7 @@ static int get_ipv4_addr()
         if (hptr == NULL ||
             hptr->h_addrtype != AF_INET ||
             *hptr->h_addr_list == NULL) {
-                printf("LibLustre: Warning: fail to get local IPv4 address\n");
+                CWARN("Warning: fail to get local IPv4 address\n");
                 return 0;
         }
 
@@ -98,50 +117,32 @@ static int get_ipv4_addr()
 
 void liblustre_init_random()
 {
-        int seed;
+        int seed[2];
         struct timeval tv;
 
 #ifdef LIBLUSTRE_USE_URANDOM
+        int _rand_dev_fd;
         _rand_dev_fd = syscall(SYS_open, "/dev/urandom", O_RDONLY);
         if (_rand_dev_fd >= 0) {
                 if (syscall(SYS_read, _rand_dev_fd,
-                            &seed, sizeof(int)) == sizeof(int)) {
-                        srand(seed);
+                            &seed, sizeof(seed)) == sizeof(seed)) {
+                        ll_srand(seed[0], seed[1]);
                         return;
                 }
                 syscall(SYS_close, _rand_dev_fd);
-                _rand_dev_fd = -1;
         }
 #endif /* LIBLUSTRE_USE_URANDOM */
 
 #ifdef HAVE_GETHOSTBYNAME
-        seed = get_ipv4_addr();
+        seed[0] = get_ipv4_addr();
 #else
-        seed = _my_pnid;
+        seed[0] = _my_pnid;
 #endif
         gettimeofday(&tv, NULL);
-        srand(tv.tv_sec + tv.tv_usec + getpid() + __swab32(seed));
-}
-
-void get_random_bytes(void *buf, int size)
-{
-        char *p = buf;
-        LASSERT(size >= 0);
-
-#ifdef LIBLUSTRE_USE_URANDOM
-        if (_rand_dev_fd >= 0) {
-                if (syscall(SYS_read, _rand_dev_fd, buf, size) == size)
-                        return;
-                syscall(SYS_close, _rand_dev_fd);
-                _rand_dev_fd = -1;
-        }
-#endif
-
-        while (size--) 
-                *p++ = rand();
+        ll_srand(tv.tv_sec ^ __swab32(seed[0]), tv.tv_usec ^__swab32(getpid()));
 }
 
-static void init_capability(int *res)
+static void init_capability(__u32 *res)
 {
 #ifdef HAVE_LIBCAP
         cap_t syscap;
@@ -152,8 +153,8 @@ static void init_capability(int *res)
 
         syscap = cap_get_proc();
         if (!syscap) {
-                printf("Liblustre: Warning: failed to get system capability, "
-                       "set to minimal\n");
+                CWARN("Warning: failed to get system capability, "
+                      "set to minimal\n");
                 return;
         }
 
@@ -224,20 +225,60 @@ int liblustre_init_current(char *comm)
         return 0;
 }
 
-void generate_random_uuid(unsigned char uuid_out[16])
+void cfs_cap_raise(cfs_cap_t cap)
 {
-        get_random_bytes(uuid_out, sizeof(uuid_out));
+        current->cap_effective |= (1 << cap);
+}
+
+void cfs_cap_lower(cfs_cap_t cap)
+{
+        current->cap_effective &= ~(1 << cap);
+}
+
+int cfs_cap_raised(cfs_cap_t cap)
+{
+        return current->cap_effective & (1 << cap);
+}
+
+void cfs_kernel_cap_pack(cfs_kernel_cap_t kcap, cfs_cap_t *cap)
+{
+        *cap = kcap;
+}
+
+void cfs_kernel_cap_unpack(cfs_kernel_cap_t *kcap, cfs_cap_t cap)
+{
+        *kcap = cap;
+}
+
+cfs_cap_t cfs_curproc_cap_pack(void) {
+        cfs_cap_t cap;
+        cfs_kernel_cap_pack(cfs_current()->cap_effective, &cap);
+        return cap;
+}
+
+void cfs_curproc_cap_unpack(cfs_cap_t cap) {
+        cfs_kernel_cap_unpack(&cfs_current()->cap_effective, cap);
+}
+
+int cfs_capable(cfs_cap_t cap)
+{
+        return cfs_cap_raised(cap);
 }
 
 int init_lib_portals()
 {
-        int max_interfaces;
         int rc;
         ENTRY;
 
-        rc = PtlInit(&max_interfaces);
-        if (rc != PTL_OK) {
-                CERROR("PtlInit failed: %d\n", rc);
+        rc = libcfs_debug_init(5 * 1024 * 1024);
+        if (rc != 0) {
+                CERROR("libcfs_debug_init() failed: %d\n", rc);
+                RETURN (-ENXIO);
+        }
+
+        rc = LNetInit();
+        if (rc != 0) {
+                CERROR("LNetInit() failed: %d\n", rc);
                 RETURN (-ENXIO);
         }
         RETURN(0);
@@ -246,5 +287,6 @@ int init_lib_portals()
 extern void ptlrpc_exit_portals(void);
 void cleanup_lib_portals()
 {
+        libcfs_debug_cleanup();
         ptlrpc_exit_portals();
 }