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