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/list.h>
39 #include <linux/slab.h>
40 
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 const struct kobj_type linux_kfree_type;
51 
52 struct kobject {
53 	struct kobject		*parent;
54 	char			*name;
55 	struct kref		kref;
56 	const struct kobj_type	*ktype;
57 	struct list_head	entry;
58 	struct sysctl_oid	*oidp;
59 };
60 
61 extern struct kobject *mm_kobj;
62 
63 struct attribute {
64 	const char	*name;
65 	struct module	*owner;
66 	mode_t		mode;
67 };
68 
69 struct kobj_attribute {
70 	struct attribute attr;
71 	ssize_t (*show)(struct kobject *kobj, struct kobj_attribute *attr,
72 	    char *buf);
73 	ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr,
74 	    const char *buf, size_t count);
75 };
76 
77 static inline void
78 kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
79 {
80 
81 	kref_init(&kobj->kref);
82 	INIT_LIST_HEAD(&kobj->entry);
83 	kobj->ktype = ktype;
84 	kobj->oidp = NULL;
85 }
86 
87 void linux_kobject_release(struct kref *kref);
88 
89 static inline void
90 kobject_put(struct kobject *kobj)
91 {
92 
93 	if (kobj)
94 		kref_put(&kobj->kref, linux_kobject_release);
95 }
96 
97 static inline struct kobject *
98 kobject_get(struct kobject *kobj)
99 {
100 
101 	if (kobj)
102 		kref_get(&kobj->kref);
103 	return kobj;
104 }
105 
106 int	kobject_set_name_vargs(struct kobject *kobj, const char *fmt, va_list);
107 int	kobject_add(struct kobject *kobj, struct kobject *parent,
108 	    const char *fmt, ...);
109 
110 static inline struct kobject *
111 kobject_create(void)
112 {
113 	struct kobject *kobj;
114 
115 	kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
116 	if (kobj == NULL)
117 		return (NULL);
118 	kobject_init(kobj, &linux_kfree_type);
119 
120 	return (kobj);
121 }
122 
123 static inline struct kobject *
124 kobject_create_and_add(const char *name, struct kobject *parent)
125 {
126 	struct kobject *kobj;
127 
128 	kobj = kobject_create();
129 	if (kobj == NULL)
130 		return (NULL);
131 	if (kobject_add(kobj, parent, "%s", name) == 0)
132 		return (kobj);
133 	kobject_put(kobj);
134 
135 	return (NULL);
136 }
137 
138 static inline void
139 kobject_del(struct kobject *kobj __unused)
140 {
141 }
142 
143 static inline char *
144 kobject_name(const struct kobject *kobj)
145 {
146 
147 	return kobj->name;
148 }
149 
150 int	kobject_set_name(struct kobject *kobj, const char *fmt, ...);
151 int	kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
152 	    struct kobject *parent, const char *fmt, ...);
153 
154 #endif /* _LINUX_KOBJECT_H_ */
155