]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/kobject.h
bcache in userspace; userspace fsck
[bcachefs-tools-debian] / include / linux / kobject.h
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 <greg@kroah.com>
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/atomic.h>
20 #include <linux/bug.h>
21 #include <linux/compiler.h>
22 #include <linux/kernel.h>
23 #include <linux/kref.h>
24 #include <linux/sysfs.h>
25 #include <linux/types.h>
26 #include <linux/wait.h>
27 #include <linux/workqueue.h>
28
29 struct kset;
30
31 struct kobj_type {
32         void (*release)(struct kobject *kobj);
33         const struct sysfs_ops *sysfs_ops;
34         struct attribute **default_attrs;
35         const struct kobj_ns_type_operations *(*child_ns_type)(struct kobject *kobj);
36         const void *(*namespace)(struct kobject *kobj);
37 };
38
39 struct kobj_uevent_env {
40 };
41
42 struct kobj_attribute {
43         struct attribute attr;
44         ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
45                         char *buf);
46         ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
47                          const char *buf, size_t count);
48 };
49
50 struct kobject {
51         struct kobject          *parent;
52         struct kset             *kset;
53         struct kobj_type        *ktype;
54         struct kernfs_node      *sd; /* sysfs directory entry */
55         struct kref             kref;
56         unsigned int state_initialized:1;
57         unsigned int state_in_sysfs:1;
58         unsigned int state_add_uevent_sent:1;
59         unsigned int state_remove_uevent_sent:1;
60         unsigned int uevent_suppress:1;
61 };
62
63 struct kset {
64         struct kobject          kobj;
65 };
66
67 static inline struct kobj_type *get_ktype(struct kobject *kobj)
68 {
69         return kobj->ktype;
70 }
71
72 #define kobject_add(...)        0
73
74 static inline void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
75 {
76         memset(kobj, 0, sizeof(*kobj));
77
78         kref_init(&kobj->kref);
79         kobj->ktype = ktype;
80         kobj->state_initialized = 1;
81 }
82
83 static inline void kobject_del(struct kobject *kobj);
84
85 static inline void kobject_cleanup(struct kobject *kobj)
86 {
87         struct kobj_type *t = get_ktype(kobj);
88
89         /* remove from sysfs if the caller did not do it */
90         if (kobj->state_in_sysfs)
91                 kobject_del(kobj);
92
93         if (t && t->release)
94                 t->release(kobj);
95 }
96
97 static inline void kobject_release(struct kref *kref)
98 {
99         struct kobject *kobj = container_of(kref, struct kobject, kref);
100
101         kobject_cleanup(kobj);
102 }
103
104 static inline void kobject_put(struct kobject *kobj)
105 {
106         BUG_ON(!kobj);
107         BUG_ON(!kobj->state_initialized);
108
109         kref_put(&kobj->kref, kobject_release);
110 }
111
112 static inline void kobject_del(struct kobject *kobj)
113 {
114         struct kernfs_node *sd;
115
116         if (!kobj)
117                 return;
118
119         sd = kobj->sd;
120         kobj->state_in_sysfs = 0;
121 #if 0
122         kobj_kset_leave(kobj);
123 #endif
124         kobject_put(kobj->parent);
125         kobj->parent = NULL;
126 }
127
128 static inline struct kobject *kobject_get(struct kobject *kobj)
129 {
130         BUG_ON(!kobj);
131         BUG_ON(!kobj->state_initialized);
132
133         kref_get(&kobj->kref);
134         return kobj;
135 }
136
137 static inline void kset_unregister(struct kset *kset) {}
138
139 #define kset_create_and_add(_name, _u, _parent)                         \
140         ((struct kset *) kzalloc(sizeof(struct kset), GFP_KERNEL))
141
142 #endif /* _KOBJECT_H_ */