Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 05e6557b authored by NeilBrown's avatar NeilBrown Committed by Greg Kroah-Hartman
Browse files

staging: lustre: add container_of_safe()



Luster has a container_of0() function which is similar to
container_of() but passes an IS_ERR_OR_NULL() pointer through
unchanged.
This could be generally useful: bcache at last has a similar function.

Naming is hard, but the precedent set by hlist_entry_safe() suggests
a _safe suffix might be most consistent.

So add container_of_safe() to kernel.h, and replace all occurrences of
container_of0() with one of
  - list_first_entry, list_next_entry, when that is a better fit,
  - container_of(), when the pointer is used as a validpointer in
    surrounding code,
  - container_of_safe() when there is no obviously better alternative.

Signed-off-by: default avatarNeilBrown <neilb@suse.com>
Reviewed-by: default avatarJames Simmons <jsimmons@infradead.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 93aa2c2a
Loading
Loading
Loading
Loading
+0 −11
Original line number Diff line number Diff line
@@ -143,17 +143,6 @@ int libcfs_ioctl_getdata(struct libcfs_ioctl_hdr **hdr_pp,
int libcfs_ioctl_data_adjust(struct libcfs_ioctl_data *data);
int libcfs_ioctl(unsigned long cmd, void __user *arg);

/* container_of depends on "likely" which is defined in libcfs_private.h */
static inline void *__container_of(void *ptr, unsigned long shift)
{
	if (IS_ERR_OR_NULL(ptr))
		return ptr;
	return (char *)ptr - shift;
}

#define container_of0(ptr, type, member) \
	((type *)__container_of((void *)(ptr), offsetof(type, member)))

#define _LIBCFS_H

extern struct miscdevice libcfs_dev;
+5 −5
Original line number Diff line number Diff line
@@ -1941,7 +1941,7 @@ static inline int lu_device_is_cl(const struct lu_device *d)
static inline struct cl_device *lu2cl_dev(const struct lu_device *d)
{
	LASSERT(!d || IS_ERR(d) || lu_device_is_cl(d));
	return container_of0(d, struct cl_device, cd_lu_dev);
	return container_of_safe(d, struct cl_device, cd_lu_dev);
}

static inline struct lu_device *cl2lu_dev(struct cl_device *d)
@@ -1952,13 +1952,13 @@ static inline struct lu_device *cl2lu_dev(struct cl_device *d)
static inline struct cl_object *lu2cl(const struct lu_object *o)
{
	LASSERT(!o || IS_ERR(o) || lu_device_is_cl(o->lo_dev));
	return container_of0(o, struct cl_object, co_lu);
	return container_of_safe(o, struct cl_object, co_lu);
}

static inline const struct cl_object_conf *
lu2cl_conf(const struct lu_object_conf *conf)
{
	return container_of0(conf, struct cl_object_conf, coc_lu);
	return container_of_safe(conf, struct cl_object_conf, coc_lu);
}

static inline struct cl_object *cl_object_next(const struct cl_object *obj)
@@ -1969,12 +1969,12 @@ static inline struct cl_object *cl_object_next(const struct cl_object *obj)
static inline struct cl_device *cl_object_device(const struct cl_object *o)
{
	LASSERT(!o || 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);
	return container_of_safe(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);
	return container_of_safe(h, struct cl_object_header, coh_lu);
}

static inline struct cl_site *cl_object_site(const struct cl_object *obj)
+3 −3
Original line number Diff line number Diff line
@@ -745,15 +745,15 @@ struct lu_object *lu_object_find_slice(const struct lu_env *env,
static inline struct lu_object *lu_object_top(struct lu_object_header *h)
{
	LASSERT(!list_empty(&h->loh_layers));
	return container_of0(h->loh_layers.next, struct lu_object, lo_linkage);
	return list_first_entry(&h->loh_layers, struct lu_object, lo_linkage);
}

/**
 * Next sub-object in the layering
 */
static inline struct lu_object *lu_object_next(const struct lu_object *o)
static inline const struct lu_object *lu_object_next(const struct lu_object *o)
{
	return container_of0(o->lo_linkage.next, struct lu_object, lo_linkage);
	return list_next_entry(o, lo_linkage);
}

/**
+1 −1
Original line number Diff line number Diff line
@@ -223,7 +223,7 @@ static int ll_nfs_get_name_filldir(struct dir_context *ctx, const char *name,
	/* It is hack to access lde_fid for comparison with lgd_fid.
	 * So the input 'name' must be part of the 'lu_dirent'.
	 */
	struct lu_dirent *lde = container_of0(name, struct lu_dirent, lde_name);
	struct lu_dirent *lde = container_of((void*)name, struct lu_dirent, lde_name);
	struct ll_getname_data *lgd =
		container_of(ctx, struct ll_getname_data, ctx);
	struct lu_fid fid;
+4 −4
Original line number Diff line number Diff line
@@ -263,22 +263,22 @@ static inline struct lu_device *vvp2lu_dev(struct vvp_device *vdv)

static inline struct vvp_device *lu2vvp_dev(const struct lu_device *d)
{
	return container_of0(d, struct vvp_device, vdv_cl.cd_lu_dev);
	return container_of_safe(d, struct vvp_device, vdv_cl.cd_lu_dev);
}

static inline struct vvp_device *cl2vvp_dev(const struct cl_device *d)
{
	return container_of0(d, struct vvp_device, vdv_cl);
	return container_of_safe(d, struct vvp_device, vdv_cl);
}

static inline struct vvp_object *cl2vvp(const struct cl_object *obj)
{
	return container_of0(obj, struct vvp_object, vob_cl);
	return container_of_safe(obj, struct vvp_object, vob_cl);
}

static inline struct vvp_object *lu2vvp(const struct lu_object *obj)
{
	return container_of0(obj, struct vvp_object, vob_cl.co_lu);
	return container_of_safe(obj, struct vvp_object, vob_cl.co_lu);
}

static inline struct inode *vvp_object_inode(const struct cl_object *obj)
Loading