From da63a46c51e86953dc62db76b8fa4d1a0f1aef4d Mon Sep 17 00:00:00 2001 From: Mr NeilBrown Date: Thu, 27 Feb 2020 13:21:07 +1100 Subject: [PATCH] LU-9859 libcfs: prepare for switch to container_of_safe() Upstream Linux uses container_of_safe() rather than container_of0(). So provide container_of_safe() for all kernels, and use spelling.txt to suggest new code uses it. We can then start switching code over gradually. Test-Parameters: trivial Signed-off-by: Mr NeilBrown Change-Id: Ie938b76d569de6bb10b51fb0b12d79fcd0f43e7e Signed-off-by: Mr NeilBrown Reviewed-on: https://review.whamcloud.com/37736 Tested-by: Maloo Tested-by: jenkins Reviewed-by: Shaun Tancheff Reviewed-by: James Simmons Reviewed-by: Oleg Drokin --- contrib/scripts/spelling.txt | 1 + libcfs/include/libcfs/libcfs.h | 12 +----------- libcfs/include/libcfs/linux/linux-misc.h | 17 +++++++++++++++++ lustre/llite/llite_nfs.c | 6 ++++-- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/contrib/scripts/spelling.txt b/contrib/scripts/spelling.txt index 286a2c2..9a96834 100644 --- a/contrib/scripts/spelling.txt +++ b/contrib/scripts/spelling.txt @@ -100,6 +100,7 @@ cfs_time_current_sec||ktime_get_real_seconds CLASSERT||BUILD_BUG_ON() msecs_to_jiffies||cfs_time_seconds DEFINE_TIMER||CFS_DEFINE_TIMER +container_of0||container_of_safe DN_MAX_BONUSLEN||DN_BONUS_SIZE(dnodesize) DN_OLD_MAX_BONUSLEN||DN_BONUS_SIZE(DNODE_MIN_SIZE) from_timer||cfs_from_timer diff --git a/libcfs/include/libcfs/libcfs.h b/libcfs/include/libcfs/libcfs.h index 2007dc6..3d17af6 100644 --- a/libcfs/include/libcfs/libcfs.h +++ b/libcfs/include/libcfs/libcfs.h @@ -113,17 +113,7 @@ void cfs_restore_sigs(sigset_t); int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data); -/* container_of depends on "likely" which is defined in libcfs_private.h */ -static inline void *__container_of(const void *ptr, unsigned long 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((ptr), offsetof(type, member))) +#define container_of0(ptr, type, member) container_of_safe(ptr, type, member) extern struct workqueue_struct *cfs_rehash_wq; diff --git a/libcfs/include/libcfs/linux/linux-misc.h b/libcfs/include/libcfs/linux/linux-misc.h index bff63ba..ab1e2ff 100644 --- a/libcfs/include/libcfs/linux/linux-misc.h +++ b/libcfs/include/libcfs/linux/linux-misc.h @@ -97,4 +97,21 @@ int kstrtobool_from_user(const char __user *s, size_t count, bool *res); void cfs_arch_init(void); +#ifndef container_of_safe +/** + * container_of_safe - cast a member of a structure out to the containing structure + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + * + * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged. + * + * Note: Copied from Linux 5.6, with BUILD_BUG_ON_MSG section removed. + */ +#define container_of_safe(ptr, type, member) ({ \ + void *__mptr = (void *)(ptr); \ + IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \ + ((type *)(__mptr - offsetof(type, member))); }) +#endif + #endif /* __LIBCFS_LINUX_MISC_H__ */ diff --git a/lustre/llite/llite_nfs.c b/lustre/llite/llite_nfs.c index e079eb4..4907d68 100644 --- a/lustre/llite/llite_nfs.c +++ b/lustre/llite/llite_nfs.c @@ -219,9 +219,11 @@ ll_nfs_get_name_filldir(struct dir_context *ctx, const char *name, int namelen, #endif /* HAVE_FILLDIR_USE_CTX */ /* * It is hack to access lde_fid for comparison with lgd_fid. - * So the input 'name' must be part of the 'lu_dirent'. + * So the input 'name' must be part of the 'lu_dirent', and + * so must appear to be a non-const pointer to an empty array. */ - struct lu_dirent *lde = container_of0(name, struct lu_dirent, lde_name); + char (*n)[0] = (void *)name; + struct lu_dirent *lde = container_of0(n, struct lu_dirent, lde_name); struct lu_fid fid; fid_le_to_cpu(&fid, &lde->lde_fid); -- 1.8.3.1