blob: d37cd7f10e3dfd955062c400641745ab6ef14eac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
7 * This file is released under the GPLv2.
8 *
9 *
10 * Please read Documentation/kobject.txt before using the kobject
11 * interface, ESPECIALLY the parts about reference counts and object
12 * destructors.
13 */
14
15#ifndef _KOBJECT_H_
16#define _KOBJECT_H_
17
18#ifdef __KERNEL__
19
20#include <linux/types.h>
21#include <linux/list.h>
22#include <linux/sysfs.h>
Andrew Morton4a7fb632006-08-14 22:43:17 -070023#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/spinlock.h>
25#include <linux/rwsem.h>
26#include <linux/kref.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
NeilBrown4508a7a2006-03-20 17:53:53 +110028#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/atomic.h>
30
Kay Sievers312c0042005-11-16 09:00:00 +010031#define KOBJ_NAME_LEN 20
32#define UEVENT_HELPER_PATH_LEN 256
Kay Sievers0296b222005-11-11 05:33:52 +010033
34/* path to the userspace helper executed on an event */
Kay Sievers312c0042005-11-16 09:00:00 +010035extern char uevent_helper[];
Kay Sievers0296b222005-11-11 05:33:52 +010036
Kay Sievers312c0042005-11-16 09:00:00 +010037/* counter to tag the uevent, read only except for the kobject core */
38extern u64 uevent_seqnum;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Kay Sievers0296b222005-11-11 05:33:52 +010040/* the actions here must match the proper string in lib/kobject_uevent.c */
41typedef int __bitwise kobject_action_t;
42enum kobject_action {
Kay Sievers5f123fb2005-11-11 14:43:07 +010043 KOBJ_ADD = (__force kobject_action_t) 0x01, /* exclusive to core */
44 KOBJ_REMOVE = (__force kobject_action_t) 0x02, /* exclusive to core */
45 KOBJ_CHANGE = (__force kobject_action_t) 0x03, /* device state change */
Greg Kroah-Hartmanfa675762006-02-22 09:39:02 -080046 KOBJ_MOUNT = (__force kobject_action_t) 0x04, /* mount event for block devices (broken) */
47 KOBJ_UMOUNT = (__force kobject_action_t) 0x05, /* umount event for block devices (broken) */
48 KOBJ_OFFLINE = (__force kobject_action_t) 0x06, /* device offline */
49 KOBJ_ONLINE = (__force kobject_action_t) 0x07, /* device online */
Cornelia Huck8a824722006-11-20 17:07:51 +010050 KOBJ_MOVE = (__force kobject_action_t) 0x08, /* device move */
Kay Sievers0296b222005-11-11 05:33:52 +010051};
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053struct kobject {
Dmitry Torokhovf3b4f3c2005-04-26 02:32:00 -050054 const char * k_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 char name[KOBJ_NAME_LEN];
56 struct kref kref;
57 struct list_head entry;
58 struct kobject * parent;
59 struct kset * kset;
60 struct kobj_type * ktype;
61 struct dentry * dentry;
NeilBrown4508a7a2006-03-20 17:53:53 +110062 wait_queue_head_t poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
65extern int kobject_set_name(struct kobject *, const char *, ...)
66 __attribute__((format(printf,2,3)));
67
Dmitry Torokhovf3b4f3c2005-04-26 02:32:00 -050068static inline const char * kobject_name(const struct kobject * kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
70 return kobj->k_name;
71}
72
73extern void kobject_init(struct kobject *);
74extern void kobject_cleanup(struct kobject *);
75
Andrew Morton4a7fb632006-08-14 22:43:17 -070076extern int __must_check kobject_add(struct kobject *);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070077extern int __must_check kobject_shadow_add(struct kobject *, struct dentry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078extern void kobject_del(struct kobject *);
79
Andrew Morton4a7fb632006-08-14 22:43:17 -070080extern int __must_check kobject_rename(struct kobject *, const char *new_name);
Eric W. Biedermanb592fcf2007-01-24 12:35:52 -070081extern int __must_check kobject_shadow_rename(struct kobject *kobj,
82 struct dentry *new_parent,
83 const char *new_name);
Cornelia Huck8a824722006-11-20 17:07:51 +010084extern int __must_check kobject_move(struct kobject *, struct kobject *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Andrew Morton4a7fb632006-08-14 22:43:17 -070086extern int __must_check kobject_register(struct kobject *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087extern void kobject_unregister(struct kobject *);
88
89extern struct kobject * kobject_get(struct kobject *);
90extern void kobject_put(struct kobject *);
91
Kay Sievers86406242007-03-14 03:25:56 +010092extern struct kobject *kobject_kset_add_dir(struct kset *kset,
93 struct kobject *, const char *);
Jun'ichi Nomura74231722006-03-13 17:14:25 -050094extern struct kobject *kobject_add_dir(struct kobject *, const char *);
95
Al Virofd4f2df2005-10-21 03:18:50 -040096extern char * kobject_get_path(struct kobject *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98struct kobj_type {
99 void (*release)(struct kobject *);
100 struct sysfs_ops * sysfs_ops;
101 struct attribute ** default_attrs;
102};
103
104
105/**
106 * kset - a set of kobjects of a specific type, belonging
107 * to a specific subsystem.
108 *
109 * All kobjects of a kset should be embedded in an identical
110 * type. This type may have a descriptor, which the kset points
111 * to. This allows there to exist sets of objects of the same
112 * type in different subsystems.
113 *
114 * A subsystem does not have to be a list of only one type
115 * of object; multiple ksets can belong to one subsystem. All
116 * ksets of a subsystem share the subsystem's lock.
117 *
Kay Sievers312c0042005-11-16 09:00:00 +0100118 * Each kset can support specific event variables; it can
119 * supress the event generation or add subsystem specific
120 * variables carried with the event.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
Kay Sievers312c0042005-11-16 09:00:00 +0100122struct kset_uevent_ops {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 int (*filter)(struct kset *kset, struct kobject *kobj);
Dmitry Torokhov419cab32005-04-26 02:32:54 -0500124 const char *(*name)(struct kset *kset, struct kobject *kobj);
Kay Sievers312c0042005-11-16 09:00:00 +0100125 int (*uevent)(struct kset *kset, struct kobject *kobj, char **envp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int num_envp, char *buffer, int buffer_size);
127};
128
129struct kset {
130 struct subsystem * subsys;
131 struct kobj_type * ktype;
132 struct list_head list;
133 spinlock_t list_lock;
134 struct kobject kobj;
Kay Sievers312c0042005-11-16 09:00:00 +0100135 struct kset_uevent_ops * uevent_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138
139extern void kset_init(struct kset * k);
Andrew Morton4a7fb632006-08-14 22:43:17 -0700140extern int __must_check kset_add(struct kset * k);
141extern int __must_check kset_register(struct kset * k);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142extern void kset_unregister(struct kset * k);
143
144static inline struct kset * to_kset(struct kobject * kobj)
145{
146 return kobj ? container_of(kobj,struct kset,kobj) : NULL;
147}
148
149static inline struct kset * kset_get(struct kset * k)
150{
151 return k ? to_kset(kobject_get(&k->kobj)) : NULL;
152}
153
154static inline void kset_put(struct kset * k)
155{
156 kobject_put(&k->kobj);
157}
158
159static inline struct kobj_type * get_ktype(struct kobject * k)
160{
161 if (k->kset && k->kset->ktype)
162 return k->kset->ktype;
163 else
164 return k->ktype;
165}
166
167extern struct kobject * kset_find_obj(struct kset *, const char *);
168
169
170/**
171 * Use this when initializing an embedded kset with no other
172 * fields to initialize.
173 */
174#define set_kset_name(str) .kset = { .kobj = { .name = str } }
175
176
177
178struct subsystem {
179 struct kset kset;
180 struct rw_semaphore rwsem;
181};
182
Kay Sievers312c0042005-11-16 09:00:00 +0100183#define decl_subsys(_name,_type,_uevent_ops) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184struct subsystem _name##_subsys = { \
185 .kset = { \
186 .kobj = { .name = __stringify(_name) }, \
187 .ktype = _type, \
Kay Sievers312c0042005-11-16 09:00:00 +0100188 .uevent_ops =_uevent_ops, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 } \
190}
Kay Sievers312c0042005-11-16 09:00:00 +0100191#define decl_subsys_name(_varname,_name,_type,_uevent_ops) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192struct subsystem _varname##_subsys = { \
193 .kset = { \
194 .kobj = { .name = __stringify(_name) }, \
195 .ktype = _type, \
Kay Sievers312c0042005-11-16 09:00:00 +0100196 .uevent_ops =_uevent_ops, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 } \
198}
199
200/* The global /sys/kernel/ subsystem for people to chain off of */
201extern struct subsystem kernel_subsys;
Michael Holzheu40394832006-05-09 12:53:49 +0200202/* The global /sys/hypervisor/ subsystem */
203extern struct subsystem hypervisor_subsys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205/**
206 * Helpers for setting the kset of registered objects.
207 * Often, a registered object belongs to a kset embedded in a
208 * subsystem. These do no magic, just make the resulting code
209 * easier to follow.
210 */
211
212/**
213 * kobj_set_kset_s(obj,subsys) - set kset for embedded kobject.
214 * @obj: ptr to some object type.
215 * @subsys: a subsystem object (not a ptr).
216 *
217 * Can be used for any object type with an embedded ->kobj.
218 */
219
220#define kobj_set_kset_s(obj,subsys) \
221 (obj)->kobj.kset = &(subsys).kset
222
223/**
224 * kset_set_kset_s(obj,subsys) - set kset for embedded kset.
225 * @obj: ptr to some object type.
226 * @subsys: a subsystem object (not a ptr).
227 *
228 * Can be used for any object type with an embedded ->kset.
229 * Sets the kset of @obj's embedded kobject (via its embedded
230 * kset) to @subsys.kset. This makes @obj a member of that
231 * kset.
232 */
233
234#define kset_set_kset_s(obj,subsys) \
235 (obj)->kset.kobj.kset = &(subsys).kset
236
237/**
238 * subsys_set_kset(obj,subsys) - set kset for subsystem
239 * @obj: ptr to some object type.
240 * @subsys: a subsystem object (not a ptr).
241 *
242 * Can be used for any object type with an embedded ->subsys.
243 * Sets the kset of @obj's kobject to @subsys.kset. This makes
244 * the object a member of that kset.
245 */
246
247#define subsys_set_kset(obj,_subsys) \
248 (obj)->subsys.kset.kobj.kset = &(_subsys).kset
249
250extern void subsystem_init(struct subsystem *);
Andrew Morton4a7fb632006-08-14 22:43:17 -0700251extern int __must_check subsystem_register(struct subsystem *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252extern void subsystem_unregister(struct subsystem *);
253
254static inline struct subsystem * subsys_get(struct subsystem * s)
255{
256 return s ? container_of(kset_get(&s->kset),struct subsystem,kset) : NULL;
257}
258
259static inline void subsys_put(struct subsystem * s)
260{
261 kset_put(&s->kset);
262}
263
264struct subsys_attribute {
265 struct attribute attr;
266 ssize_t (*show)(struct subsystem *, char *);
267 ssize_t (*store)(struct subsystem *, const char *, size_t);
268};
269
Andrew Morton4a7fb632006-08-14 22:43:17 -0700270extern int __must_check subsys_create_file(struct subsystem * ,
271 struct subsys_attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Kay Sievers4d17ffd2006-04-25 15:37:26 +0200273#if defined(CONFIG_HOTPLUG)
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800274int kobject_uevent(struct kobject *kobj, enum kobject_action action);
275int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,
Cornelia Huck8a824722006-11-20 17:07:51 +0100276 char *envp[]);
Kay Sievers0296b222005-11-11 05:33:52 +0100277
Kay Sievers312c0042005-11-16 09:00:00 +0100278int add_uevent_var(char **envp, int num_envp, int *cur_index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 char *buffer, int buffer_size, int *cur_len,
280 const char *format, ...)
281 __attribute__((format (printf, 7, 8)));
282#else
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800283static inline int kobject_uevent(struct kobject *kobj, enum kobject_action action)
284{ return 0; }
285static inline int kobject_uevent_env(struct kobject *kobj,
Cornelia Huck8a824722006-11-20 17:07:51 +0100286 enum kobject_action action,
287 char *envp[])
Aneesh Kumar K.V542cfce2006-12-19 13:01:27 -0800288{ return 0; }
Kay Sievers5f123fb2005-11-11 14:43:07 +0100289
Kay Sievers312c0042005-11-16 09:00:00 +0100290static inline int add_uevent_var(char **envp, int num_envp, int *cur_index,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 char *buffer, int buffer_size, int *cur_len,
292 const char *format, ...)
293{ return 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#endif
295
296#endif /* __KERNEL__ */
297#endif /* _KOBJECT_H_ */