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 | * $FreeBSD$ |
---|
30 | */ |
---|
31 | #ifndef _LINUX_KOBJECT_H_ |
---|
32 | #define _LINUX_KOBJECT_H_ |
---|
33 | |
---|
34 | #include <machine/stdarg.h> |
---|
35 | |
---|
36 | #include <linux/kernel.h> |
---|
37 | #include <linux/kref.h> |
---|
38 | #include <linux/slab.h> |
---|
39 | |
---|
40 | #if 0 |
---|
41 | struct kobject; |
---|
42 | struct sysctl_oid; |
---|
43 | |
---|
44 | struct kobj_type { |
---|
45 | void (*release)(struct kobject *kobj); |
---|
46 | const struct sysfs_ops *sysfs_ops; |
---|
47 | struct attribute **default_attrs; |
---|
48 | }; |
---|
49 | |
---|
50 | extern struct kobj_type kfree_type; |
---|
51 | |
---|
52 | struct kobject { |
---|
53 | struct kobject *parent; |
---|
54 | char *name; |
---|
55 | struct kref kref; |
---|
56 | struct kobj_type *ktype; |
---|
57 | struct list_head entry; |
---|
58 | struct sysctl_oid *oidp; |
---|
59 | }; |
---|
60 | |
---|
61 | extern struct kobject *mm_kobj; |
---|
62 | |
---|
63 | static inline void |
---|
64 | kobject_init(struct kobject *kobj, struct kobj_type *ktype) |
---|
65 | { |
---|
66 | |
---|
67 | kref_init(&kobj->kref); |
---|
68 | INIT_LIST_HEAD(&kobj->entry); |
---|
69 | kobj->ktype = ktype; |
---|
70 | kobj->oidp = NULL; |
---|
71 | } |
---|
72 | |
---|
73 | static inline void kobject_put(struct kobject *kobj); |
---|
74 | void kobject_release(struct kref *kref); |
---|
75 | |
---|
76 | static inline void |
---|
77 | kobject_put(struct kobject *kobj) |
---|
78 | { |
---|
79 | |
---|
80 | if (kobj) |
---|
81 | kref_put(&kobj->kref, kobject_release); |
---|
82 | } |
---|
83 | |
---|
84 | static inline struct kobject * |
---|
85 | kobject_get(struct kobject *kobj) |
---|
86 | { |
---|
87 | |
---|
88 | if (kobj) |
---|
89 | kref_get(&kobj->kref); |
---|
90 | return kobj; |
---|
91 | } |
---|
92 | |
---|
93 | static inline int |
---|
94 | kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list args) |
---|
95 | { |
---|
96 | char *old; |
---|
97 | char *name; |
---|
98 | |
---|
99 | old = kobj->name; |
---|
100 | |
---|
101 | if (old && !fmt) |
---|
102 | return 0; |
---|
103 | |
---|
104 | name = kzalloc(MAXPATHLEN, GFP_KERNEL); |
---|
105 | if (!name) |
---|
106 | return -ENOMEM; |
---|
107 | vsnprintf(name, MAXPATHLEN, fmt, args); |
---|
108 | kobj->name = name; |
---|
109 | kfree(old); |
---|
110 | for (; *name != '\0'; name++) |
---|
111 | if (*name == '/') |
---|
112 | *name = '!'; |
---|
113 | return (0); |
---|
114 | } |
---|
115 | |
---|
116 | int kobject_add(struct kobject *kobj, struct kobject *parent, |
---|
117 | const char *fmt, ...); |
---|
118 | |
---|
119 | static inline struct kobject * |
---|
120 | kobject_create(void) |
---|
121 | { |
---|
122 | struct kobject *kobj; |
---|
123 | |
---|
124 | kobj = kzalloc(sizeof(*kobj), GFP_KERNEL); |
---|
125 | if (kobj == NULL) |
---|
126 | return (NULL); |
---|
127 | kobject_init(kobj, &kfree_type); |
---|
128 | |
---|
129 | return (kobj); |
---|
130 | } |
---|
131 | |
---|
132 | static inline struct kobject * |
---|
133 | kobject_create_and_add(const char *name, struct kobject *parent) |
---|
134 | { |
---|
135 | struct kobject *kobj; |
---|
136 | |
---|
137 | kobj = kobject_create(); |
---|
138 | if (kobj == NULL) |
---|
139 | return (NULL); |
---|
140 | if (kobject_add(kobj, parent, "%s", name) == 0) |
---|
141 | return (kobj); |
---|
142 | kobject_put(kobj); |
---|
143 | |
---|
144 | return (NULL); |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | static inline char * |
---|
149 | kobject_name(const struct kobject *kobj) |
---|
150 | { |
---|
151 | |
---|
152 | return kobj->name; |
---|
153 | } |
---|
154 | |
---|
155 | int kobject_set_name(struct kobject *kobj, const char *fmt, ...); |
---|
156 | int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, |
---|
157 | struct kobject *parent, const char *fmt, ...); |
---|
158 | |
---|
159 | /* sysfs.h calles for 'kobject' which is defined here, |
---|
160 | * so we need to add the include only after the 'kobject' def. |
---|
161 | */ |
---|
162 | #include <linux/sysfs.h> |
---|
163 | |
---|
164 | struct kobj_attribute { |
---|
165 | struct attribute attr; |
---|
166 | ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr, |
---|
167 | char *buf); |
---|
168 | ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, |
---|
169 | const char *buf, size_t count); |
---|
170 | }; |
---|
171 | #endif |
---|
172 | |
---|
173 | #endif /* _LINUX_KOBJECT_H_ */ |
---|