Whamcloud - gitweb
LU-6245 libcfs: use libcfs_private.h only for kernel space
[fs/lustre-release.git] / lustre / utils / libiam.c
index 36a7765..b0649ac 100644 (file)
@@ -1,31 +1,41 @@
-/* -*- 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.
  *
- *  libiam.c
- *  iam user level library
+ * 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.
  *
- *   Copyright (c) 2006 Cluster File Systems, Inc.
- *   Author: Wang Di <wangdi@clusterfs.com>
- *   Author: Nikita Danilov <nikita@clusterfs.com>
- *   Author: Fan Yong <fanyong@clusterfs.com>
+ * 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).
  *
- *   This file is part of the Lustre file system, http://www.lustre.org
- *   Lustre is a trademark of Cluster File Systems, Inc.
+ * 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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Copyright (c) 2014, 2015, Intel Corporation.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
  *
- *   You may have signed or agreed to another license before downloading
- *   this software.  If so, you are bound by the terms and conditions
- *   of that agreement, and the following does not apply to you.  See the
- *   LICENSE file included with this distribution for more information.
+ * lustre/utils/libiam.c
  *
- *   If you did not agree to a different license, then this copy of Lustre
- *   is open source 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.
+ * iam user level library
  *
- *   In either case, 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
- *   license text for more details.
+ * Author: Wang Di <wangdi@clusterfs.com>
+ * Author: Nikita Danilov <nikita@clusterfs.com>
+ * Author: Fan Yong <fanyong@clusterfs.com>
  */
 
 #include <unistd.h>
 #include <stdio.h>
 #include <fcntl.h>
 #include <string.h>
+#include <endian.h>
 #include <errno.h>
-#include <assert.h>
-
+#include <sys/ioctl.h>
 #include <sys/types.h>
 
-#ifdef HAVE_ENDIAN_H
-#include <endian.h>
-#endif
-
-#include <libcfs/libcfs.h>
-#include <liblustre.h>
+#include <libcfs/util/string.h>
 #include <lustre/libiam.h>
 
 typedef __u32 lvar_hash_t;
@@ -101,6 +106,18 @@ enum {
         LVAR_ROUND = LVAR_PAD - 1
 };
 
