From 4cfa87dad0a9c08bba2f86650fafab9ad21fc524 Mon Sep 17 00:00:00 2001 From: "John L. Hammond" Date: Wed, 5 Mar 2014 19:08:24 -0600 Subject: [PATCH] LU-4699 libcfs: unify ERR_PTR definitions 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 Change-Id: I26b454f835b51f1fb17b94eec7cf184f0a82071e Reviewed-on: http://review.whamcloud.com/9519 Tested-by: Jenkins Tested-by: Maloo Reviewed-by: wangdi Reviewed-by: Dmitry Eremin Reviewed-by: Oleg Drokin --- libcfs/include/libcfs/Makefile.am | 39 +++++++++++++---- libcfs/include/libcfs/darwin/darwin-prim.h | 8 ---- libcfs/include/libcfs/err.h | 70 ++++++++++++++++++++++++++++++ libcfs/include/libcfs/libcfs.h | 15 ++++--- libcfs/include/libcfs/posix/libcfs.h | 5 --- libcfs/include/libcfs/winnt/winnt-fs.h | 4 -- lustre/include/cl_object.h | 6 --- lustre/include/darwin/obd_support.h | 5 --- lustre/include/linux/obd_support.h | 3 -- lustre/include/md_object.h | 6 --- 10 files changed, 109 insertions(+), 52 deletions(-) create mode 100644 libcfs/include/libcfs/err.h diff --git a/libcfs/include/libcfs/Makefile.am b/libcfs/include/libcfs/Makefile.am index 4025898..cb5ccb2 100644 --- a/libcfs/include/libcfs/Makefile.am +++ b/libcfs/include/libcfs/Makefile.am @@ -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 diff --git a/libcfs/include/libcfs/darwin/darwin-prim.h b/libcfs/include/libcfs/darwin/darwin-prim.h index 07edf35..c456e3f 100644 --- a/libcfs/include/libcfs/darwin/darwin-prim.h +++ b/libcfs/include/libcfs/darwin/darwin-prim.h @@ -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 index 0000000..3d24510 --- /dev/null +++ b/libcfs/include/libcfs/err.h @@ -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 + */ +#ifndef LIBCFS_ERR_H_ +#define LIBCFS_ERR_H_ + +#if defined(__linux__) && defined(__KERNEL__) +# include +#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_ */ diff --git a/libcfs/include/libcfs/libcfs.h b/libcfs/include/libcfs/libcfs.h index 8a5ac8a..01fd891 100644 --- a/libcfs/include/libcfs/libcfs.h +++ b/libcfs/include/libcfs/libcfs.h @@ -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 #include #include #include @@ -310,16 +311,16 @@ void cfs_get_random_bytes(void *buf, int size); #include /* 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 diff --git a/libcfs/include/libcfs/posix/libcfs.h b/libcfs/include/libcfs/posix/libcfs.h index 180d18e..d96afcc 100644 --- a/libcfs/include/libcfs/posix/libcfs.h +++ b/libcfs/include/libcfs/posix/libcfs.h @@ -109,11 +109,6 @@ # 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 diff --git a/libcfs/include/libcfs/winnt/winnt-fs.h b/libcfs/include/libcfs/winnt/winnt-fs.h index 41b207c..492cc2e 100644 --- a/libcfs/include/libcfs/winnt/winnt-fs.h +++ b/libcfs/include/libcfs/winnt/winnt-fs.h @@ -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__*/ diff --git a/lustre/include/cl_object.h b/lustre/include/cl_object.h index 7ccff98..d933936 100644 --- a/lustre/include/cl_object.h +++ b/lustre/include/cl_object.h @@ -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); diff --git a/lustre/include/darwin/obd_support.h b/lustre/include/darwin/obd_support.h index db3f101..5d2c939 100644 --- a/lustre/include/darwin/obd_support.h +++ b/lustre/include/darwin/obd_support.h @@ -41,9 +41,4 @@ #include -/* for obd_class.h */ -# ifndef ERR_PTR -# define ERR_PTR(a) ((void *)(a)) -# endif - #endif diff --git a/lustre/include/linux/obd_support.h b/lustre/include/linux/obd_support.h index 352b38e..bc85713 100644 --- a/lustre/include/linux/obd_support.h +++ b/lustre/include/linux/obd_support.h @@ -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 diff --git a/lustre/include/md_object.h b/lustre/include/md_object.h index c1ec562..caf1e16 100644 --- a/lustre/include/md_object.h +++ b/lustre/include/md_object.h @@ -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); -- 1.8.3.1