xref: /linux-6.15/include/linux/kobject.h (revision 5d4a2e29)
1 /*
2  * kobject.h - generic kernel object infrastructure.
3  *
4  * Copyright (c) 2002-2003 Patrick Mochel
5  * Copyright (c) 2002-2003 Open Source Development Labs
6  * Copyright (c) 2006-2008 Greg Kroah-Hartman <[email protected]>
7  * Copyright (c) 2006-2008 Novell Inc.
8  *
9  * This file is released under the GPLv2.
10  *
11  * Please read Documentation/kobject.txt before using the kobject
12  * interface, ESPECIALLY the parts about reference counts and object
13  * destructors.
14  */
15 
16 #ifndef _KOBJECT_H_
17 #define _KOBJECT_H_
18 
19 #include <linux/types.h>
20 #include <linux/list.h>
21 #include <linux/sysfs.h>
22 #include <linux/compiler.h>
23 #include <linux/spinlock.h>
24 #include <linux/kref.h>
25 #include <linux/kernel.h>
26 #include <linux/wait.h>
27 #include <asm/atomic.h>
28 
29 #define UEVENT_HELPER_PATH_LEN		256
30 #define UEVENT_NUM_ENVP			32	/* number of env pointers */
31 #define UEVENT_BUFFER_SIZE		2048	/* buffer for the variables */
32 
33 /* path to the userspace helper executed on an event */
34 extern char uevent_helper[];
35 
36 /* counter to tag the uevent, read only except for the kobject core */
37 extern u64 uevent_seqnum;
38 
39 /*
40  * The actions here must match the index to the string array
41  * in lib/kobject_uevent.c
42  *
43  * Do not add new actions here without checking with the driver-core
44  * maintainers. Action strings are not meant to express subsystem
45  * or device specific properties. In most cases you want to send a
46  * kobject_uevent_env(kobj, KOBJ_CHANGE, env) with additional event
47  * specific variables added to the event environment.
48  */
49 enum kobject_action {
50 	KOBJ_ADD,
51 	KOBJ_REMOVE,
52 	KOBJ_CHANGE,
53 	KOBJ_MOVE,
54 	KOBJ_ONLINE,
55 	KOBJ_OFFLINE,
56 	KOBJ_MAX
57 };
58 
59 struct kobject {
60 	const char		*name;
61 	struct list_head	entry;
62 	struct kobject		*parent;
63 	struct kset		*kset;
64 	struct kobj_type	*ktype;
65 	struct sysfs_dirent	*sd;
66 	struct kref		kref;
67 	unsigned int state_initialized:1;
68 	unsigned int state_in_sysfs:1;
69 	unsigned int state_add_uevent_sent:1;
70 	unsigned int state_remove_uevent_sent:1;
71 	unsigned int uevent_suppress:1;
72 };
73 
74 extern int kobject_set_name(struct kobject *kobj, const char *name, ...)
75 			    __attribute__((format(printf, 2, 3)));
76 extern int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
77 				  va_list vargs);
78 
79 static inline const char *kobject_name(const struct kobject *kobj)
80 {
81 	return kobj->name;
82 }
83 
84 extern void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
85 extern int __must_check kobject_add(struct kobject *kobj,
86 				    struct kobject *parent,
87 				    const char *fmt, ...);
88 extern int __must_check kobject_init_and_add(struct kobject *kobj,
89 					     struct kobj_type *ktype,
90 					     struct kobject *parent,
91 					     const char *fmt, ...);
92 
93 extern void kobject_del(struct kobject *kobj);
94 
95 extern struct kobject * __must_check kobject_create(void);
96 extern struct kobject * __must_check kobject_create_and_add(const char *name,
97 						struct kobject *parent);
98 
99 extern int __must_check kobject_rename(struct kobject *, const char *new_name);
100 extern int __must_check kobject_move(struct kobject *, struct kobject *);
101 
102 extern struct kobject *kobject_get(struct kobject *kobj);
103 extern void kobject_put(struct kobject *kobj);
104 
105 extern char *kobject_get_path(struct kobject *kobj, gfp_t flag);
106 
107 struct kobj_type {
108 	void (*release)(struct kobject *kobj);
109 	const struct sysfs_ops *sysfs_ops;
110 	struct attribute **default_attrs;
111 	const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
112 	const void *(*namespace)(struct kobject *kobj);
113 };
114 
115 struct kobj_uevent_env {
116 	char *envp[UEVENT_NUM_ENVP];
117 	int envp_idx;
118 	char buf[UEVENT_BUFFER_SIZE];
119 	int buflen;
120 };
121 
122 struct kset_uevent_ops {
123 	int (* const filter)(struct kset *kset, struct kobject *kobj);
124 	const char *(* const name)(struct kset *kset, struct kobject *kobj);
125 	int (* const uevent)(struct kset *kset, struct kobject *kobj,
126 		      struct kobj_uevent_env *env);
127 };
128 
129 struct kobj_attribute {
130 	struct attribute attr;
131 	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
132 			char *buf);
133 	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
134 			 const char *buf, size_t count);
135 };
136 
137 extern const struct sysfs_ops kobj_sysfs_ops;
138 
139 /*
140  * Namespace types which are used to tag kobjects and sysfs entries.
141  * Network namespace will likely be the first.
142  */
143 enum kobj_ns_type {
144 	KOBJ_NS_TYPE_NONE = 0,
145 	KOBJ_NS_TYPE_NET,
146 	KOBJ_NS_TYPES
147 };
148 
149 struct sock;
150 
151 /*
152  * Callbacks so sysfs can determine namespaces
153  *   @current_ns: return calling task's namespace
154  *   @netlink_ns: return namespace to which a sock belongs (right?)
155  *   @initial_ns: return the initial namespace (i.e. init_net_ns)
156  */
157 struct kobj_ns_type_operations {
158 	enum kobj_ns_type type;
159 	const void *(*current_ns)(void);
160 	const void *(*netlink_ns)(struct sock *sk);
161 	const void *(*initial_ns)(void);
162 };
163 
164 int kobj_ns_type_register(const struct kobj_ns_type_operations *ops);
165 int kobj_ns_type_registered(enum kobj_ns_type type);
166 const struct kobj_ns_type_operations *kobj_child_ns_ops(struct kobject *parent);
167 const struct kobj_ns_type_operations *kobj_ns_ops(struct kobject *kobj);
168 
169 const void *kobj_ns_current(enum kobj_ns_type type);
170 const void *kobj_ns_netlink(enum kobj_ns_type type, struct sock *sk);
171 const void *kobj_ns_initial(enum kobj_ns_type type);
172 void kobj_ns_exit(enum kobj_ns_type type, const void *ns);
173 
174 
175 /**
176  * struct kset - a set of kobjects of a specific type, belonging to a specific subsystem.
177  *
178  * A kset defines a group of kobjects.  They can be individually
179  * different "types" but overall these kobjects all want to be grouped
180  * together and operated on in the same manner.  ksets are used to
181  * define the attribute callbacks and other common events that happen to
182  * a kobject.
183  *
184  * @list: the list of all kobjects for this kset
185  * @list_lock: a lock for iterating over the kobjects
186  * @kobj: the embedded kobject for this kset (recursion, isn't it fun...)
187  * @uevent_ops: the set of uevent operations for this kset.  These are
188  * called whenever a kobject has something happen to it so that the kset
189  * can add new environment variables, or filter out the uevents if so
190  * desired.
191  */
192 struct kset {
193 	struct list_head list;
194 	spinlock_t list_lock;
195 	struct kobject kobj;
196 	const struct kset_uevent_ops *uevent_ops;
197 };
198 
199 extern void kset_init(struct kset *kset);
200 extern int __must_check kset_register(struct kset *kset);
201 extern void kset_unregister(struct kset *kset);
202 extern struct kset * __must_check kset_create_and_add(const char *name,
203 						const struct kset_uevent_ops *u,
204 						struct kobject *parent_kobj);
205 
206 static inline struct kset *to_kset(struct kobject *kobj)
207 {
208 	return kobj ? container_of(kobj, struct kset, kobj) : NULL;
209 }
210 
211 static inline struct kset *kset_get(struct kset *k)
212 {
213 	return k ? to_kset(kobject_get(&k->kobj)) : NULL;
214 }
215 
216 static inline void kset_put(struct kset *k)
217 {
218 	kobject_put(&k->kobj);
219 }
220 
221 static inline struct kobj_type *get_ktype(struct kobject *kobj)
222 {
223 	return kobj->ktype;
224 }
225 
226 extern struct kobject *kset_find_obj(struct kset *, const char *);
227 
228 /* The global /sys/kernel/ kobject for people to chain off of */
229 extern struct kobject *kernel_kobj;
230 /* The global /sys/kernel/mm/ kobject for people to chain off of */
231 extern struct kobject *mm_kobj;
232 /* The global /sys/hypervisor/ kobject for people to chain off of */
233 extern struct kobject *hypervisor_kobj;
234 /* The global /sys/power/ kobject for people to chain off of */
235 extern struct kobject *power_kobj;
236 /* The global /sys/firmware/ kobject for people to chain off of */
237 extern struct kobject *firmware_kobj;
238 
239 #if defined(CONFIG_HOTPLUG)
240 int kobject_uevent(struct kobject *kobj, enum kobject_action action);
241 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
242 			char *envp[]);
243 
244 int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)
245 	__attribute__((format (printf, 2, 3)));
246 
247 int kobject_action_type(const char *buf, size_t count,
248 			enum kobject_action *type);
249 #else
250 static inline int kobject_uevent(struct kobject *kobj,
251 				 enum kobject_action action)
252 { return 0; }
253 static inline int kobject_uevent_env(struct kobject *kobj,
254 				      enum kobject_action action,
255 				      char *envp[])
256 { return 0; }
257 
258 static inline int add_uevent_var(struct kobj_uevent_env *env,
259 				 const char *format, ...)
260 { return 0; }
261 
262 static inline int kobject_action_type(const char *buf, size_t count,
263 				      enum kobject_action *type)
264 { return -EINVAL; }
265 #endif
266 
267 #endif /* _KOBJECT_H_ */
268