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