Whamcloud - gitweb
LU-4699 libcfs: unify ERR_PTR definitions 19/9519/4
authorJohn L. Hammond <john.hammond@intel.com>
Thu, 6 Mar 2014 01:08:24 +0000 (19:08 -0600)
committerOleg Drokin <oleg.drokin@intel.com>
Fri, 4 Apr 2014 02:39:13 +0000 (02:39 +0000)
Add libcfs/err.h which either includes linux/err.h or defines
equiavlent functions (including ERR_CAST()) for use by userspace or
any of Lustre's other imaginary platforms. Modify the definition of
__container_of() to remove the pre-call explicit cast. Remove two
unused function that used container_of0() on an already dereferenced
pointer.

Signed-off-by: John L. Hammond <john.hammond@intel.com>
Change-Id: I26b454f835b51f1fb17b94eec7cf184f0a82071e
Reviewed-on: http://review.whamcloud.com/9519
Tested-by: Jenkins
Tested-by: Maloo <hpdd-maloo@intel.com>
Reviewed-by: wangdi <di.wang@intel.com>
Reviewed-by: Dmitry Eremin <dmitry.eremin@intel.com>
Reviewed-by: Oleg Drokin <oleg.drokin@intel.com>
libcfs/include/libcfs/Makefile.am
libcfs/include/libcfs/darwin/darwin-prim.h
libcfs/include/libcfs/err.h [new file with mode: 0644]
libcfs/include/libcfs/libcfs.h
libcfs/include/libcfs/posix/libcfs.h
libcfs/include/libcfs/winnt/winnt-fs.h
lustre/include/cl_object.h
lustre/include/darwin/obd_support.h
lustre/include/linux/obd_support.h
lustre/include/md_object.h

index 4025898..cb5ccb2 100644 (file)
@@ -4,11 +4,34 @@ SUBDIRS += darwin
 endif
 DIST_SUBDIRS = linux posix util darwin
 
-EXTRA_DIST = curproc.h libcfs_private.h libcfs.h list.h \
-               user-lock.h user-prim.h user-time.h user-mem.h \
-               user-tcpip.h user-bitops.h bitmap.h user-crypto.h \
-               libcfs_prim.h libcfs_time.h libcfs_hash.h libcfs_cpu.h \
-               libcfs_debug.h libcfsutil.h libcfs_ioctl.h \
-               libcfs_pack.h libcfs_unpack.h libcfs_string.h \
-               libcfs_kernelcomm.h libcfs_workitem.h lucache.h \
-               libcfs_fail.h params_tree.h libcfs_crypto.h libcfs_heap.h
+EXTRA_DIST = \
+       bitmap.h \
+       curproc.h \
+       err.h \
+       libcfs.h \
+       libcfs_cpu.h \
+       libcfs_crypto.h \
+       libcfs_debug.h \
+       libcfs_fail.h \
+       libcfs_hash.h \
+       libcfs_heap.h \
+       libcfs_ioctl.h \
+       libcfs_kernelcomm.h \
+       libcfs_pack.h \
+       libcfs_prim.h \
+       libcfs_private.h \
+       libcfs_string.h \
+       libcfs_time.h \
+       libcfs_unpack.h \
+       libcfs_workitem.h \
+       libcfsutil.h \
+       list.h \
+       lucache.h \
+       params_tree.h \
+       user-bitops.h \
+       user-crypto.h \
+       user-lock.h \
+       user-mem.h \
+       user-prim.h \
+       user-tcpip.h \
+       user-time.h
index 07edf35..c456e3f 100644 (file)
@@ -497,14 +497,6 @@ static inline int request_module(const char *name, ...)
 #define KERN_INFO       "<6>"   /* informational                        */
 #define KERN_DEBUG      "<7>"   /* debug-level messages                 */
 
-static inline long PTR_ERR(const void *ptr)
-{
-       return (long) ptr;
-}
-
-#define ERR_PTR(err)   ((void *)err)
-#define IS_ERR(p)      ((unsigned long)(p) + 1000 < 1000)
-
 #else  /* !__KERNEL__ */
 
 typedef struct cfs_proc_dir_entry {
diff --git a/libcfs/include/libcfs/err.h b/libcfs/include/libcfs/err.h
new file mode 100644 (file)
index 0000000..3d24510
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * GPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * 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) 2014, Intel Corporation.
+ * Author: John L. Hammond <john.hammond@intel.com>
+ */
+#ifndef LIBCFS_ERR_H_
+#define LIBCFS_ERR_H_
+
+#if defined(__linux__) && defined(__KERNEL__)
+# include <linux/err.h>
+#else /* __KERNEL__ */
+
+# define IS_ERR_VALUE(x) ((x) >= (unsigned long)-4095)
+
+static inline void *ERR_PTR(long error)
+{
+       return (void *)error;
+}
+
+static inline long PTR_ERR(const void *ptr)
+{
+       return (long)ptr;
+}
+
+static inline long IS_ERR(const void *ptr)
+{
+       return IS_ERR_VALUE((unsigned long)ptr);
+}
+
+static inline long IS_ERR_OR_NULL(const void *ptr)
+{
+       return IS_ERR_VALUE((unsigned long)ptr) || ptr == NULL;
+}
+
+/**
+ * ERR_CAST - Explicitly cast an error-valued pointer to another pointer type
+ * @ptr: The pointer to cast.
+ *
+ * Explicitly cast an error-valued pointer to another pointer type in such a
+ * way as to make it clear that's what's going on.
+ */
+static inline void *ERR_CAST(const void *ptr)
+{
+       /* Cast away the const. */
+       return (void *)ptr;
+}
+
+# endif /* !__KERNEL__ */
+
+#endif /* LIBCFS_ERR_H_ */
index 8a5ac8a..01fd891 100644 (file)
@@ -294,6 +294,7 @@ unsigned int cfs_rand(void);
 void cfs_srand(unsigned int, unsigned int);
 void cfs_get_random_bytes(void *buf, int size);
 
