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