Whamcloud - gitweb
LU-9859 libcfs: prepare for switch to container_of_safe() 36/37736/6
authorMr NeilBrown <neilb@suse.de>
Thu, 27 Feb 2020 02:21:07 +0000 (13:21 +1100)
committerOleg Drokin <green@whamcloud.com>
Tue, 17 Mar 2020 03:40:52 +0000 (03:40 +0000)
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 <neilb@suse.de>
Change-Id: Ie938b76d569de6bb10b51fb0b12d79fcd0f43e7e
Signed-off-by: Mr NeilBrown <neilb@suse.de>
Reviewed-on: https://review.whamcloud.com/37736
Tested-by: Maloo <maloo@whamcloud.com>
Tested-by: jenkins <devops@whamcloud.com>
Reviewed-by: Shaun Tancheff <shaun.tancheff@hpe.com>
Reviewed-by: James Simmons <jsimmons@infradead.org>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
contrib/scripts/spelling.txt
libcfs/include/libcfs/libcfs.h
libcfs/include/libcfs/linux/linux-misc.h
lustre/llite/llite_nfs.c

index 286a2c2..9a96834 100644 (file)
@@ -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
index 2007dc6..3d17af6 100644 (file)
@@ -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;
 
index bff63ba..ab1e2ff 100644 (file)
@@ -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__ */
index e079eb4..4907d68 100644 (file)
@@ -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);