+#include <libcfs/err.h>
 #include <libcfs/libcfs_debug.h>
 #include <libcfs/libcfs_cpu.h>
 #include <libcfs/libcfs_private.h>
@@ -310,16 +311,16 @@ void cfs_get_random_bytes(void *buf, int size);
 #include <libcfs/libcfs_crypto.h>
 
 /* container_of depends on "likely" which is defined in libcfs_private.h */
-static inline void *__container_of(void *ptr, unsigned long shift)
+static inline void *__container_of(const void *ptr, unsigned long shift)
 {
-        if (unlikely(IS_ERR(ptr) || ptr == NULL))
-                return ptr;
-        else
-                return (char *)ptr - shift;
+       if (unlikely(IS_ERR(ptr) || ptr == NULL))
+               return ERR_CAST(ptr);
+       else
+               return (char *)ptr - shift;
 }
 
-#define container_of0(ptr, type, member) \
-        ((type *)__container_of((void *)(ptr), offsetof(type, member)))
+#define container_of0(ptr, type, member)                               \
+       ((type *)__container_of((ptr), offsetof(type, member)))
 
 #define _LIBCFS_H
 
index 180d18e..d96afcc 100644 (file)
 # define do_gettimeofday(tv) gettimeofday(tv, NULL);
 typedef unsigned long long cfs_cycles_t;
 
-#define IS_ERR(a) ((unsigned long)(a) > (unsigned long)-1000L)
-#define IS_ERR_VALUE(a) (IS_ERR(a))
-#define PTR_ERR(a) ((long)(a))
-#define ERR_PTR(a) ((void*)((long)(a)))
-
 /* this goes in posix-fs.h */
 #include <sys/mount.h>
 
index 41b207c..492cc2e 100644 (file)
@@ -477,8 +477,4 @@ struct dentry {
  *  misc
  */
 
-#define ERR_PTR(error) ((void *)(long_ptr_t)(error))
-#define PTR_ERR(ptr)   ((long)(long_ptr_t) (ptr))
-#define IS_ERR(ptr)    ((long)(((ulong_ptr_t) (ptr)) > (ulong_ptr_t)(-1000L)))
-
 #endif /* __LIBCFS_WINNT_CFS_FS_H__*/
index 7ccff98..d933936 100644 (file)
@@ -2636,12 +2636,6 @@ static inline struct cl_object *cl_object_next(const struct cl_object *obj)
         return obj ? lu2cl(lu_object_next(&obj->co_lu)) : NULL;
 }
 
-static inline struct cl_device *cl_object_device(const struct cl_object *o)
-{
-        LASSERT(o == NULL || IS_ERR(o) || lu_device_is_cl(o->co_lu.lo_dev));
-        return container_of0(o->co_lu.lo_dev, struct cl_device, cd_lu_dev);
-}
-
 static inline struct cl_object_header *luh2coh(const struct lu_object_header *h)
 {
         return container_of0(h, struct cl_object_header, coh_lu);
index db3f101..5d2c939 100644 (file)
@@ -41,9 +41,4 @@
 
 #include <darwin/lustre_compat.h>
 
-/* for obd_class.h */
-# ifndef ERR_PTR
-#  define ERR_PTR(a) ((void *)(a))
-# endif
-
 #endif
index 352b38e..bc85713 100644 (file)
@@ -64,9 +64,6 @@
 #else /* !__KERNEL__ */
 # define LTIME_S(time) (time)
 /* for obd_class.h */
-# ifndef ERR_PTR
-#  define ERR_PTR(a) ((void *)(a))
-# endif
 #endif  /* __KERNEL__ */
 
 #endif
index c1ec562..caf1e16 100644 (file)
@@ -479,12 +479,6 @@ static inline struct md_object *md_object_next(const struct md_object *obj)
         return (obj ? lu2md(lu_object_next(&obj->mo_lu)) : NULL);
 }
 
-static inline struct md_device *md_obj2dev(const struct md_object *o)
-{
-        LASSERT(o == NULL || IS_ERR(o) || lu_device_is_md(o->mo_lu.lo_dev));
-        return container_of0(o->mo_lu.lo_dev, struct md_device, md_lu_dev);
-}
-
 static inline int md_device_init(struct md_device *md, struct lu_device_type *t)
 {
         return lu_device_init(&md->md_lu_dev, t);