xref: /freebsd/sys/compat/linuxkpi/common/include/linux/kobject.h (revision 9d9cc24662d95b56a87e2d92d57899224cec42df)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013, 2014 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUX_KOBJECT_H_
32 #define	_LINUX_KOBJECT_H_
33 
34 #include <machine/stdarg.h>
35 
36 #include <linux/kernel.h>
37 #include <linux/kref.h>
38 #include <linux/slab.h>
39 
40 struct kobject;
41 struct sysctl_oid;
42 
43 struct kobj_type {
44 	void (*release)(struct kobject *kobj);
45 	const struct sysfs_ops *sysfs_ops;
46 	struct attribute **default_attrs;
47 };
48 
49 extern struct kobj_type kfree_type;
50 
51 struct kobject {
52 	struct kobject		*parent;
53 	char			*name;
54 	struct kref		kref;
55 	struct kobj_type	*ktype;
56 	struct list_head	entry;
57 	struct sysctl_oid	*oidp;
58 };
59 
60 extern struct kobject *mm_kobj;
61 
62 static inline void
63 kobject_init(struct kobject *kobj, struct kobj_type *ktype)
64 {
65 
66 	kref_init(&kobj->kref);
67 	INIT_LIST_HEAD(&kobj->entry);
68 	kobj->ktype = ktype;
69 	kobj->oidp = NULL;
70 }
71 
72 static inline void kobject_put(struct kobject *kobj);
73 void kobject_release(struct kref *kref);
74 
75 static inline void
76 kobject_put(struct kobject *kobj)
77 {
78 
79 	if (kobj)
80 		kref_put(&kobj->kref, kobject_release);
81 }
82 
83 static inline struct kobject *
84 kobject_get(struct kobject *kobj)
85 {
86 
87 	if (kobj)
88 		kref_get(&kobj->kref);
89 	return kobj;
90 }
91 
92 static inline int
93 kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args)
94 {
95 	char *old;
96 	char *name;
97 
98 	old = kobj->name;
99 
100 	if (old && !fmt)
101 		return 0;
102 
103 	name = kzalloc(MAXPATHLEN, GFP_KERNEL);
104 	if (!name)
105 		return -ENOMEM;
106 	vsnprintf(name, MAXPATHLEN, fmt, args);
107 	kobj->name = name;
108 	kfree(old);
109 	for (; *name != '\0'; name++)
110 		if (*name == '/')
111 			*name = '!';
112 	return (0);
113 }
114 
115 int	kobject_add(struct kobject *kobj, struct kobject *parent,
116 	    const char *fmt, ...);
117 
118 static inline struct kobject *
119 kobject_create(void)
120 {
121 	struct kobject *kobj;
122 
123 	kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
124 	if (kobj == NULL)
125 		return (NULL);
126 	kobject_init(kobj, &kfree_type);
127 
128 	return (kobj);
129 }
130 
131 static inline struct kobject *
132 kobject_create_and_add(const char *name, struct kobject *parent)
133 {
134 	struct kobject *kobj;
135 
136 	kobj = kobject_create();
137 	if (kobj == NULL)
138 		return (NULL);
139 	if (kobject_add(kobj, parent, "%s", name) == 0)
140 		return (kobj);
141 	kobject_put(kobj);
142 
143 	return (NULL);
144 }
145 
146 
147 static inline char *
148 kobject_name(const struct kobject *kobj)
149 {
150 
151 	return kobj->name;
152 }
153 
154 int	kobject_set_name(struct kobject *kobj, const char *fmt, ...);
155 int	kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
156 	    struct kobject *parent, const char *fmt, ...);
157 
158 /* sysfs.h calles for 'kobject' which is defined here,
159  * so we need to add the include only after the 'kobject' def.
160  */
161 #include <linux/sysfs.h>
162 
163 struct kobj_attribute {
164         struct attribute attr;
165         ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
166                         char *buf);
167         ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
168                          const char *buf, size_t count);
169 };
170 
171 #endif /* _LINUX_KOBJECT_H_ */
172