+/**
+ * Stores \a val at \a dst, where the latter is possibly unaligned. Uses
+ * memcpy(). This macro is needed to avoid dependency of user level tools on
+ * the kernel headers.
+ */
+#define STORE_UNALIGNED(val, dst)               \
+({                                              \
+        typeof(*(dst)) __val = (val);           \
+                                                \
+        memcpy(dst, &__val, sizeof *(dst));     \
+})
+
 static int root_limit(int rootgap, int blocksize, int size)
 {
         int limit;
@@ -127,10 +144,10 @@ static void lfix_root(void *buf,
 
         root = buf;
         *root = (typeof(*root)) {
-                .ilr_magic           = cpu_to_le64(IAM_LFIX_ROOT_MAGIC),
-                .ilr_keysize         = cpu_to_le16(keysize),
-                .ilr_recsize         = cpu_to_le16(recsize),
-                .ilr_ptrsize         = cpu_to_le16(ptrsize),
+               .ilr_magic           = htole64(IAM_LFIX_ROOT_MAGIC),
+               .ilr_keysize         = htole16(keysize),
+               .ilr_recsize         = htole16(recsize),
+               .ilr_ptrsize         = htole16(ptrsize),
                 .ilr_indirect_levels = 0
         };
 
@@ -139,7 +156,7 @@ static void lfix_root(void *buf,
                 /*
                  * limit itself + one pointer to the leaf.
                  */
-                .count = cpu_to_le16(2),
+               .count = htole16(2),
                 .limit = lfix_root_limit(blocksize, keysize + ptrsize)
         };
 
@@ -159,9 +176,9 @@ static void lfix_root(void *buf,
         entry += keysize;
         /* now @entry points to <ptr> */
         if (ptrsize == 4)
-                *(u_int32_t *)entry = cpu_to_le32(1);
+               STORE_UNALIGNED(htole32(1), (u_int32_t *)entry);
         else
-                *(u_int64_t *)entry = cpu_to_le64(1);
+               STORE_UNALIGNED(htole64(1), (u_int64_t *)entry);
 }
 
 static void lfix_leaf(void *buf,
@@ -172,12 +189,12 @@ static void lfix_leaf(void *buf,
         /* form leaf */
         head = buf;
         *head = (typeof(*head)) {
-                .ill_magic = cpu_to_le16(IAM_LEAF_HEADER_MAGIC),
+               .ill_magic = htole16(IAM_LEAF_HEADER_MAGIC),
                 /*
                  * Leaf contains an entry with the smallest possible key
                  * (created by zeroing).
                  */
-                .ill_count = cpu_to_le16(1),
+               .ill_count = htole16(1),
         };
 }
 
@@ -197,9 +214,9 @@ static void lvar_root(void *buf,
         isize = sizeof(lvar_hash_t) + ptrsize;
         root = buf;
         *root = (typeof(*root)) {
-                .vr_magic            = cpu_to_le32(IAM_LVAR_ROOT_MAGIC),
-                .vr_recsize          = cpu_to_le16(recsize),
-                .vr_ptrsize          = cpu_to_le16(ptrsize),
+               .vr_magic            = htole32(IAM_LVAR_ROOT_MAGIC),
+               .vr_recsize          = htole16(recsize),
+               .vr_ptrsize          = htole16(ptrsize),
                 .vr_indirect_levels  = 0
         };
 
@@ -208,7 +225,7 @@ static void lvar_root(void *buf,
                 /*
                  * limit itself + one pointer to the leaf.
                  */
-                .count = cpu_to_le16(2),
+               .count = htole16(2),
                 .limit = lvar_root_limit(blocksize, keysize + ptrsize)
         };
 
@@ -228,9 +245,9 @@ static void lvar_root(void *buf,
         entry += sizeof(lvar_hash_t);
         /* now @entry points to <ptr> */
         if (ptrsize == 4)
-                *(u_int32_t *)entry = cpu_to_le32(1);
+               STORE_UNALIGNED(htole32(1), (u_int32_t *)entry);
         else
-                *(u_int64_t *)entry = cpu_to_le64(1);
+               STORE_UNALIGNED(htole64(1), (u_int64_t *)entry);
 }
 
 static int lvar_esize(int namelen, int recsize)
@@ -248,8 +265,8 @@ static void lvar_leaf(void *buf,
         /* form leaf */
         head = buf;
         *head = (typeof(*head)) {
-                .vlh_magic = cpu_to_le16(IAM_LVAR_LEAF_MAGIC),
-                .vlh_used  = cpu_to_le16(sizeof *head + lvar_esize(0, recsize))
+               .vlh_magic = htole16(IAM_LVAR_LEAF_MAGIC),
+               .vlh_used  = htole16(sizeof *head + lvar_esize(0, recsize))
         };
         rec = (void *)(head + 1);
         rec[offsetof(struct lvar_leaf_entry, vle_key)] = recsize;
@@ -295,7 +312,7 @@ static unsigned char *packdigit(unsigned char *number)
         unsigned char *area;
         unsigned char *scan;
 
-        area = calloc(strlen(number) / 2 + 2, sizeof(char));
+        area = calloc(strlen((char *)number) / 2 + 2, sizeof(char));
         if (area != NULL) {
                 for (scan = area; *number; number += 2, scan++)
                         *scan = (hex2dec(number[0]) << 4) | hex2dec(number[1]);
@@ -306,7 +323,7 @@ static unsigned char *packdigit(unsigned char *number)
 static char *iam_convert(int size, int need_convert, char *source)
 {
         char *ptr;
-        char *opt;
+        unsigned char *opt;
 
         if (source == NULL)
                 return NULL;
@@ -316,7 +333,7 @@ static char *iam_convert(int size, int need_convert, char *source)
                 return NULL;
 
         if (need_convert) {
-                opt = packdigit(source);
+                opt = packdigit((unsigned char*)source);
                 if (opt == NULL) {
                         free(ptr);
                         return NULL;
@@ -325,7 +342,7 @@ static char *iam_convert(int size, int need_convert, char *source)
                         free(opt);
                 }
         } else {
-                strncpy(ptr, source, size + 1);
+               strlcpy(ptr, source, size + 1);
         }
 
         return ptr;